Skip to content

Commit c9a4db1

Browse files
committed
new post
1 parent 4eb81e3 commit c9a4db1

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
![Header for this post, reads 'Handing off variables in ADO'](\assets\images\2024\assembly_line.gif)
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!

assets/images/2024/assembly_line.gif

1.82 MB
Loading

new-blogPost.ps1

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$base = @"
1+
$global:base = @"
22
---
33
title: "PostTitle"
44
date: "calculatedPostDate"
@@ -24,6 +24,13 @@ calculatedPostExcerpt
2424
"@
2525

2626
function New-BlogPost{
27+
<#
28+
.EXAMPLES
29+
New-BlogPost -postDate $targetDate `
30+
-postTitle "Got messy Ifs Guard Clauses to the Rescue" `
31+
-postExcerpt "Revisiting PowerShell after mostly writing nothing but c# for years, I'm finding lots of useful programming practices can make my code easier to read. In this post, we'll talk about guard clauses and how they can make your code easier to read!" `
32+
-postTags ham,onionions,cheese,foxes | clip
33+
#>
2734
param($postTitle,$postDate, $postExcerpt,$postTags)
2835
$postContent = $base
2936
$calculatedPostTitle= $postTitle -replace " ","-"
@@ -40,11 +47,11 @@ param($postTitle,$postDate, $postExcerpt,$postTags)
4047

4148
$postContent -replace "postTitle", $postTitle
4249
"would be created as $calculatedFileName.md"
50+
51+
$postContent
52+
remove-item $calculatedFileName
53+
new-item -Name $calculatedFileName -Value $postContent -ItemType File -force
4354
}
4455

4556
$targetDate = get-date ([DateTime]::Now.AddDays(-1)) -UFormat %Y-%m-%d
4657

47-
New-BlogPost -postDate $targetDate `
48-
-postTitle "Got messy Ifs Guard Clauses to the Rescue" `
49-
-postExcerpt "Revisiting PowerShell after mostly writing nothing but c# for years, I'm finding lots of useful programming practices can make my code easier to read. In this post, we'll talk about guard clauses and how they can make your code easier to read!" `
50-
-postTags ham,onionions,cheese,foxes | clip

0 commit comments

Comments
 (0)