如何运行一个不会阻止Gitlab中后续阶段的工作?

时间:2018-05-23 05:47:35

标签: gitlab

在一个项目中,我使用这些工作运行两个阶段:

  • build
    • compile & test
    • generate sonar report
  • deploy
    • deploy to staging environment [manual]
    • deploy to production [manual]

deploy阶段的作业取决于compile & test作业的输出。但是,在generate sonar report阶段开始任何工作之前,deploy工作不需要完成。然而,GitLab坚持认为build阶段的所有工作都已完成,然后才能在deploy阶段开展任何工作。

有没有办法告诉GitLab generate sonar report作业不应该阻止后续的管道阶段?我已经在这项工作上尝试allow_failure: true,但这似乎没有达到预期的效果。这项工作需要很长时间才能完成,我真的不想在部署之前一直等待它。

3 个答案:

答案 0 :(得分:0)

除非我弄错了,否则目前无法做到这一点,并且有一个open feature proposalanother one similar来添加您的建议。

答案 1 :(得分:0)

我们也遇到类似的情况,尽管我们确实使用allow_failure: true,但是当Sonar作业仅需要很长时间才能运行时(不管失败还是成功),这都没有帮助。

由于您不希望您的deploy阶段实际上受generate sonar report工作的结果影响,所以我建议将generate sonar report工作移至deploy阶段,因此您的管道将变为:

  • 构建
    • 编译并测试
  • 部署
    • 部署临时环境[手册]
    • 生产部署[手册]
    • 生成声纳报告[allow_failure:true]

通过这种方式,generate sonar report作业不会延迟您的deploy阶段作业

在{em>之后 generate sonar report运行build & test的另一个好处是,您可以将build & test作业的覆盖率报告保存为Gitlab作业工件,然后使{ {1}}工作将它们作为依赖项使用,因此Sonar也可以监视您的覆盖范围

最后,我们发现将generate sonar report分离为build & test,然后再分离为build很有用,因此我们可以将test失败与build失败分开-并且我们然后可以同时在同一test阶段中并行运行多个测试作业,等等。请注意,您需要将工件从test作业传递到build作业通过Gitlab作业工件和依赖项(如果您选择这样做)

答案 2 :(得分:0)

在我看来,这取决于您的阶段语义。您应该尝试确定您的管道中最重要的是什么:明确阶段还是完成工作。

GitLab 有许多方便的功能,例如 needs keyword,您可以使用它来指定依赖关系图上的直接边。

stages:
    - build
    - deploy

build:package:
    stage: build
    script: 
        - echo "compile and test"
        - mkdir -p target && echo "hello" > target/file.txt
    artifacts:
        paths: 
            - ./**/target

build:report:
    stage: build
    script:
        - echo "consume the target artifacts"
        - echo "waiting for 120 seconds to continue"
        - sleep 120
        - mkdir -p target/reports && echo "reporting" > target/reports/report.txt
    artifacts:
        paths: 
            - ./**/target/reports

deploy:
    stage: deploy
    needs: ["build:package"]
    script:
        - echo "deploy your package on remote site"
        - cat target/file.txt

pipeline screenshot exhibiting the deploy executed before the end of build stage

相关问题