我正在尝试使用Azure DevOps配置docker swarm集群,该环境包含3个RHEL7虚拟机,并安装了Docker,其中一个配置为swarm主机。
这是管道的yaml文件:
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
我已尝试按照https://docs.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
无论我怎么尝试,我都无法在下一步部署步骤中扩展变量。
在此先感谢您的帮助!
更新: @Kontekst感谢您分享您的工作Yaml,使用它,我已经设法将其追踪到定义“主”节点的环境:
environment:
name: swarm
resourceType: VirtualMachine
tags: SwarmNodeMaster
swarm主节点具有标签“ SwarmNodeMaster”,并且与其他两个具有标签“ SwarmNode”的VM处于同一环境中,如果我将以上内容更改为正义环境,则在第二步中使用该标签: '我可以将变量传递到下一步,但是如果我尝试将主节点作为目标,则会再次停止工作...
这是成功将变量传递给下一个部署作业的Yaml: https://hastebin.com/gudelokufi.bash
我的主要问题是我需要将令牌从swarm主机获取到swarm节点,但是当我尝试将变量从针对swarm主机的部署步骤传递到针对swarm节点的部署作业时,则无法在第二个部署作业中扩展该变量...
这是我想要的工作,但似乎破坏了输出变量:https://hastebin.com/yocepowopu.bash
答案 0 :(得分:2)
因此,在经过反复试验之后,我发现当您将环境标记用作部署作业的一部分时,它会在作业名称后附加环境资源名称,后跟生命周期挂钩。这是工作代码:
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)
我还没有找到一种方法来引用作业名称作为变量,因为该值随每个作业而变化,但是系统变量是“ SYSTEM_JOBNAME”