gitlab-ci

job depends on artifacts but not on success or failure


what I need

three gitlab-ci jobs - lets call them A, B and C.

they are all in the same stage (if that matters)

so clearly job C needs to depend on A and B to finish (artifacts are generated when they finish). But the docs don't seem to have such an entry. They only have a trigger for success and failure. not for finished jobs in general.

minimal example

stages:
  - build
  - test
  - coverage

build_job:
  stage: build
  script:
    - echo building...

test_job_A:
  needs:
    - build_job
  stage: test
  script:
    - echo "logs of tests executed in A" > test_result_A.txt
  artifacts:
    paths:
      - test_result_*

test_job_B:
  needs:
    - build_job
  stage: test
  script:
    - echo "logs of tests executed in B" > test_result_B.txt
    - mimic failing pipeline
  artifacts:
    paths:
      - test_result_*

coverage_C:
  needs:
    - test_job_A
    - test_job_B
  stage: test
  script:
    - cat test_result_A.txt
    - cat test_result_B.txt

which will result in the coverage_C job not executed:


using dependencies and moving the job C into another stage:

...

coverage_C:
  dependencies:
    - test_job_A
    - test_job_B
  stage: coverage
  script:
    - cat test_result_A.txt
    - cat test_result_B.txt

results in the same - coverage_C job not executed:


Solution

  • coverage_C is in test, shouldn't it be in coverage? In any case, test_job_B fails, therefore any job that depends on its success (!) will not start. That is a reasonable default interpretation of what a dependency means.

    Your mistake is that by using the default, you depend on the success of the test jobs, you need to configure coverage_C so that it runs even if one of its dependencies fails. Check out the docs for the jobs' rules section, you need when: always.

    Now, there is still another challenge left: A failing job will by default not export any artifacts. You probably want test logs even if one of them fails, so you will have to configure that as well. As a suggestion, create an empty file first though, so if anything fails fatally and the file is not created, it doesn't cause the dependent job to fail.


    Allow me to edit your answer for some more details. You can re-write this as you like.

    ...
    
    test_job_B:
      needs:
        - build_job
      stage: test
      script:
        - echo "logs of tests executed in B" > test_result_B.txt
        - mimic failing pipeline
      artifacts:
        when: always
        paths:
          - test_result_*
    
    coverage_C:
      needs:
        - test_job_A
        - test_job_B
      stage: coverage
      script:
        - cat test_result_A.txt
        - cat test_result_B.txt
      when: always
    
    other_job_D:
      stage: test
      script:
        - sleep 200
    

    enter image description here