Last active
March 6, 2024 12:19
-
-
Save JustinGrote/4ece827540da00d5ffce1d9eb593e56c to your computer and use it in GitHub Desktop.
Test Azure Devops Pipeline YAML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-AzDOPipeline { | |
<# | |
.SYNOPSIS | |
Tests an Azure Devops Pipeline YAML configuration | |
.DESCRIPTION | |
This can be used to validate an Azure Devops pipeline configuration within a particular pipeline project. | |
#> | |
param ( | |
#Your Azure Devops Organization Name | |
[Parameter(Mandatory)]$Organization, | |
#Your Azure Devops Project | |
[Parameter(Mandatory)]$Project, | |
#Your Azure Devops Project Pipeline ID | |
[Parameter(Mandatory)]$PipelineID, | |
#Optional Path to the YAML File you wish to validate. This will override the existing pipeline configuration. | |
[String]$Path, | |
#Your Azure Devops Personal Access Token as a secure string. If not supplied you will be prompted for it. More Info: https://tinyurl.com/AZDOPAT | |
[SecureString]$PersonalAccessToken | |
) | |
$ErrorActionPreference = 'Stop' | |
if (-not $PersonalAccessToken) {[SecureString]$PersonalAccessToken = Read-Host -Prompt 'Azure Devops Personal Access Token' -AsSecureString} | |
$PATBase64 = [System.Convert]::ToBase64String( | |
[System.Text.Encoding]::ASCII.GetBytes( | |
":$([Net.NetworkCredential]::new('PAT', $PersonalAccessToken).password)" | |
) | |
) | |
$InvokeRestParams = @{ | |
ContentType = 'application/json' | |
Uri = "https://dev.azure.com/$Organization/$Project/_apis/pipelines/$PipelineId/runs?api-version=5.1-preview" | |
Method = 'POST' | |
Headers = @{ | |
Authorization = "Basic $PATBase64" | |
} | |
Body = @{ | |
PreviewRun = $true | |
} | |
} | |
if ($Path) { | |
$InvokeRestParams.Body.YamlOverride = [string](Get-Content -raw $Path) | |
} | |
$InvokeRestParams.Body = $InvokeRestParams.Body | ConvertTo-Json | |
try { | |
$result = Invoke-RestMethod @InvokeRestParams -ErrorAction Stop | |
} catch { | |
if ($PSItem -match 'PipelineValidationException') { | |
Write-Error (($PSItem | ConvertFrom-Json).message -replace '/azure-pipelines.yml( ?: ?)? ?','') | |
return | |
} else { | |
throw | |
} | |
} | |
if ($Result -match 'Azure DevOps Services \| Sign In') { | |
Write-Error 'Authentication failed. Please check that your Personal Access Token is correct. | |
More Info: https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate' | |
return | |
} | |
write-host -ForegroundColor green 'Pipeline YAML is VALID. Expanded result:' | |
write-host -ForegroundColor green '----------------------------------------' | |
$result.finalYaml | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"label": "Validate Azure Devops Pipeline YAML", | |
"windows": { | |
"command": "pwsh.exe" | |
}, | |
"args": [ | |
"-NoProfile", | |
"-NonInteractive", | |
"-ExecutionPolicy", | |
"Bypass", | |
"-Command", | |
"iwr https://gist.githubusercontent.com/JustinGrote/4ece827540da00d5ffce1d9eb593e56c/raw/04fba12299de888154b3f1eb526654b3c24fbc5a/Test-AzDOPipeline.ps1 | iex; Test-AzDOPipeline -Path ${file} -Organization justingrote -project github -pipelineid 1 -Credential (get-secret 'AzureDevopsPAT')" | |
], | |
"problemMatcher": "$pester" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment