如何为GitLab CI/CD任务添加依赖:仅在test-job1成功后执行test-job2
配置GitLab CI/CD实现test-job2依赖test-job1成功后运行
要让test-job2仅在test-job1成功完成后立即触发,只需在test-job2的配置中添加needs关键字指定依赖即可。修改后的完整CI配置如下:
build-job: stage: build script: - echo "Hello, $GITLAB_USER_LOGIN!" test-job1: stage: test script: - python3 mydumbscript.py $arg1 $arg2 $arg3 test-job2: stage: test needs: [test-job1] # 关键配置:指定依赖test-job1 script: - echo "This job tests something, but takes more time than test-job1." - echo "After the echo commands complete, it runs the sleep command for 20 seconds" - echo "which simulates a test that runs 20 seconds longer than test-job1" - sleep 20
配置说明:
needs: [test-job1]会强制test-job2等待test-job1成功完成后才启动,即便两个job同属test阶段,也不会和test-job1并行执行,而是在test-job1成功后立即触发。- 默认情况下,若test-job1失败,test-job2会被自动跳过,无需额外配置。如果需要明确禁止失败时运行,可以添加
allow_failure: false(此为默认值,可省略)。
内容的提问来源于stack exchange,提问作者kishi
相关产品推荐
相关产品推荐

