AZDO YAML - klagan/learning GitHub Wiki
Building YAML files is fraught with issues in basic syntax. We can mitigate a lot of the back and forth in testing the pipeline by validating the syntax in a what if scenario.
We shall assume the following .yaml file as a pipeline:
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'To check the syntax we need to POST THE .YAML to the endpoint POST dev.azure.com/<org>/<project>/_apis/pipelines/<pipelineId>/runs?api-version=5.1-preview.
Donovan Brown wrote a powershell library - VSTeam - that helps simplify a lot of Azure DevOps interactions. One of the modules simplifies the .yaml syntax validation.
#======================================================================================
# Create a PAT in AZDO
#======================================================================================
$accessToken = $(Get-AzAccessToken).Token | ConvertTo-SecureString -AsPlainText -Force
$Params = @{
Uri = "https://vssps.dev.azure.com/ikitsolutions/_apis/tokens/pats?api-version=6.1-preview.1"
Authentication = "Bearer"
Token = $accessToken
Method = 'POST'
ContentType = 'application/json'
Body = '{
"displayName": "Temporary automated YAML preview",
"scope": "vso.build_execute vso.release_manage",
"validTo": "2022-12-01T23:46:23.319Z",
"allOrgs": false
}'
}
$patResult = Invoke-RestMethod @Params
$pat = $patResult.patToken.token
Write-Output $pat | az devops login
#======================================================================================
# Validate YAML pipeline
#======================================================================================
# install the PowerShell library
Import-Module -Name VSTeam -RequiredVersion 6.4.6
# set the environment
# https://methodsandpractices.github.io/vsteam-docs/docs/modules/vsteam/commands/Set-VSTeamAccount/
# <account> is found in devops URL: https://dev.azure.com/laganslabs-it is account: laganlabs-it
Set-VSTeamAccount -Account ikitsolutions -SecurePersonalAccessToken $(ConvertTo-SecureString $pat -AsPlainText -Force)
$pipelineName='preview-pipelines'
$pipelineId=$(az pipelines list --query "[?name=='$pipelineName'].{Id:id}" -o tsv)
# run validation on pipeline $pipelineId for project MyProject with YAML azure-pipelines.yml
Test-VSTeamYamlPipeline -PipelineId $pipelineId -FilePath .\azure-pipelines.yml -ProjectName Samples
#======================================================================================
# Delete the PAT from AZDO
#======================================================================================
$Params = @{
Uri = "https://vssps.dev.azure.com/ikitsolutions/_apis/tokens/pats?authorizationId={0}&api-version=6.1-preview.1" -f $patResult.patToken.authorizationId
Authentication = "Bearer"
Token = $accessToken
Method = 'DELETE'
}
Invoke-RestMethod @ParamsThis AZDO task will update the title of the build and the version number. It is an example of being in control of the version format.
$offsetis acountervariable defined outside the snippet
- task: PowerShell@2
displayName: Set the name of the build (i.e. the Build.BuildNumber)
inputs:
targetType: 'inline'
script: |
[int] $buildIdWithOffset = ([int] $(Build.BuildId)) + ([int] $(offset))
[string] $buildName = "$(major).$(minor).$buildIdWithOffset"
Write-Host "Setting the name of the build to '$buildName'."
Write-Host "##vso[build.updatebuildnumber]$buildName"