= Cheatsheet Azure DevOps =
**Summary**: Azure DevOps hints, tips, oneliners and best practices. \\
**Date**: 8 December 2024 \\
{{tag>cheatsheet azureDevops}}
== Output in Azure DevOps ==
> Logging output
# Logging output in azure devops
##[group]Beginning of a group - white
##[warning]Warning message - orange
##[error]Error message - red
##[section]Start of a section - green
##[debug]Debug text - purple
##[command]Command-line being run - blue
##[endgroup] - green
Write-Host "`n##[section]Function: $($MyInvocation.MyCommand).`n"
\\
> Log error or warning
Write-Host "##vso[task.logissue type=error]Something went very wrong."
Write-Host "##vso[task.logissue type=warning]Found something that could be a problem."
\\
> Generate extra output if debug mode is set
if ($env:SYSTEM_DEBUG -eq "True"){$headers | Out-Host}
== Variables in Azure DevOps ==
> Pipeline variable in yaml - without PowerShell
variables:
- name: BuildConfiguration
value: 'Release'
- task: DotNetCoreCLI@2
displayName: "Build Solutions"
inputs:
command: 'build'
projects: '**/*.sln'
arguments: '--no-restore --configuration $(BuildConfiguration)'
\\
> Pipeline parameter in yaml
if ( "${{ parameters.alllogs }}" -eq "True" ){write-Host "Parameter from pipeline"}
> System variable in yaml
> Variables can be used like system environment variables
if (Test-Path "$env:Build_SourcesDirectory\$env:Build_Repository_Name\appsettings.Development.json"){Write-Host "System / Environment Variable"}
$sites = get-childitem -path env:* | Where-Object {$_.name -like "*-SiteName"} | Select-Object -ExpandProperty value # Added through Variable Group
> Variables can also be used like pipeline variables
get-childitem $(Pipeline.Workspace)
$dbserver = "$(serverDb)" + "." + "$(dnsZone)" # from Azure App Configuration
\\
> Variables in PS scriptfile
$adserver = $env:adServer + "." + $env:domain # from Azure App Configuration
$projects = Get-ChildItem -Path "$env:Build_SourcesDirectory\$env:Build_Repository_Name\deploy\" -Directory | Select-Object Name
== Starter Azure Pipeline ==
name: $(Build.DefinitionName)-$(Build.BuildId)
appendCommitMessageToRunName: false
variables:
buildConfiguration: 'Release'
deploymentSuffix: '$(Build.DefinitionName)-$(Build.BuildId).$(System.StageAttempt).$(System.JobAttempt)'
parameters:
- name: alllogs
displayName: "Enable all logging"
type: boolean
default: true
# https://learn.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers?view=azure-devops&tabs=yaml
schedules:
- cron: "05 23 * * *" # 23.05h
displayName: Nightly build on master
branches:
include:
- master
always: true
pool:
name: ReleasePool # Self hosted
vmImage: ubuntu-latest # MS Hosted
vmImage: windows-latest # MS Hosted
trigger:
branches:
include:
- release/*
- master
trigger: none
resources:
repositories:
- repository: templates
type: git
name: DevOps/templates
- repository: self
pipelines:
- pipeline: TA
source: TestAutomation # Pipeline name that produces the artifact (must be unique)
stages:
- stage: build
displayName: "Stage: Build"
pool:
name: BuildPoolDotNet
jobs:
- job: build
displayName: "Job: Build & Test"
steps:
- checkout: self
clean: true # If true, run git clean -ffdx followed by git reset --hard HEAD before fetching.
- checkout: templatses
clean: true
- ${{ if not(eq(variables['Build.Reason'], 'PullRequest')) }}:
- template: /pipelines/log-information.yml@operations
parameters:
environment: "environment: build"
alllogs: ${{ parameters.alllogs }}
# Stage with template that has steps (see deploy.yml)
- stage: release
displayName: "Stage: release"
condition: and(succeeded(), ne(variables['Build.SourceBranchName'], 'master'))
variables:
environment: 'tst'
jobs:
- deployment: Deploy
workspace:
clean: all
environment: ${{ variables.environment }}
- template: /pipelines/deploy.yml@templates
parameters:
environment: ${{ variables.environment }}
alllogs: ${{ parameters.alllogs }}
# Stage with template as step (see prod.yml)
- stage: production
displayName: "Stage: prod"
dependsOn: release
variables:
tag: 'Production'
jobs:
- job: prd
displayName: "production"
condition: and(succeeded(), eq( ${{ parameters.deployProduction }}, true))
variables:
environment: 'prd'
steps:
- template: /deploy/production.yml
parameters:
environment: ${{ variables.environment }}
alllogs: ${{ parameters.alllogs }}
=== Template deploy.yml ===
parameters:
environment: ""
alllogs: ""
dependsOn1: []
dependsOn2: []
dependsOn3: []
dependsOn4: []
jobs:
- job: deploy
displayName: "Deploy steps"
dependsOn:
- ${{ parameters.dependsOn1 }}
- ${{ parameters.dependsOn2 }}
- ${{ parameters.dependsOn3 }}
- ${{ parameters.dependsOn4 }}
workspace:
clean: all
steps:
- checkout: operations
clean: true # If true, run git clean -ffdx followed by git reset --hard HEAD before fetching.
#- ${{ if eq(parameters.alllogs, true) }}:
- template: /pipelines/log-information.yml@operations
parameters:
environment: ${{ parameters.environment }}
alllogs: ${{ parameters.alllogs }}
input1: "adGroups: ${{ parameters.adGroups }}"
- task: PowerShell@2
displayName: "PowerShell task"
inputs:
pwsh: true
targetType: 'inline'
script: |
Write-Host "`n##[section]PowerShell task `n"
=== Template prod.yml - Most Common Tasks ===
parameters:
environment: ""
alllogs: ""
spokeEntity: ""
steps:
- task: PowerShell@2
displayName: "Powershell Core task"
inputs:
pwsh: true
targetType: 'inline'
script: |
Write-Host "`n##[section]Start PS Core task`n"
Get-ChildItem -path env:* | Sort Name
- task: Bash@3
displayName: "Bash task"
inputs:
targetType: 'inline'
failOnStderr: true
script: |
echo '##[Section]Start Bash task'
printenv | sort
- task: AzureCLI@2
displayName: "Azure CLI task"
condition: and(succeeded(), or(eq(variables.runAzureTask, true), eq(variables.runAzureAll, true)))
inputs:
azureSubscription: $(azureServiceConnection)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
set -e
echo '##[Section]Start Azure CLI task'
az account set --subscription $(subId)
- task: AzurePowerShell@5
displayName: "Azure Powershell task"
inputs:
azureSubscription: $(azureServiceConnection)
azurePowerShellVersion: 'LatestVersion'
pwsh: true
ScriptType: 'InlineScript'
Inline: |
if ( "${{ parameters.alllogs }}" -eq "True" ){Write-Host "`n##[section]Start Azure Powershell task`n"}
# Set Azure Context for environment subscription
Set-AzContext -Name ${{ parameters.environment }} -Subscription $env:azureSubscriptionName