Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aws-scripting-guy/a06dda4d88bb0eba78886b840de13e62 to your computer and use it in GitHub Desktop.
Save aws-scripting-guy/a06dda4d88bb0eba78886b840de13e62 to your computer and use it in GitHub Desktop.
Azure DevOps tips: Using predefined variables in the scripts

Azure DevOps Pipelines: How to access predefined variables from scripts

In this scenario we want to access predefined variable $Build.Reason from the PowerShell script. $Build.Reason will have different value based on what triggered the build.

Configure azure-pipelines.yml

Map $Build.Reason to env variable for the powershell script.

- powershell: |
    .\scripts\git-package.ps1
  env:
      BUILD_REASON: $(Build.Reason)
  displayName: "push updated package.json to remote git"

Use in the powershell script

Access $env:BUILD_REASON from the script.

# Write package.json back to github only for release builds

if ($env:BUILD_REASON -eq "Schedule" -or $env:BUILD_REASON -eq "Manual") {
  # write package.json back to repo 
  git push 
}
else {
  # do nothing    
  write-verbose "[info] this is not a scheduled build. Build reason is $env:BUILD_REASON "
}

Get list of build variables:

https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&viewFallbackFrom=vsts&tabs=yaml

Get list of release variables:

https://docs.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment