In my classic (release) pipeline, I see $(Build.BuildNumber)
resolves to the artifact name, that is the build name:
By resolve, I mean I have a PS script where I refer $(Build.BuildNumber)
and it takes the build name as specified in the actual build pipeline, which also is populated in the Release. In the screenshot, it is the highlighted one - "Hotfix_SOL4378246-6.1". But when I do the same in the YAML, it doesn't accept it:
This is the section where I declare the resources (it's the same as the Classic)
resources:
pipelines:
- pipeline: _HotfixBuild
source: DeepDove-Hotfix
repositories:
- repository: _HotfixRepo
type: git
name: Hotfix
# ref: master
ref: feature/convertClassicReleasePipelinetoYAML
But the PS script doesn't accept either $(Build.BuildNumber)
, or _HotfixRepo
Essentially, in classic pipeline, this works (in the PS task):
$buildName="$(Build.BuildNumber)"
$hotfixName=$buildName.Split(".")[0]
But not in YAML.
The error (when _HotfixRepo is used) is "hotfixName : The term 'hotfixName' is not recognized as the name of a cmdlet, function, script file, or operable ......", which is the standards PS error, and when "$(Build.BuildNumber)" is used, I get "You cannot call a method on a null-valued expression.", which comes further down the script.
How do I get to call it?
In a classic release pipeline, you can use the variable Build.BuildNumber
to get the build/run number of the Primary Artifact which is a Build type source artifact. See "Primary Artifact variables".
In a YAML pipeline, the variable Build.BuildNumber
is used to get the build/run number of current build/run, it can not get the build/run number of pipeline resources.
To get the build/run number of a pipeline resource in YAML pipeline, you should use the variable resources.pipeline.{alias}.runName
. Here the {alias}
is a custom alias you set when defining the pipeline resource (e.g., _HotfixBuild
in your above case).
resources:
pipelines:
- pipeline: _HotfixBuild # The alias of the pipeline resource.
source: DeepDove-Hotfix
For more details, you can see "Pipeline resource variables".
In the YAML pipeline:
If you set the task run inline
type script, you can directly call the variables using the expression $(variable-name)
.
resources:
pipelines:
- pipeline: _HotfixBuild
source: DeepDove-Hotfix
steps:
- task: PowerShell@2
displayName: 'Show build/run number'
inputs:
targetType: 'inline' # Run the inline type script.
script: |
Write-Host "The build/run number of the pipeline resource is $(resources.pipeline._HotfixBuild.runName)"
Write-Host "The build/run number of current run is $(Build.BuildNumber)"
If you set the task to call a script file, in the script file, you cannot directly use the expression $(variable-name)
. Instead, you need to call the corresponding environment variables of the variables. The normal variables (not secret variables) will be automatically mapped as environment variables on the agent every time to run the pipeline.
The script file (For example, test-var.ps1
)
# test-var.ps1
Write-Host "The build/run number of the pipeline resource is $env:RESOURCES_PIPELINE__HOTFIXBUILD_RUNNAME"
Write-Host "The build/run number of current run is $env:BUILD_BUILDNUMBER"
Call the script file in pipeline.
resources:
pipelines:
- pipeline: _HotfixBuild
source: DeepDove-Hotfix
steps:
- task: PowerShell@2
displayName: 'Show build/run number'
inputs:
targetType: filePath # Call the script file.
filePath: 'path/to/test-var.ps1'