From 1d20501f725a586e31d91afb6a4c6396620c250f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=BCrzeder?= Date: Thu, 16 Sep 2021 15:14:17 +0200 Subject: [PATCH] Fixed endless loop if target pipeline was canceled, improved doc --- README.md | 33 +++++++++++++++++++++++++------- cmd/pipelines/trigger/trigger.go | 21 ++++++++++---------- pkg/azure/azure.go | 2 +- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 957b2ce..f541341 100644 --- a/README.md +++ b/README.md @@ -5,21 +5,22 @@ ## Description Image which can be used in Azure DevOps Pipelines to trigger other pipelines. + ## Usage -``` +```plaintext Usage: - devops-worker pipelines trigger [flags] + devops-worker pipelines trigger [flags] -- PIPELINES... Flags: -h, --help help for trigger -p, --project string Name of the Azure DevOps Project --targetRefName string (Optional) Specify the GitRef on which the Pipeline should run --targetVersion string (Optional) Specify the Commit Hash on which the Pipeline should run - -w, --waitForCompletion Specify if the task should block until the target pipeline is completed + -w, --waitForCompletion (Optional) Specify if the task should block until the target pipeline is completed Global Flags: - --token string Azure DevOps Personal Access Token (PAT) + --token string Azure DevOps Personal Access Token (PAT) / Value of $(System.AccessToken) --url string Azure DevOps Organization Url (must be https://dev.azure.com/ORG or https://ORG.visualstudio.com) ``` @@ -28,6 +29,24 @@ Global Flags: For authentication, you can use a Personal Access Token (PAT) or the Build-In `Build Service` User. In order to use the `Build Service` User, you need to manually grant the User permission to queue builds on the pipelines, then you can use as token value the value of the `System.AccessToken` built-in variable. E.g. `--token $(System.AccessToken)`. -## Example: - -ToDo +## Example + +```yaml +pool: + vmImage: 'ubuntu-latest' + +container: + image: whiteduck/azure-devops-pipeline-trigger-task:2.1.3 + +steps: + - script: | + devops-worker pipelines trigger \ + --url "$(System.CollectionUri)" \ + --token "$(System.AccessToken)" \ + --project "$(System.TeamProject)" \ + --targetRefName "$(Build.SourceBranchName)" \ + --targetVersion "$(Build.SourceVersion)" \ + --waitForCompletion \ + -- "My Demo Pipeline" + displayName: "Trigger 'My Demo Pipeline' pipeline" +`` \ No newline at end of file diff --git a/cmd/pipelines/trigger/trigger.go b/cmd/pipelines/trigger/trigger.go index 8b317a9..ae9cbe2 100644 --- a/cmd/pipelines/trigger/trigger.go +++ b/cmd/pipelines/trigger/trigger.go @@ -66,7 +66,7 @@ var Cmd = &cobra.Command{ Short: "Triggers the specified pipeline(s)", RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("No pipeline names have been passed") + return errors.New("no pipeline names have been passed") } flags := cmd.Flags() @@ -136,7 +136,7 @@ var Cmd = &cobra.Command{ } if *runResult.State != pipelines.RunStateValues.InProgress && *runResult.State != pipelines.RunStateValues.Completed { - return fmt.Errorf("Unkown error occured, result: %s, state: %s", string(*runResult.Result), string(*runResult.State)) + return fmt.Errorf("unkown error occured, result: %s, state: %s", string(*runResult.Result), string(*runResult.State)) } log.Info(*runResult.Url) @@ -153,18 +153,19 @@ var Cmd = &cobra.Command{ } if run.Result != nil { - if *run.Result == pipelines.RunResultValues.Succeeded { + switch *run.Result { + case pipelines.RunResultValues.Succeeded: json, err := json.Marshal(*run) if err != nil { - log.Errorln("Failed to serialize result into json", err) + return fmt.Errorf("failed to serialize result into json, %s", err.Error()) } else { log.Info(string(json)) + return nil } - break - } - - if *run.Result == pipelines.RunResultValues.Failed { - return fmt.Errorf("Pipeline %s failed", *pipeline.Name) + case pipelines.RunResultValues.Failed: + return fmt.Errorf("pipeline %s failed", *pipeline.Name) + case pipelines.RunResultValues.Canceled: + return fmt.Errorf("pipeline %s was canceled", *pipeline.Name) } } @@ -173,7 +174,7 @@ var Cmd = &cobra.Command{ } else { json, err := json.Marshal(*runResult) if err != nil { - log.Errorln("Failed to serialize result into json", err) + return fmt.Errorf("failed to serialize result into json, %s", err.Error()) } else { log.Info(string(json)) } diff --git a/pkg/azure/azure.go b/pkg/azure/azure.go index b83b471..9b46eaa 100644 --- a/pkg/azure/azure.go +++ b/pkg/azure/azure.go @@ -48,7 +48,7 @@ var ( PersonalAccessTokenFlagName FlagDefinition = FlagDefinition{ Name: "token", Default: "", - Description: "Azure DevOps Personal Access Token (PAT)", + Description: "Azure DevOps Personal Access Token (PAT) / Value of $(System.AccessToken)", Persistent: true, } )