在模板中读取时,azure 管道中设置的变量会损坏

时间:2021-07-28 19:15:52

标签: azure azure-devops azure-pipelines

变量 big_var_01 通过遵循 azure 管道定义为值 '3q4w#V$X3q4w#V$X' 当在 azure 管道模板中读回时,yaml 文件被损坏为值 '3q4w#V#V'

cat parent_scott.yaml



variables:
- name: big_var_01
  value: ${{ parameters.big_var_01 }}


parameters:
  - name: big_var_01
    displayName: "this var wants to get written to then read by templates"
    type: string
    default: '3q4w#V$X3q4w#V$X'


# CI Triggers
trigger:
  branches:
    exclude:
      - '*'

pool:
  vmImage: 'ubuntu-latest'

# Release Stages
stages:

- template: child_scott_one.yaml

在 azure 管道模板变量 big_var_01 之后被读回,但其值已损坏且与上述赋值不匹配

cat child_scott_one.yaml 


# Release Stages
stages:
- stage: A
  jobs:
  - job: JA
    steps:
    - script: |
        echo "here is big_var_01  -->$(big_var_01)<-- "

        local_var_01=$(big_var_01)

        echo
        echo "here is local_var_01  -->$local_var_01<-- "
        echo
        echo "length of local_var_01 is ${#local_var_01}"
        echo


      name: DetermineResult

查看上面管道的运行

https://dev.azure.com/sekhemrekhutawysobekhotep/public_project/_build/results?buildId=525&view=logs&j=54e3124b-25ae-54f7-b0df-b26e1988012b&t=52fad91f-d6ac-51fb-b63d-00fda7898bb6&l=13

https://github.com/sekhemrekhutawysobekhotep/shared_variables_across_templates 处查看代码

如何使字符串变量 big_var_01 显然被视为文字,它以某种方式被评估并因此损坏......上面的代码是我实际天蓝色管道的简化,即使在设置键值时我也遇到相同的变量损坏问题值为 3q4w#V$X3q4w#V$X 的秘密在管道模板中读回时会损坏

这是另一个管道运行,它在这次运行中明确显示了这个问题 https://dev.azure.com/sekhemrekhutawysobekhotep/public_project/_build/results?buildId=530&view=logs&j=ed5db508-d8c1-5154-7d4e-a21cef45e99c&t=a9f49566-82d0-5c0a-2e98-46af3de0d6e9&l=38 我检查了标记为 ON 管道运行选项:“启用系统诊断”......接下来我将尝试从 azure 中单引号我的 shell 分配变量

2 个答案:

答案 0 :(得分:0)

在某个步骤 Azure DevOps 或 Ubuntu 替换了您的部分字符串。所以你有:

3q4w#V$X3q4w#V$X = 3q4w#V + $X3q4w + #V + $X

这部分 $X3q4w 和这 $X 被替换为空字符串,为您提供 3q4w#V + #V

如果你在 \ 之前用 $ 运行这个,就像这里 3q4w#V\$X3q4w#V\$X

This is job Foo.
here is big_var_01  -->3q4w#V$X3q4w#V$X<-- 

here is local_var_01  -->3q4w#V$X3q4w#V$X<-- 

length of local_var_01 is 16

我在 windows-latest 上运行时出错,但我得到了正确的字符串:

"This is job Foo."
"here is big_var_01  -->3q4w#V$X3q4w#V$X<-- "
'local_var_01' is not recognized as an internal or external command,
operable program or batch file.
ECHO is off.
"here is local_var_01  -->$local_var_01<-- "
ECHO is off.
"length of local_var_01 is ${#local_var_01}"
ECHO is off.
##[error]Cmd.exe exited with code '9009'.

所以看起来 ubuntu 用 env 变量替换了它,因为没有像 $X3q4w$X 这样的变量,它用空字符串替换。

答案 1 :(得分:0)

找到了一个解决方案……如果我在 bash shell 赋值期间将 azure 变量单引号,它会起作用

local_var_01=$(big_var_01)    #  bad results with value  3q4w#V#V
local_var_02="$(big_var_01)"  #  bad results with value  3q4w#V#V
local_var_03='$(big_var_01)'  #  good this gives value 3q4w#V$X3q4w#V$X

我的直觉说不使用单引号,知道它不是 bash shell 方式,但是一旦我接受了这一事实,azure 会执行一些魔术~~帮助~~在管道源代码和完全解析的 bash shell 执行之间的间隙层预处理我采取有机会尝试了这个……来了解一下 bash shell 如何阻止变量扩展