Search code examples
gitlab-ci

How to add dependency to gitlab ci/cd jobs?


I would like to execute test-job2 , only if test-job1 succeeds. how can i configure such that test-job2 gets fired , as soon as test-job1 finishes and with a success.

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
  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


Solution

  • You can make use of needs keyword to set up the dependency.

    In your case, you need to add needs: test-job1 under your test-job2.

    test-job2:
      stage: test
      needs: 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
    

    Note: This works for both GitLab and GitHub.