【发布时间】:2019-02-28 01:52:22
【问题描述】:
我想跨两个步骤共享一个变量。
我是这样定义的:
- export MY_VAR="FOO-$BITBUCKET_BUILD_NUMBER"
但是当我尝试在其他步骤中打印它时:
- echo $MY_VAR
它是空的。
我如何共享这样的变量?
【问题讨论】:
标签: environment-variables bitbucket-pipelines
我想跨两个步骤共享一个变量。
我是这样定义的:
- export MY_VAR="FOO-$BITBUCKET_BUILD_NUMBER"
但是当我尝试在其他步骤中打印它时:
- echo $MY_VAR
它是空的。
我如何共享这样的变量?
【问题讨论】:
标签: environment-variables bitbucket-pipelines
恐怕,但似乎不可能从一个步骤共享环境变量,但是您可以在pipelines类别下的项目设置中为所有步骤定义全局环境变量。
Settings -> Pipelines -> Repository Variables
【讨论】:
由于某种原因,导出的环境变量不会保留在"step:" 的子项之间或顶级"step:" 项之间(有关这些定义here)。但是您可以将所有环境变量复制到一个文件中,然后再次读取它们,因为文件在步骤之间保留:
如何在“script:”和“after-script:”之间共享变量
pipelines:
default:
- step:
script:
# Export some variables
- export MY_VAR1="FOO1-$BITBUCKET_BUILD_NUMBER"
- export MY_VAR2="FOO2-$BITBUCKET_BUILD_NUMBER"
- echo $MY_VAR1
- echo $MY_VAR2
# Copy all the environment variables to a file, as KEY=VALUE, to share to other steps
- printenv > ENVIRONMENT_VARIABLES.txt
after-script:
# If the file exists, read all the previous environment variables
# from the file, and export them again
- |
if [ -f ENVIRONMENT_VARIABLES.txt ]; then
export $(cat ENVIRONMENT_VARIABLES.txt | xargs)
fi
- echo $MY_VAR1
- echo $MY_VAR2
注意:尽量避免使用包含空格或换行符的字符串(用于键和值)。 export 命令将无法读取它们,并且可能会引发错误。一种可能的解决方法是use sed to automatically delete any line,其中包含一个空格字符:
# Copy all the environment variables to a file, as KEY=VALUE, to share to other steps
- printenv > ENVIRONMENT_VARIABLES.txt
# Remove lines that contain spaces, to avoid errors on re-import (then delete the temporary file)
- sed -i -e '/ /d' ENVIRONMENT_VARIABLES.txt ; find . -name "ENVIRONMENT_VARIABLES.txt-e" -type f -print0 | xargs -0 rm -f
更多信息:
printenv 命令pipelines:
default:
- step:
script:
- export MY_VAR1="FOO1-$BITBUCKET_BUILD_NUMBER"
- step:
script:
- echo $MY_VAR1 # This will not work
在这种情况下,Bitbucket Pipelines 会将 2 个 "step:" 项目视为完全独立的构建,因此第二个 "step:" 将从头开始,带有一个空白文件夹和一个新的 git clone。
因此,您应该使用声明的 artifacts 在步骤之间共享文件,如 belgacea 的答案所示(2019 年 12 月 19 日)。
【讨论】:
git clone开始,你的ENVIRONMENT_VARIABLES.txt将消失。不过,这是可能使用工件的。将 ENVIRONMENT_VARIABLES.txt 作为工件添加到您的 bitbucket-pipelines.yml 文件中,它将在下一步中可用(如果下一步在 7 天内运行,则工件会在一周后移除)。
"ENVIRONMENT_VARIABLES.txt-e" 是正确的。但是,可能不需要整个 find 命令。我之所以添加它,是因为 MacOS 上的 sed -i -e 会导致创建一个新的临时文件(文件名后附加“-e”)。但是 Linux/Ubuntu 上的 sed 没有这个错误。 Bitbucket Pipelines 通常在 Linux/Ubuntu 机器上运行。
正如Mr-IDE 和Rik Tytgat 解释的那样,您可以通过将环境变量写入文件来导出环境变量,然后通过以下步骤将该文件共享为artifact。一种方法是在一个步骤中将变量写入 shell 脚本,将其定义为 artifact,然后在下一步中获取它。
definitions:
steps:
- step: &build
name: Build
script:
- MY_VAR="FOO-$BITBUCKET_BUILD_NUMBER"
- echo $MY_VAR
- echo "export MY_VAR=$MY_VAR" >> set_env.sh
artifacts: # define the artifacts to be passed to each future step
- set_env.sh
- step: &deploy
name: Deploy
script:
# use the artifact from the previous step
- cat set_env.sh
- source set_env.sh
- echo $MY_VAR
pipelines:
branches:
master:
- step: *build
- step:
<<: *deploy
deployment: test
注意:就我而言,将set_env.sh 作为工件发布的步骤并不总是我的管道的一部分。在这种情况下,请务必在下一步使用之前检查该文件是否存在。
- step: &deploy
name: Deploy
image: alpine
script:
# check if env file exists
- if [ -e set_env.sh ]; then
- cat set_env.sh
- source set_env.sh
- fi
【讨论】:
artifacts,我想不出一种方法来安全地加密它而不把它弄得一团糟。无论如何,为你自己而使用 GitHub...