|
| 1 | +--- |
| 2 | +title: "Passing variables from PowerShell to Bash in Azure Devops" |
| 3 | +date: "2024-06-16" |
| 4 | +redirect_from : 2024/06/16/How-to-pass-variables-between-Azure-Devops-Bash-and-PowerShell-commands |
| 5 | +coverImage: \assets\images\2024\assembly_line.gif |
| 6 | +categories: |
| 7 | + - "scripting" |
| 8 | +tags: |
| 9 | + - "ado" |
| 10 | + - "pipelines" |
| 11 | + - "tasks" |
| 12 | + - "yaml" |
| 13 | +excerpt: "Have you ever needed to do complex automation in Azure Devops? Like retrieving a token for one service and handing it off to subsequent commands to use? Then you might have been puzzled about the correct syntax to use. In this post, I'll give you a working example of how the syntax should be used to hand variables between bash and PowerShell tasks in Azure Devops" |
| 14 | +fileName: '2024-06-16-how-to-pass-variables-between-azure-devops-bash-and-powershell-commands' |
| 15 | +--- |
| 16 | +Have you ever needed to do complex automation in Azure Devops? Like retrieving a token for one service and handing it off to subsequent commands to use? Then you might have been puzzled about the correct syntax to use. In this post, I'll give you a working example of how the syntax should be used to hand variables between bash and PowerShell tasks in Azure Devops |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +*Post Outline* |
| 21 | + |
| 22 | +* Is it even possible to do this? |
| 23 | +* What makes it hard? |
| 24 | +* Working example of passing variables from PowerShell to Bash Steps |
| 25 | +* Referencing variables from previous stages |
| 26 | + |
| 27 | +## Is it even possible to do this? |
| 28 | + |
| 29 | +For a while, I will admit, I despaired. The task seemed simple, use the `AzurePowerShell@4` task to retrieve an AzAccessToken using `Get-AzAccessToken`, and then hand that off to other bash commands for further consumption. |
| 30 | + |
| 31 | +But it took **dozens** of iterations to get this to work! |
| 32 | + |
| 33 | +## Why is this difficult? |
| 34 | + |
| 35 | +Well, I found the hardest part of this to be noting the differences in syntax between PowerShell and Bash, specifically that bash is very sensitive to spacing. |
| 36 | + |
| 37 | +```yaml |
| 38 | + - task: AzurePowerShell@5 |
| 39 | + name: GetAzToken |
| 40 | + displayName: "Retrieve AzToken to authenticate to Nuget" |
| 41 | + inputs: |
| 42 | + azureSubscription: "TheNameOfYourServiceConnection" |
| 43 | + ScriptType: 'InlineScript' |
| 44 | + Inline: | |
| 45 | + $accessToken = Get-AzAccessToken | Select -Expand Token |
| 46 | + Write-Host "Storing Access Token for subsequent usage, truncating for privacy $($accessToken.Substring(0,8))" |
| 47 | + |
| 48 | + # Set the output variable |
| 49 | + |
| 50 | + Write-Host "##vso[task.setvariable variable=MyAccessToken;isOutput=true]$accessToken" |
| 51 | + azurePowerShellVersion: 'LatestVersion' |
| 52 | + pwsh: true |
| 53 | + |
| 54 | +``` |
| 55 | + |
| 56 | +With this step done, you've produced an output variable which will be automatically available to follow-up tasks within this stage. Now, to retrieve it and use it in bash. |
| 57 | + |
| 58 | +```yaml |
| 59 | + - task: Bash@3 |
| 60 | + displayName: 'Configure Nuget file with PAT' |
| 61 | + inputs: |
| 62 | + targetType: 'inline' |
| 63 | + script: | |
| 64 | + locKey=$(GetAzToken.MyAccessToken) |
| 65 | +
|
| 66 | + echo "Using retrieved az Access token, truncated for privacy ${locKey:0:6}..." |
| 67 | +
|
| 68 | + sed -i -e "s/NUGET_PAT/$locKey/g" '$(Build.SourcesDirectory)/NuGet.Config' |
| 69 | + cat '$(Build.SourcesDirectory)/NuGet.Config' |
| 70 | +``` |
| 71 | +
|
| 72 | +The core piece to point out is that it's easy to reference variables. If you commit a variable to `Output` using the syntax used in PowerShell above, it will be available to subsequent tasks. |
| 73 | + |
| 74 | +Simply use `Name.Variable` syntax to reference the variable. Some have reported that you must use upper-case for this, but I have found this is not necessary. |
| 75 | + |
| 76 | +## Using a variable within a different stage |
| 77 | + |
| 78 | +If you've made a variable an output variable, you can then reference it in follow-up jobs like so: |
| 79 | + |
| 80 | +```yaml |
| 81 | + - job: Job2 |
| 82 | + dependsOn: |
| 83 | + - Setup_PreReq #must be the name of the previous job |
| 84 | + |
| 85 | + pool: |
| 86 | + type: linux |
| 87 | + variables: AzureResourceGroupName']] |
| 88 | + AzAccessToken: $[dependencies.Setup_AzPolicy.outputs['GetAzToken.MyAccessToken']] |
| 89 | +``` |
| 90 | + |
| 91 | +Now the variable will be available for even tasks in different jobs! |
0 commit comments