Azure Pipeline Part 10 – Conditions Expressions
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss how to execute a stage, job or step in Azure Pipeline based on condition. You can use different in-built functions or even a custom expression in your Azure Pipeline condition block.
Azure Pipeline Part 10 – Resources
Prerequisites:
- One active Azure DevOps account
- Personal Access Token (PAT)
- A self-hosted agent registered to your Azure DevOps organization
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 |
Azure Pipeline – Conditions
You can specify the conditions under which each stage, job, or step runs. By default, a job or stage runs if it does not depend on any other job or stage, or if all of the jobs or stages that it depends on have completed and succeeded. By default, a step runs if nothing in its job has failed yet and the step immediately preceding it has finished. You can customize this behavior by forcing a stage, job, or step to run even if a previous dependency fails or by specifying a custom condition.
Azure Pipeline – Conditions Syntax:
1 2 3 4 5 6 7 8 9 10 |
## Syntax: stages: - stage: MyStage condition: jobs: - job: MyJob condition: steps: - script: echo Hello World! condition: |
Azure Pipeline – Conditions Example:
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 |
## ---------- ## Conditions ## ---------- ## Create pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool jobs: - job: Job1 steps: - script: echo This step will always run, even if the pipeline is canceled condition: always() - job: Job2 steps: - script: echo this job will always fail - script: exit 1 - job: Job3 dependsOn: Job2 condition: failed() steps: - script: echo this job will only run if Job2 fails EOF ## Push the changes to Azure Repo git add . git commit -m "update1" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – Expression:
Expressions can be used in many places where you need to specify a string, boolean, or number value when authoring a pipeline. The most common use of expressions is in conditions to determine whether a job or step should run.
Another common use of expressions is in defining variables. Expressions can be evaluated at compile time or at run time. Compile time expressions can be used anywhere; runtime expressions can be used in variables and conditions. Runtime expressions are intended as a way to compute the contents of variables and state (example: condition).
Azure Pipeline – Expression Syntax:
1 2 3 4 5 |
## Syntax: # ${{}} for compile time and $[] for runtime expressions. variables: a: ${{ b: $[ |
Azure Pipeline – Expression Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## ---------- ## Expression ## ---------- ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool variables: staticVar: 'cloudaffaire' # static variable compileVar: ${{ variables.staticVar }} # compile time expression isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')] # runtime expression steps: - script: | echo ${{variables.staticVar}} # outputs cloudaffaire echo $(compileVar) # outputs cloudaffaire echo $(isMain) # outputs True EOF ## Push the changes to Azure Repo git add . git commit -m "update2" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Clean up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## -------- ## 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 |
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