代码之家  ›  专栏  ›  技术社区  ›  pixel

使变量在Bitbucket管道中的步骤之间可见?

  •  5
  • pixel  · 技术社区  · 6 年前

    我想在两个步骤中共享一个变量。

    我把它定义为:

    - export MY_VAR="FOO-$BITBUCKET_BUILD_NUMBER"
    

    但当我在另一个步骤中尝试打印时:

    - echo $MY_VAR
    

    它是空的。

    2 回复  |  直到 6 年前
        1
  •  14
  •   Jack Klimov    6 年前

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

    Settings -> Pipelines -> Repository Variables
    
        2
  •  16
  •   Mr-IDE    4 年前

    由于某些原因,导出的环境变量不会保留 在的子项之间 "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:
              # 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
    

    注意:尽量避免使用带有空格或新行字符的字符串(用于键或值)。这个 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
    

    更多信息:

    pipelines:
      default:
        - step:
            script:
              - export MY_VAR1="FOO1-$BITBUCKET_BUILD_NUMBER"
        - step:
            script:
              - echo $MY_VAR1 # This will not work
    

    在这种情况下,Bitbucket管道将处理 “步骤:” 项作为完全独立的构建,因此 “步骤:” 将从一个空白文件夹和一个新文件夹开始 git clone .

    因此,您应该使用声明的 ,如答案所示 贝尔加西亚 (2019年12月19日)。

        3
  •  13
  •   belgacea    4 年前

    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