Azure Pipeline Part 6 – Task
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss smallest unit of execution in an Azure pipeline, i.e., tasks. You can imagine task as pre-configured script that you can reuse any numbers of times in your Azure pipeline. Microsoft Azure provides a set of built-in tasks to perform most common types of activity through your pipeline, or you can create a custom task of your own. Task also supports versioning and you can call a specific version of task in your Azure pipeline.
Next, we are going to create an Azure pipeline that uses some built-in tasks. We will first create some files and directories, then archive those files and directories, then extract the archived files and directories and finally list the files using multiple built-in tasks.
Azure Pipeline Part 6 – Task
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 – Task:
Task are pre-configured scripts that you can use in your Azure pipelines to achieve different tasks.
Azure Pipeline – Task syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## Syntax: steps: - task: string # reference to a task and version, e.g. "VSBuild@1" displayName: string # friendly name displayed in the UI name: string # identifier for this step (A-Z, a-z, 0-9, and underscore) condition: string continueOnError: boolean # 'true' if future steps should run even if this step fails; defaults to 'false' enabled: boolean # whether to run this step; defaults to 'true' target: container: string # where this step will run; values are the container name or the word 'host' commands: enum # whether to process all logging commands from this step; values are `any` (default) or `restricted` settableVariables: string # what variables are allowed; defaults to all; can be `none` or a list of allowed vars timeoutInMinutes: number inputs: { string: string } # task-specific inputs env: { string: string } # list of environment variables to add |
Azure Pipeline – Task 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 30 31 32 33 34 |
## Create pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - task: Bash@3 inputs: targetType: 'inline' script: | mkdir mydir1 echo hello world > mydir1/hello.txt displayName: create_file - task: ArchiveFiles@2 inputs: rootFolderOrFile: mydir1 includeRootFolder: true archiveType: 'zip' tarCompression: 'gz' archiveFile: 'mydir2.zip' displayName: archive_file - task: ExtractFiles@1 inputs: destinationFolder: mydir2 displayName: extract_file - task: Bash@3 inputs: targetType: 'inline' script: ls -l mydir2 displayName: list_file EOF ## Push the changes to Azure Repo git add . git commit -m "update" 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