Jim
Jim

Reputation: 899

How to use AzureDevOps predefined variables in Bash/Powershell scripts

In an AzureDevOps pipeline, I have tasks written in Bash/Powershell script.
If I choose to use Inline scrpit, I can use predefined variables directly, such as

cd $(Build.SourcesDirectory)

However, if I choose to use a file path to call a script, I can't use predefined variable directly in the script file. I have to pass the predefined variable to an environment variable in the task definition, like in the example below, so I can use $SourceDirectoy in script.sh,
enter image description here

Is there a better way to call predefined variable direclty in the script?

Upvotes: 8

Views: 7127

Answers (1)

Matt
Matt

Reputation: 4065

I believe the variables are also made available to scripts, but the formatting to reference them in the script might depend on script type. Reference the documentation.

Notice that variables are also made available to scripts through environment variables. The syntax for using these environment variables depends on the scripting language.

The name is upper-cased, and the . is replaced with the _. This is automatically inserted into the process environment. Here are some examples:

  • Batch script: %VARIABLE_NAME%
  • PowerShell script: $env:VARIABLE_NAME
  • Bash script: $VARIABLE_NAME

Predefined variables that contain file paths are translated to the appropriate styling (Windows style C:\foo\ versus Unix style /foo/) based on agent host type and shell type. If you are running bash script tasks on Windows, you should use the environment variable method for accessing these variables rather than the pipeline variable method to ensure you have the correct file path styling.

Upvotes: 11

Related Questions