在Bitbucket管道的各个步骤中使变量可见?

问题描述 投票:5回答:2

我想分两个步骤来共享一个变量。

我把它定义为:

- export MY_VAR="FOO-$BITBUCKET_BUILD_NUMBER"

但是当我尝试在其他步骤中打印时:

- echo $MY_VAR

它是空的。

我如何分享这样的变量?

environment-variables bitbucket-pipelines
2个回答
5
投票

我很害怕,但似乎无法从一个步骤到另一个步骤共享环境变量,但是您可以在pipelines类别下为项目设置中的所有步骤定义全局环境变量。

Settings -> Pipelines -> Repository Variables

6
投票

您可以将所有环境变量复制到文件中,然后将其读回:

- step1:
  # 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

- step2:
  # Read all the previous environment variables from the file, and export them again
  - export $(cat ENVIRONMENT_VARIABLES.txt | xargs)
  - echo $MY_VAR1
  - echo $MY_VAR2

更多信息:

© www.soinside.com 2019 - 2024. All rights reserved.