Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
Fixed endless loop if target pipeline was canceled, improved doc
Browse files Browse the repository at this point in the history
  • Loading branch information
StiviiK committed Sep 16, 2021
1 parent b9d2122 commit 1d20501
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand All @@ -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"
``
21 changes: 11 additions & 10 deletions cmd/pipelines/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
Expand Down

0 comments on commit 1d20501

Please sign in to comment.