【问题标题】:Azure pipelines Secret Variables don't work on PR triggersAzure 管道秘密变量不适用于 PR 触发器
【发布时间】:2019-06-25 04:02:34
【问题描述】:

我有一个 azure 管道,其中包含一个在拉取请求上触发的秘密变量。触发时,秘密变量对管道不可用。

秘密变量在提交到分支时起作用。

管道

pr:
  branches:
    include:
    - '*'
trigger:
  branches:
    exclude:
    - '*'

jobs:
- job:
  pool:
    vmImage: 'ubuntu-latest'
  timeoutInMinutes: 360
  displayName: 'Running test'
  steps:
  - bash: |
      if [ -z "$(system.pullRequest.sourceRepositoryUri)" ]
      then
        python3 runTest.py \
          --config "blessedImageConfig-temp.json" \
          --code $(SecretCode)
      else
        python3 runTest.py \
          --config "blessedImageConfig-temp.json" \
          --pullRepo $(system.pullRequest.sourceRepositoryUri) \
          --pullId $(system.pullRequest.pullRequestNumber) \
          --code $(SecretCode)
      fi

Secret variable added via the webUI

输出和错误

Generating script.
========================== Starting Command Output ===========================
[command]/bin/bash --noprofile --norc /home/vsts/work/_temp/95f6ae7c-d2e1-4ebd-891c-2d998eb4b1d9.sh
/home/vsts/work/_temp/95f6ae7c-d2e1-4ebd-891c-2d998eb4b1d9.sh: line 7: SecretCode: command not found
usage: runTest.py [-h] [--config CONFIG] [--code CODE] [--pullId PULLID]
                  [--pullRepo PULLREPO]
runTest.py: error: argument --code: expected one argument
##[error]Bash exited with code '2'.

【问题讨论】:

  • 我的回答对您解决问题有帮助吗?随时分享问题的最新状态。

标签: azure azure-devops azure-pipelines


【解决方案1】:

SecretCode:找不到命令

这个错误是因为它是一个秘密变量,它是在命令行中以错误的方式传递的。

您可能对此感到困惑。但是,事实上,微软曾经用doc 警告过这一点:永远不要在命令行上传递秘密。这是设计的。

我曾经在我的 docker build 中遇到过类似的问题。我通过将秘密变量值映射到环境变量来解决它,这也在Variable的文档中提到。

对于你的 Bash 任务,还有关于秘密变量的解决方案:使用环境变量输入将秘密变量传递给这个脚本'并设置 targetType == Inline 是必要的强>。

因此,您可以将以下脚本添加到您的 Bash 任务脚本中,以将秘密变量映射到环境变量中:

inputs:
    targetType: 'inline'
    - script: 
      echo $code 
      env: 
        code: $(SecretCode)

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多