Reputation: 31
I am trying to use Azure DevOps to configure a docker swarm cluster, with an environment containing 3 RHEL7 vm's with Docker installed and one of them being configured as the swarm host.
Here is the yaml file for the pipeline:
jobs:
- deployment: MasterNode
strategy:
runOnce:
deploy:
steps:
- bash: |
SWARMTOKEN=$(docker swarm join-token manager -q)
echo "##vso[task.setvariable variable=tokenswarm;isOutput=true]$SWARMTOKEN"
name: setvar
- script: echo $(setvar.tokenswarm)
name: echovar
environment:
name: swarm
resourceType: VirtualMachine
tags: SwarmNodeMaster
- deployment: SwarmNode
dependsOn: MasterNode
variables:
tokenvar: $[ dependencies.MasterNode.outputs['deploy.setvar.tokenswarm'] ]
strategy:
runOnce:
deploy:
steps:
- bash: |
echo $(tokenvar)
environment:
name: swarm
resourceType: VirtualMachine
tags: SwarmNode
I have logged this over at VSD https://developercommunity.visualstudio.com/content/problem/937451/unable-to-share-variable-from-one-deployment-job-t.html
I have tried setting the second deployment job to a normal job as per the syntax at https://learn.microsoft.com/en-us/azure/devops/release-notes/2020/sprint-164-update#support-for-output-variables-in-a-deployment-job
jobs:
- deployment: masternode
strategy:
runOnce:
deploy:
steps:
- script: |
SWARMTOKEN="Docker GUID"
echo "##vso[task.setvariable variable=token;isOutput=true]$SWARMTOKEN"
name: 'setvar'
- script: echo $(setvar.token)
name: echovar
environment:
name: swarm
resourceType: VirtualMachine
tags: SwarmNodeMaster
- job: 'swarmnode'
dependsOn: 'masternode'
variables:
tokenvar: $[ dependencies.masternode.outputs['deploy.setvar.token'] ]
steps:
- script: echo $(tokenvar)
name: echovar
It seems whatever I try I can't get the variable to expand in the next deployment step.
Thanks in advance for any help!
UPDATE: @Kontekst thanks for sharing your working yaml, using that i've managed to track it down to defining the environment for the "master" node:
environment:
name: swarm
resourceType: VirtualMachine
tags: SwarmNodeMaster
The swarm master node has the tag "SwarmNodeMaster" and is part of the same environment as the 2 other VMs which have the tag "SwarmNode" which is used in the second step if I change the above to just environment: 'swarm' I can pass a variable to the next step, but again stops working if I try and target the master node...
Here is the yaml that successfully passes a variable to the next deployment job: https://hastebin.com/gudelokufi.bash
My main problem is that I need to get the token from the swarm host to the swarm nodes, but when I try and pass a variable from the deployment step that targets the swarm host, to the deployment job that targets the swarm nodes, the variable cannot be expanded in the second deployment job...
This is what I want to work, but seems to break the output variable: https://hastebin.com/yocepowopu.bash
Upvotes: 1
Views: 437
Reputation: 31
So after much trial and error I found that when you use environment tags as part of a deployment job, it appends the job name with the environment resource name, followed by the lifecycle hook. This is the working code:
stages:
- stage: MyStage
jobs:
- deployment: masterNode
displayName: AnyDeploy
pool:
vmImage: 'ubuntu-latest'
environment:
name: swarm
resourceType: virtualmachine
tags: swarmnodemaster
strategy:
runOnce:
deploy:
steps:
- task: Bash@3
displayName: 'Setting output variable'
inputs:
targetType: 'inline'
script: |
token=Some_Value
echo "##vso[task.setvariable variable=token;isOutput=true]$token"
echo $token
name: setvarStep
- task: Bash@3
inputs:
targetType: 'inline'
script: 'env | sort'
- deployment: swarmNode
dependsOn: masterNode
pool:
vmImage: 'ubuntu-latest'
environment:
name: swarm
resourceType: virtualmachine
tags: swarmnode
variables:
myToken: $[ dependencies.masterNode.outputs['masterNode_<resource name>_Deploy.setvarStep.token'] ]
strategy:
runOnce:
deploy:
steps:
- task: Bash@3
displayName: 'Checking if output variable was passed'
inputs:
targetType: 'inline'
script: |
echo My docker token = $(myToken)
I haven't found a way to reference the job name as a variable yet, as the value changes with each job, but the system variable is "SYSTEM_JOBNAME"
Upvotes: 2