Azure Pipeline Part 13 – Logging
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss how to log errors and other useful information in Azure Pipeline using logging commands.
Azure Pipeline Part 13 – Logging
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 – Logging Commands:
Logging commands are how tasks and scripts communicate with the agent. They cover actions like creating new variables, marking a step as failed, and uploading artifacts. Logging commands are useful when you are troubleshooting a pipeline.
Azure Pipeline – Logging Commands Syntax:
1 2 |
## Syntax: ##vso[area.action property1=value;property2=value;...]message |
Azure Pipeline – Logging – Formatting Commands:
You can log an error or other types of messages in your Azure Pipeline using formatting commands. This messaged will be visible to the Azure Pipeline UI.
Azure Pipeline – Logging – Formatting Commands Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## ------------------- ## Formatting commands ## ------------------- ## Create pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - bash: | echo "##[group]Beginning of a group" echo "##[warning]This is Warning message" echo "##[error]This Error message" echo "##[section]Start of a section" echo "##[debug]Debug text" echo "##[command]Command-line being run" echo "##[endgroup]" EOF ## Push the changes to Azure Repo git add . git commit -m "update1" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
If you check the Azure Pipeline UI, it will show the logged messages as shown below
Azure Pipeline – Logging – Task Commands:
Task commands can be used to define the task status, log an error or warning for task execution, show task completion percentage, set task variables, upload a file or update an endpoint at runtime. Below are all supported task commands available in Azure Pipeline.
- LogIssue: Log an error or warning message in the timeline record of the current task.
- SetProgress: Set progress and current operation for the current task.
- Complete: Finish the timeline record for the current task, set task result and current operation. When result not provided, set result to succeeded.
- SetVariable: Sets a variable in the variable service of taskcontext. The first task can set a variable, and following tasks are able to use the variable. The variable is exposed to the following tasks as an environment variable.
- SetEndpoint: Set a service connection field with given value. Value updated will be retained in the endpoint for the subsequent tasks that execute within the same job.
- AddAttachment: Upload and attach attachment to current timeline record. These files are not available for download with logs. These can only be referred to by extensions using the type or name values.
- UploadSummary: Upload and attach summary markdown to current timeline record. This summary shall be added to the build/release summary and not available for download with logs.
- UploadFile: Upload user interested file as additional log information to the current timeline record. The file shall be available for download along with task logs.
Azure Pipeline – Logging – Task Commands Examples:
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
## -------- ## LogIssue ## -------- ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - script: | echo "##vso[task.logissue type=error]Some Errors" echo "##vso[task.logissue type=warning]Some Warnings" displayName: logissue EOF ## Push the changes to Azure Repo git add . git commit -m "update2" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## ----------- ## SetProgress ## ----------- ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - script: | echo "Begin a lengthy process..." for i in {0..100..1} do sleep 1 echo "##vso[task.setprogress value=$i;]Sample Progress Indicator" done echo "Lengthy process is complete." displayName: SetProgress EOF ## Push the changes to Azure Repo git add . git commit -m "update3" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## -------- ## Complete ## -------- ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - script: echo "##vso[task.complete result=Succeeded;]DONE" displayName: Complete_Succeeded - script: echo "##vso[task.complete result=SucceededWithIssues;]DONE" displayName: Complete_SucceededWithIssues - script: echo "##vso[task.complete result=Failed;]DONE" displayName: Complete_Failed EOF ## Push the changes to Azure Repo git add . git commit -m "update4" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## ----------- ## SetVariable ## ----------- ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - bash: | echo "##vso[task.setvariable variable=var1;]aws" echo "##vso[task.setvariable variable=var2;issecret=true]azure" echo "##vso[task.setvariable variable=var3;isoutput=true]gcp" echo "##vso[task.setvariable variable=var4;isreadonly=true]tencent" name: SetVars - bash: | echo "var1 = $VAR1" echo "var1 = $(var1)" echo "var2 = $(var2)" echo "var3 = $(SetVars.var3)" echo "var4 = $VAR4" name: GetVars EOF ## Push the changes to Azure Repo git add . git commit -m "update5" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push ## ---------- ## UploadFile ## ---------- ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - bash: | echo "Hello World" > $(System.DefaultWorkingDirectory)/hello.txt cat $(System.DefaultWorkingDirectory)/hello.txt echo "##vso[task.uploadfile]$(System.DefaultWorkingDirectory)/hello.txt" name: UploadFile EOF ## Push the changes to Azure Repo git add . git commit -m "update6" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Note: Apart from task commands, Azure pipeline also have artifact, build and release commands. You can get more details on this link.
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