The only way to know what a YAML pipeline really turns into is usually to queue it. The Preview API does a dry run instead: Azure DevOps compiles the YAML exactly as it would for a real run, and gives us back the final YAML. Nothing is queued, so nothing shows up under the pipeline's runs.
The pipeline
trigger: none parameters: - name: environment type: string default: dev values: - dev - qa pool: vmImage: ubuntu-latest steps: - script: echo Deploying to ${{ parameters.environment }} displayName: Deploy
Calling the Preview API
POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/preview?api-version=7.1
$azureDevOpsResourceId = "499b84ac-1321-427f-aa17-267ca6975798" $organization = "<Organization>" $project = "<Project>" $pipelineId = "<PipelineId>" $token = az account get-access-token ` --resource $azureDevOpsResourceId ` --query accessToken ` --output tsv $uri = "https://dev.azure.com/$organization/$project/_apis/pipelines/$pipelineId/preview?api-version=7.1" $body = @{ previewRun = $true templateParameters = @{ environment = "qa" } } | ConvertTo-Json $response = Invoke-RestMethod ` -Method Post ` -Uri $uri ` -Headers @{ Authorization = "Bearer $token" } ` -ContentType "application/json" ` -Body $body $response.finalYaml
trigger: enabled: false parameters: - name: environment type: string default: dev values: - dev - qa stages: - stage: __default jobs: - job: Job pool: vmImage: ubuntu-latest steps: - task: CmdLine@2 displayName: Deploy inputs: script: echo Deploying to qa
The expression is resolved to qa, and we can see what Azure DevOps does behind the scenes: the steps are wrapped in a default stage and job, the pool is moved down to the job, and script becomes the CmdLine@2 task.
{
"message": "The 'environment' parameter value 'prod' is not a valid value.",
"typeKey": "PipelineValidationException"
// Omitted for brevity
}
After all of these calls, the pipeline still has no runs.
There are two more properties worth knowing. yamlOverride lets us send YAML that isn't committed yet and preview that instead. And resources.repositories.self.refName (for example refs/heads/feature/my-change) previews another branch, which is handy for diffing the finalYaml of a branch against main before merging.
Keep in mind this only covers what's known at compile time. It won't tell you whether a service connection is authorized, an agent is available, or a runtime variable resolves.
Hope this helps.
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment