Azure Pipeline Part 4 – Script Bash pwsh PowerShell
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss script, bash, pwsh, and PowerShell in Azure Pipelines. Script, bash, pwsh or PowerShell block allows you to execute a command or script inside a step in Azure Pipelines.
Azure Pipeline Part 4 – Script Bash pwsh PowerShell
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 – Script:
Using script block inside a step, you can execute a command or script in cmd.exe on Windows or Bash on Linux or Mac.
Azure Pipeline – Script syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## Syntax: steps: - script: string # contents of the script to run displayName: string # friendly name displayed in the UI name: string # identifier for this step (A-Z, a-z, 0-9, and underscore) workingDirectory: string # initial working directory for the step failOnStderr: boolean # if the script writes to stderr, should that be treated as the step failing? 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` timeoutInMinutes: number env: { string: string } # list of environment variables to add |
Azure Pipeline – Script 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 |
## Create two demo scripts file ## Gets CPU, memeory and disk usage or agents cat <<'EOF'> myscript1.sh #!/bin/bash free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }' df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}' top -bn1 | grep load | awk '{printf "CPU Usage: %.2f\n", $(NF-2)}' EOF ## Script to demonstrate env variable cat <<'EOF'> myscript2.sh #!/bin/bash echo "Hello everyone" echo "This is $NAME from $SITE!" EOF ## Create the pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - script: source ./myscript1.sh displayName: system_monitor name: sys_mon continueOnError: true timeoutInMinutes: 5 - script: source ./myscript2.sh displayName: Hello name: hello continueOnError: true timeoutInMinutes: 5 env: NAME: Debjeet SITE: CloudAffaire EOF ## Push the changes to Azure Repo git add . git commit -m "update1" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – Bash:
Using bash block inside a step, you can execute a command or script in Bash shell on Windows, Linux or Mac.
Azure Pipeline – Bash syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## Syntax: steps: - bash: string # contents of the script to run displayName: string # friendly name displayed in the UI name: string # identifier for this step (A-Z, a-z, 0-9, and underscore) workingDirectory: string # initial working directory for the step failOnStderr: boolean # if the script writes to stderr, should that be treated as the step failing? 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` timeoutInMinutes: number env: { string: string } # list of environment variables to add |
Azure Pipeline – Bash 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 |
## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool steps: - bash: source ./myscript1.sh displayName: system_monitor name: sys_mon continueOnError: true timeoutInMinutes: 5 - bash: | echo this is a multi echo line command displayName: multiline name: multiline - bash: source ./myscript2.sh displayName: env_variables name: env_variables env: NAME: Debjeet SITE: CloudAffaire EOF ## Push the changes to Azure Repo git add . git commit -m "update2" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – pwsh:
Using pwsh block inside a step, you can execute a command or script in PowerShell Core on Windows, Linux or Mac.
Azure Pipeline – pwsh syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## Syntax: steps: - pwsh: string # contents of the script to run displayName: string # friendly name displayed in the UI name: string # identifier for this step (A-Z, a-z, 0-9, and underscore) errorActionPreference: enum # see the following "Output stream action preferences" topic warningPreference: enum # see the following "Output stream action preferences" topic informationPreference: enum # see the following "Output stream action preferences" topic verbosePreference: enum # see the following "Output stream action preferences" topic debugPreference: enum # see the following "Output stream action preferences" topic ignoreLASTEXITCODE: boolean # see the following "Ignore last exit code" topic failOnStderr: boolean # if the script writes to stderr, should that be treated as the step failing? workingDirectory: string # initial working directory for the step 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' timeoutInMinutes: number env: { string: string } # list of environment variables to add |
Azure Pipeline – pwsh 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 35 |
## Create a powershell script cat <<'EOF' > myscript.ps1 $cpu = Get-CimInstance -ClassName win32_perfformatteddata_perfos_processor ` | ? {$_.name -eq "_total"} | select -ExpandProperty PercentProcessorTime -ea silentlycontinue Write-host "CPU Usage: " $cpu $ComputerMemory = Get-CimInstance -ClassName win32_operatingsystem -ErrorAction Stop $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100) ` / $ComputerMemory.TotalVisibleMemorySize) $RoundMemory = [math]::Round($Memory, 2) Write-host "Memory Usage: " $RoundMemory Write-host "Disk Usage: " Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | select Name, FileSystem,FreeSpace,BlockSize,Size ` | % {$_.BlockSize=(($_.FreeSpace)/($_.Size))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Size=($_.Size/1GB);$_} ` | Format-Table Name,@{n='Free (GB)';e={'{0:N2}'-f $_.FreeSpace}}, @{n='Free (%)';e={'{0:N2}'-f $_.BlockSize}} ` ,@{n='Capacity (GB)';e={'{0:N3}' -f $_.Size}} -AutoSize EOF ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: vmImage: 'windows-latest' steps: - pwsh: .\myscript.ps1 displayName: system_monitor name: sysmon continueOnError: true timeoutInMinutes: 5 EOF ## Push the changes to Azure Repo git add . git commit -m "update3" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – PowerShell:
Using PowerShell block inside a step, you can execute a command or script in Windows PowerShell.
Azure Pipeline – PowerShell syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
steps: - powershell: string # contents of the script to run displayName: string # friendly name displayed in the UI name: string # identifier for this step (A-Z, a-z, 0-9, and underscore) errorActionPreference: enum # see the following "Output stream action preferences" topic warningPreference: enum # see the following "Output stream action preferences" topic informationPreference: enum # see the following "Output stream action preferences" topic verbosePreference: enum # see the following "Output stream action preferences" topic debugPreference: enum # see the following "Output stream action preferences" topic ignoreLASTEXITCODE: boolean # see the following "Ignore last exit code" topic failOnStderr: boolean # if the script writes to stderr, should that be treated as the step failing? workingDirectory: string # initial working directory for the step 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' timeoutInMinutes: number env: { string: string } # list of environment variables to add |
Azure Pipeline – PowerShell example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: vmImage: 'windows-latest' steps: - powershell: .\myscript.ps1 displayName: system_monitor name: sysmon continueOnError: true timeoutInMinutes: 5 EOF ## Push the changes to Azure Repo git add . git commit -m "update4" 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 15 |
## -------- ## 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