【问题标题】:Create variables dynamically in azure pipeline在 Azure 管道中动态创建变量
【发布时间】:2020-12-08 15:16:37
【问题描述】:

我正在尝试在 azure piplelines 阶段生成发行说明并将说明推送到 azure 服务总线。

如何在 bash 脚本中公开变量,然后在同一阶段的后续作业中使用它?

我正在使用 bash 任务来执行 git 命令并尝试将其导出为我想在以下作业中使用的环境变量。

  - stage: PubtoAzureServiceBus
    variables:
      COMMIT_MSG: "alsdkfgjdsfgjfd"
    jobs:
      - job: gitlog
        steps:
          - task: Bash@3
            inputs:
              targetType: 'inline'
              script: |
                # Write your commands here
                
                export COMMIT_MSG=$(git log -1 --pretty=format:"Author: %aN%n%nCommit: %H%n%nNotes:%n%n%B")
                env | grep C
      - job:
        pool: server
        dependsOn: gitlog
        steps:
          - task: PublishToAzureServiceBus@1
            inputs:
              azureSubscription: 'Slack Release Notifications'
              messageBody: |
                {
                  "channel":"XXXXXXXXXX",
                  "username":"bot",
                  "iconEmoji":"",
                  "text":":airhorn: release :airhorn: \n`$(COMMIT_MSG)`"
                }
              signPayload: false
              waitForCompletion: false

【问题讨论】:

    标签: azure-devops azure-pipelines


    【解决方案1】:

    您需要使用logging syntax and output variables,如下所示:

    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    stages:
    - stage: A
      jobs:
      - job: A1
        steps:
         - bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
         # or on Windows:
         # - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
           name: printvar
    
    - stage: B
      dependsOn: A
      jobs:
      - job: B1
        condition: in(stageDependencies.A.A1.result, 'Succeeded', 'SucceededWithIssues', 'Skipped')
        steps:
        - script: echo hello from Job B1
      - job: B2
        variables:
          varFromA: $[ stageDependencies.A.A1.outputs['printvar.shouldrun'] ]
        steps:
        - script: echo $(varFromA) # this step uses the mapped-in variable
    
    

    请查看here 以查看文档。

    所以你需要更换

                    export COMMIT_MSG=$(git log -1 --pretty=format:"Author: %aN%n%nCommit: %H%n%nNotes:%n%n%B")
    

    isOutput=true 的智能日志记录命令

    然后把它映射到这里

    jobs:
    - job: A
      steps:
      - bash: |
           echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
        name: ProduceVar  # because we're going to depend on it, we need to name the step
    - job: B
      dependsOn: A
      variables:
        # map the output variable from A into this job
        varFromA: $[ dependencies.A.outputs['printvar.shouldrun']
      steps:
      - script: echo $(varFromA) # this step uses the mapped-in variable
    
    

    因为您想在作业之间共享变量(而不是第一个示例中显示的阶段)。

    【讨论】:

    • 是作业A1中的echo命令生成的输出,并保存在名为printvar的变量中?
    • 是的。但重要的是拥有isOutput=true
    • 在哪里运行 git log 命令?是附加到 echo "##vso..."
    • 之前,并将结果分配给变量。然后在这里使用##vso[task.setvariable variable=shouldrun;isOutput=true]<put variable here>
    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2021-11-23
    • 2021-08-09
    相关资源
    最近更新 更多