动态执行gitlab-ci作业

时间:2020-10-29 12:26:49

标签: bash gitlab gitlab-ci

我有一个名为“ job1”的工作,用于检查某些内容。根据此结果,应执行“ job2”或“ job3”。我用build.env构件尝试了它。但是环境变量EXIT_CODE仅在脚本部分可用,而在rulesonly作业注释中不可用。然后,我尝试使用全局变量(variables: EXIT_CODE: "0")并尝试在“ job1”的脚本部分进行设置。但这并不能正常工作。我应该如何执行作业2/3,具体取决于作业1的某些结果

stages:
  - check
  - merge

variables:
  EXIT_CODE: "0"

job1:
  stage: check
  image: ubuntu:20.10
  allow_failure: true
  script:
    - set +e
    - git merge --ff-only github/master
    - EXIT_CODE=4
    - EXIT_CODE="4"
      # - echo "EXIT_CODE=$?" >> build.env
      # - cat build.env
      #artifacts:
      #  reports:
      #    dotenv: build.env

job2:
  stage: merge
  dependencies:
    - job1
  rules:
    - if: '$EXIT_CODE == "0"'
  script:
    - echo "WILL FAIL $EXIT_CODE"
      #  only:
      #    variables:
      #      - $EXIT_CODE == "128"

job3:
  stage: merge
  dependencies:
    - job1
  script:
    - echo "WILL success"
  only:
    variables:
      - $EXIT_CODE == "0"

1 个答案:

答案 0 :(得分:0)

如果您输入job2定义:

job2:
  stage: merge
  when: on_success

然后,仅当job1成功(即,其退出代码为“ 0”)时才应执行。