Azure DevOps Pipeline在部署中定义变量,并在后续作业中重用 [英] Azure DevOps Pipeline define variable in deployment and reuse in subsequent job

查看:684
本文介绍了Azure DevOps Pipeline在部署中定义变量,并在后续作业中重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure DevOps管道来部署我的代码,现在我需要将变量值从部署作业传递到依赖于它的后续作业.我已经阅读了此示例,但它似乎根本不起作用.

I'm using an Azure DevOps pipeline to deploy my code and now I'm in need of passing a variable value from a deployment job to a subsequent job that depends on it. I've read up on this example but it does not seem to work at all.

我要执行的操作是运行提供密钥保管库的Azure ARM部署.密钥保管库的名称是从ARM部署作业输出的,然后我尝试将该名称传递给另一个需要添加特定机密的作业.访问控制已解决,但我仍然需要传递名称.

What I'm trying to do is run an Azure ARM Deployment that provisions a Key Vault. The name of the key vault is outputted from the ARM deployment job and I'm then trying to pass that name to another job which needs to add specific secrets. Access control is taken care of, but I still need to pass the name.

我将问题归结为将变量从deployment传递到job的基础.这是我完整的测试管道(几乎完全复制自

I've boiled the problem down to the basics of passing a variable from a deployment to a job. Here is my complete test pipeline (almost entirely copied from here):

trigger: none

stages:
  - stage: X
    jobs:
      - deployment: A
        pool:
          vmImage: "ubuntu-16.04"
        environment: test
        strategy:
          runOnce:
            deploy:
              steps:
                - script: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the deployment variable value"
                  name: setvarStep
                - script: echo $(setvarStep.myOutputVar)
                  name: echovar

      - job: B
        dependsOn: A
        pool:
          vmImage: "ubuntu-16.04"
        variables:
          myVarFromDeploymentJob: $[ dependencies.A.outputs['deploy.setvarStep.myOutputVar'] ]
        steps:
          - script: "echo $(myVarFromDeploymentJob)"
            name: echovar

运行此命令后,回显的值在作业B中为空,但在部署A中定义.为什么是这样?并有一种方法可以清除dependencies.A.outputs中的所有内容,以便我可以看到需要使用的东西吗?

Once I run this the echoed value is blank in job B, but defined in deployment A. Why is this? And is there a way to dum everything in dependencies.A.outputs so that I can see what I have to work with?

如何将变量从runOnce部署作业传递给常规作业?

How can I pass a variable from a runOnce deployment job to a regular job?

推荐答案

我已经解决了.问题在于文档此处指定此架构以获取runOnce部署的变量:

I've solved it. The problem is that the documentation here specifies this schema for fetching the variable for a runOnce deployment:

$[dependencies.<job-name>.outputs['<lifecycle-hookname>.<step-name>.<variable-name>']]

这实际上是错误. <lifecycle-hookname>参数应替换为部署名称,如下所示:

This is in fact WRONG. The <lifecycle-hookname> parameter should be replaced with the name of the deployment like this:

$[dependencies.<job-name>.outputs['<job-name>.<step-name>.<variable-name>']]

来自

The example from this documentation (scroll down a bit) is correct.

我已经测试并运行的完整示例管道:

A full example pipeline that I've tested and works:

trigger: none

stages:
- stage: X
  jobs:
  - deployment: A # This name is important
    pool:
      vmImage: 'ubuntu-16.04'
    environment: staging
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the deployment variable value"
            name: setvarStep # This name is also important
          - script: echo $(setvarStep.myOutputVar)
            name: echovar

  - job: B
    dependsOn: A
    pool:
      vmImage: 'ubuntu-16.04'
    variables:
      myVarFromDeploymentJob: $[ dependencies.A.outputs['A.setvarStep.myOutputVar'] ]
    steps:
    - script: "echo $(myVarFromDeploymentJob)"
      name: echovar

这篇关于Azure DevOps Pipeline在部署中定义变量,并在后续作业中重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆