首页 > 解决方案 > 如何使用 Bash 在 Azure DevOps 中设置构建变量?

问题描述

我正在使用以下内容从我的 package.json 文件中提取版本并将其设置为我的构建变量之一,Version.

# successfully retrieves and prints the version to console
ver=$(node -e "console.log(require('./package.json').version)")
echo "Version: $ver"

# does jack squat
# even trying to hard-code something in place of $ver doesn't set the variable
echo "##vso[task.setvariable variable=Version]$ver"
echo "Version: $(Version)"

我尝试使用verand$(ver)而不是$ver, none 工作,因为控制台$(Version)在所有情况下都打印空白(一开始是空的)。如果我硬编码Version,它打印得很好,所以不是打印或检索,而是设置问题。我的脚本基于 MS 的示例,

echo "##vso[task.setvariable variable=sauce]crushed tomatoes"

我们的构建服务器在 Windows 环境中。

标签: bashazure-devops

解决方案


发布后不久就来了,但我想我会分享以防万一其他人偶然发现这一点。

从文档中,管道变量直到任务结束后才会扩展。微软增强了他们的文档以更好地说明它

steps:

# Create a variable
- script: |
    echo '##vso[task.setvariable variable=sauce]crushed tomatoes'

# Use the variable
# "$(sauce)" is replaced by the contents of the `sauce` variable by Azure Pipelines
# before handing the body of the script to the shell.
- script: |
    echo my pipeline variable is $(sauce)

我怀疑这就是为什么即使对值进行硬编码,您仍然看不到任何东西。


推荐阅读