0

My azure-pipeline.yml sets a special magical variable called name that causes the pipeline name to be set.

I want to use this value inside a PowerShell script to put this value into a text file as a setting.

variables:
  System.Debug: true
  versionNumber: '0.0.0' 
  namePrefix: 'yml_$(versionNumber).'
  buildConfiguration: 'Release'
  tag: '-yml-$(versionNumber)'
  publishBuildArtifactsSubfolder: 'yml\$(Build.Repository.Name)_yml_$(versionNumber).'

name: ${{ variables['namePrefix']}}$(Build.BuildId)

steps:
# Find and replace in files. PowerShell because bash isn't available.
- task: PowerShell@2
  displayName: Set version number in environment.*.ts
  inputs:
    targetType: 'inline'
    script: |
        Write-Host "Build.DefinitionName is $(Build.DefinitionName)"
        Write-Host "env-name is $($env:name)"
        Write-Host "name is $($name)"

#Get-ChildItem 'environment.*.ts' -Recurse | ForEach {
#  (Get-Content $_ | ForEach  { $_ -replace '[[name]]', '${Build.DefinitionName}' }) | Set-Content $_
#}

Is it possible to get the value of name inside the PowerShell script? How? (Is this documented anywhere?)

Workaround

It appears to be impossible to get the special name thing, therefore set a variable with the same value.

variables:
  System.Debug: true
  versionNumber: '0.0.0' 
  namePrefix: 'yml_$(versionNumber).'
  buildConfiguration: 'Release'
  tag: '-yml-$(versionNumber)'
  publishBuildArtifactsSubfolder: 'yml\$(Build.Repository.Name)_yml_$(versionNumber).'
  name: ${{ variables['namePrefix']}}$(Build.BuildId)

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
        Write-Host "name is $($name)"
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Richard Barraclough
  • 2,625
  • 3
  • 36
  • 54

1 Answers1

0

Yes, the name it's a pre-defined variable Build.BuildNumber, so just use $(Build.BuildNumber).

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114