如何在 gitlab ci/cd 中 pipeline job 之间传递变量

pipeline 中不同 job 默认是无法传递变量的,但是我们可以通过 artifacts 功能实现传递,具体看实例

build:
  stage: build
  script:
    - echo "BUILD_VARIABLE=value_from_build_job" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - env
    - echo "$BUILD_VARIABLE"  # Output is: 'value_from_build_job'

参考官方文档:https://docs.gitlab.com/ee/ci/variables/#pass-an-environment-variable-to-another-job

artifacts 这种传递变量的方法还可用于多项目 pipeline、父子 pipeline 中,具体参考:https://docs.gitlab.com/14.7/ee/ci/pipelines/multi_project_pipelines.html#pass-cicd-variables-to-a-downstream-pipeline-by-using-variable-inheritance

posted @ 2022-03-25 11:02  leffss  阅读(2646)  评论(0)    收藏  举报