Reputation: 43
I am trying to generate a random string of length 32 characters in Build Stage, then I want to pass the same to the next job/deployment And Also to the next Stage.
I referred to https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=powershell to pass a variable from one job to another job and stage. But it is not working, Here is my pipeline code YAML file.
trigger: none
pool:
name: agentpool-myproj
stages:
- stage: 'BuildStage'
variables:
- group: myproj-vargrp-common-dp-poc
displayName: 'BuildStage'
jobs:
- deployment: BuildStageSecretProducerjobs # deployment is equal to job
environment: dev
displayName: 'BuildStageSecretProducerjobs'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- download: none
- task: AzurePowerShell@5
name: mastertask
inputs:
azureSubscription: 'con-myproj-dev'
ScriptType: 'InlineScript'
Inline: |
$Random32Key = -join (((48..57)+(65..90)+(97..122)) |Get-Random -Count 32 |%{[char]$_})
Write-Host "##vso[task.setvariable variable=supersecret;isoutput=true;issecret=false]$Random32Key"
FailOnStandardError: true
azurePowerShellVersion: LatestVersion
- deployment: BuildStageSecretConsumerjobs
dependsOn: BuildStageSecretProducerjobs
variables:
thissupersecret: $[ dependencies.BuildStageSecretProducerjobs.outputs['masterjob.supersecret'] ]
environment: dev
displayName: 'BuildStageSecretConsumer'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- download: none
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host $(thissupersecret)
- stage: 'DeployStage'
dependsOn: 'BuildStage'
condition: succeeded('BuildStage')
displayName: 'DeployStage'
jobs:
- deployment: DeployStageSecretconsumerjobs # deployment is equal to job
variables:
thissupersecret: $[stageDependencies.BuildStage.BuildStageSecretProducerjobs.outputs['mastertask.supersecret']]
environment: dev
displayName: 'DeployStageSecretconsumerjobs'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- download: none
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host $(thissupersecret)
Upvotes: 0
Views: 654
Reputation: 321
$releaseurl = ('{0}{1}/_apis/release/releases/{2}?api-version=5.0' -f $($env:SYSTEM_TEAMFOUNDATIONSERVERURI), $($env:SYSTEM_TEAMPROJECTID), $($env:RELEASE_RELEASEID) )
$Release = Invoke-RestMethod -Uri $releaseurl -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$variableName = '<HERE_VARIABLE_NAME>'
$release.variables.($variableName).value = "<YOUR VALUE HERE IN STRING OR REFERNCE TO A VARIABLE E.G. $(OTHER_VARIABLE)>"
$json = @($release) | ConvertTo-Json -Depth 99
Invoke-RestMethod -Uri $releaseurl -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$Release = Invoke-RestMethod -Uri $releaseurl -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
Above is 100% working for Releases in ADO. Try to dig for URLs referencing Builds and you should be fine. Of course is you opt to Powershell.
Upvotes: 0