Azure Pipeline Part 2 – Triggers
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss different ways to trigger an Azure Pipeline. Azure Pipeline supports triggering an Azure Pipeline based on push, pull, schedule, from another pipeline or manually using UI or API.
Azure Pipeline Part 2 – Triggers
Prerequisites:
- One active Azure DevOps account
- Personal Access Token (PAT)
- A self-hosted agent registered to your Azure DevOps organization
Initial setup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
## ----- ## Setup ## ----- ## Define variables ORGANIZATION=" USER=$(az account show | jq -r .user.name) PROJECT=" PAT=' B64_PAT=$(printf "%s"":$PAT" | base64) ## Test if authentication working (List projects in your organization) curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Create a new Azure DevOps project curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "'"$PROJECT"'", "description": "Demo Project For Azure Pipeline", "capabilities": { "versioncontrol": { "sourceControlType": "Git" }, "processTemplate": { "templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45" } } }' \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Get project default repository details REPOSITORY_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq -r .value[0].id) && curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 | jq . ## Create a new Azure Pipeline using REST API curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "folder": null, "name": "mypipeline", "configuration": { "type": "yaml", "path": "/azure-pipelines.yml", "repository": { "id": "'"$REPOSITORY_ID"'", "name": "'"$PROJECT"'", "type": "azureReposGit" } } }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/pipelines?api-version=6.0-preview.1 | jq . ## Clone project default repository REPO_HTTPS_CLONE_URL=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq -r .value[0].webUrl ) && B64_PAT=$(printf "%s"":$PAT" | base64) && git -c http.extraHeader="Authorization: Basic ${B64_PAT}" \ clone $REPO_HTTPS_CLONE_URL cd $PROJECT |
Push trigger:
A push trigger specifies which branches, tags or path changes will result in Azure Pipeline trigger. Push triggers are defined using “trigger” keyword in Azure Pipeline config file. “trigger” keyword support “include” and “exclude” keywords to include or exclude an branch, tag or path from triggering your pipeline. You can also use wildcard characters with include or exclude keywords.
Note: If you specify an exclude clause without an include clause for branches, tags, or paths, it is equivalent to specifying * in the include clause.
Azure Pipeline Push Triggers Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Syntax: ## Push trigger trigger: batch: boolean # batch changes if true; start a new build for every push if false (default) branches: include: [ string ] # branch names which will trigger a build exclude: [ string ] # branch names which will not tags: include: [ string ] # tag names which will trigger a build exclude: [ string ] # tag names which will not paths: include: [ string ] # file paths which must match to trigger a build exclude: [ string ] # file paths which will not trigger a build |
Azure Pipeline Push Triggers Examples:
By default, any push to your Azure pipeline repository will trigger the pipeline if trigger keyword is excluded from the pipeline config file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## By default, any push will trigger the pipeline ## Create a new pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - script: echo Hello World! EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
You can also disable your Azure Pipeline from getting triggered using “trigger: none” in your pipeline config file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Do not trigger any pipeline execution ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: none # will disable CI builds entirely pool: mypool steps: - script: echo This pipeline will not get executed EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Trigger Azure Pipeline only for specific branches.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
## Trigger pipeline only in specific branches ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: branches: include: - mybranch1 pool: mypool steps: - script: echo This pipeline will not get executed as we are in main branch EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## Checkout to mybranch1 git checkout -b mybranch1 ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: branches: include: - mybranch1 - deb* pool: mypool steps: - script: echo This pipeline will get executed as we are in mybranch1 branch EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push --set-upstream origin mybranch1 ## Checkout to mybranch2 git checkout -b mybranch2 ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: branches: include: - mybranch1 - deb* exclude: - mybranch2 pool: mypool steps: - script: echo This pipeline will not get executed as we have excluded mybranch2 branch EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push --set-upstream origin mybranch2 ## Checkout to main git checkout main ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: branches: include: - '*' exclude: - mybranch2 pool: mypool steps: - script: echo This pipeline will get executed as we are not in mybranch2 branch EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Trigger Azure Pipeline only in specific tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## Trigger pipeline only in specific tags ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: tags: include: - mytag.v1 exclude: - mytag.v2 pool: mypool steps: - script: echo This pipeline will only get executed on mytag.v1 EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## Craete a new tag and push it git tag mytag.v1 git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push origin mytag.v1 |
Trigger Azure Pipeline only if specific file or directory content gets changed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
## Trigger pipeline only in specific files are changed ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml trigger: paths: include: - /mydir/myfile.txt exclude: - README.md pool: mypool steps: - script: echo This pipeline will only get executed if myfile.txt changes EOF ## Create a new README.md file echo "Hello world" > README.md ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## Add /mydir/myfile.txt mkdir mydir && echo "Hello world" > mydir/myfile.txt ## Push the changes to Azure Repo git add . git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Next, we will explore scheduled trigger in Azure Pipeline.
Scheduled trigger:
Scheduled triggers start your pipeline based on a schedule, such as a nightly build. Scheduled triggers are configured using “schedules” keyword in Azure Pipeline config file.
Azure Pipeline Scheduled Triggers Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
## Syntax ## Scheduled trigger schedules: - cron: string # cron syntax defining a schedule in UTC time displayName: string # friendly name given to a specific schedule branches: include: [ string ] # which branches the schedule applies to exclude: [ string ] # which branches to exclude from the schedule always: boolean # whether to always run the pipeline or only if there have been source # code changes since the last successful scheduled run. The default is false. ## cron expression ## mm HH DD MM DW ## \ \ \ \ \__ Days of week ## \ \ \ \____ Months ## \ \ \______ Days ## \ \________ Hours ## \__________ Minutes |
Azure Pipeline Scheduled Triggers Examples:
Trigger Azure Pipeline on specific schedule.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## Trigger pipeline on specific schedule ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml schedules: - cron: "0 0 * * *" displayName: Daily midnight build branches: include: - master exclude: - mybranch* always: true pool: mypool steps: - script: echo This pipeline will only get executed every day 00 am EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Pipeline Triggers:
You can execute another Azure Pipeline from an Azure Pipeline using “resource” keyword.
Azure Pipeline Triggers Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
## Syntax ## Pipeline trigger resources: pipelines: - pipeline: source: project: trigger: true # Run this pipeline when any run of source pipeline completes Azure Pipeline Triggers Example: ## Trigger one pipeline from another ## Define a new repository name REPOSITORY="debjeet" ## Create a new repository PROJECT_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$PROJECT"'") | .id') && curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "'"$REPOSITORY"'", "project": { "id": "'"$PROJECT_ID"'" } }' \ https://dev.azure.com/$ORGANIZATION/_apis/git/repositories?api-version=6.0 | jq . ## Get new Azure repo details REPOSITORY_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$REPOSITORY"'") | .id') && curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 | jq . ## Clone new Azure repo cd .. REPO_HTTPS_CLONE_URL=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 \ | jq -r .webUrl) && B64_PAT=$(printf "%s"":$PAT" | base64) && git -c http.extraHeader="Authorization: Basic ${B64_PAT}" \ clone $REPO_HTTPS_CLONE_URL && cd $REPOSITORY ## Create a new Azure Pipeline using the new Azure repo curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "folder": null, "name": "mynewpipeline", "configuration": { "type": "yaml", "path": "/azure-pipelines.yml", "repository": { "id": "'"$REPOSITORY_ID"'", "name": "'"$PROJECT"'", "type": "azureReposGit" } } }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/pipelines?api-version=6.0-preview.1 | jq . ## Create a new pipeline definition in new Azure repo cat <<'EOF'> azure-pipelines.yml trigger: none resources: pipelines: - pipeline: cloudaffaire source: 'mypipeline' trigger: true pool: mypool steps: - script: echo Hello world EOF ## Push the changes to new Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## Update and push in source pipeline (mypipeline) cd .. cd $PROJECT cat <<'EOF'> azure-pipelines.yml trigger: - main pool: mypool steps: - script: echo This pipeline will trigger mynewpipeline EOF ## Push the changes to Azure Repo git add azure-pipelines.yml git commit -m "pipeline updated" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Pull (PR) trigger:
A pull request trigger specifies which branches cause a pull request build to run. If you specify no pull request trigger, pull requests to any branch trigger a build. Sadly, YAML PR triggers are supported only in GitHub and Bitbucket Cloud and currently not supported for Azure Repos, hence not included an example.
Azure Pipeline Pull (PR) Trigger Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## Syntax: ## Pull trigger (Pull request) pr: autoCancel: boolean # indicates whether additional pushes to a PR should cancel # in-progress runs for the same PR. Defaults to true branches: include: [ string ] # branch names which will trigger a build exclude: [ string ] # branch names which will not paths: include: [ string ] # file paths which must match to trigger a build exclude: [ string ] # file paths which will not trigger a build drafts: boolean # For GitHub only, whether to build draft PRs, defaults to true |
Azure Pipeline Pull (PR) Trigger Example:
Not supported for Azure Repos, only GitHub and Bitbucket is supported.
Manual trigger:
You can also manually trigger an Azure Pipeline from Azure DevOps console or using REST API.
Manual trigger using Azure DevOps console:
Manual Trigger using REST API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## Manual Trigger ## Execute Azure pipeline manually using REST API PIPELINE_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/pipelines?api-version=6.0-preview.1 | jq .value[0].id) && curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "resources": { "repositories": { "self": { "refName": "refs/heads/main" } } } }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/pipelines/$PIPELINE_ID/runs?api-version=6.0-preview.1 | jq . |
Clean up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## -------- ## Clean up ## -------- ## Delete the Azure DevOps project and local repository PROJECT_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$PROJECT"'") | .id') && curl -s --request DELETE \ -u $USER:$PAT \ --header "Content-Type: application/json" \ https://dev.azure.com/$ORGANIZATION/_apis/projects/$PROJECT_ID?api-version=6.0 cd .. rm -rf $PROJECT rm -rf $REPOSITORY |
Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation
https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops