I am building Azure Pipelines for deployments of intranet, dotnet core projects from our Windows Self hosted agent.
I have a request from the team where if a PowerShell script initialize a variable to some value, the next step should include a specific environment.
The goal is, for some project, we compare a file from the agent to the DEV server and if different we request an approval.
I'm trying to make this work with global variables and get confused with different syntax.
Here's what I have:
YAML
variables:
- name: MYVAR
value: '0'
stages:
- stage: Init
jobs:
- job: DisplayBefore
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo Varname before :
echo $(MYVAR)
===> Displays: 0
- stage: RunScript
dependsOn: Init
jobs:
- job: CallScript
steps:
- checkout: none
- task: PowerShell@2
name: SetVarname
inputs:
targetType: filePath
filePath: '{agent}\compareFileContent.ps1'
- job: DisplayInSameStage
dependsOn: CallScript
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo Varname after :
echo ${{ variables.MYVAR }}
===> Displays: 0
echo $(MYVAR)
===> Displays: 0
- stage: DeploymentStage
dependsOn: RunScript
condition: eq(variables.MYVAR, '1')
jobs:
- deployment: DeploymentJob
environment: 'ATest'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo Varname after :
echo ${{ variables.MYVAR }}
{agent}\compareFileContent.ps1
[CmdletBinding()]
param ()
echo $env:MYVAR
===> Displays: 0
$env:MYVAR = 2
echo $env:MYVAR
===> Displays: 2
echo "##vso[task.setvariable variable=env:MYVAR;]3"
exit
My plan is to have another stage for the opposite condition, maybe you would see a better approach or guidance on what I am missing ? Thanks !!!
*********** Update 1
I used environment variables as I understood it was the way to access them from script, if there is another way, I'd be glad to know !
I used Stages because I need to use an Environment and they seem to need their own stage and deployment block otherwise the environment is tested before the condition is evaluated.
Here is my latest test:
variables:
- name: MYVAR
value: '0'
stages:
- stage: SingleStage
jobs:
- job: DisplayBefore
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo $(MYVAR)
- job: CallScript
dependsOn: DisplayBefore
steps:
- checkout: none
- task: PowerShell@2
name: SetVarname
inputs:
targetType: filePath
filePath: '..\compareFileContent.ps1'
- deployment: DeploymentJob
dependsOn: DisplayInSameStage
condition: eq(variables.MYVAR, '1')
environment: 'ATest'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- task: CmdLine@2
inputs:
script: |
echo EnvName Finally: $(EnvName)
This YAML doesn't do much as it request the approval from the environment at the start.
My scenario ask that this approval is triggered only based on a value calculated in the PowerShell script.
Any ideas ? Thanks.
Jobs and stages usually run on different agents so you cannot expect environmental variables from different jobs to keep their values.
If you want to work with globals, you can have steps within one job. Otherwise, to keep some logical job/stage structure, you can just write and read values to/from an external source. This depends on your scenario.