'Gitlab-ci How can I trigger a cleanup job only after previous stage succeeded disregarding all other stages status

I need to create a CI that erases the VMs that were created in the previous stage only if the previous stage succeeded. if I use when: on_success - It works only if all stages passed.

stages:

  • prep (2 jobs)
  • build (5 jobs)
  • test (5 jobs)
  • cleanup

I want cleanup to work if all 5 test jobs passed even if I have a failure in a job that is in the build stage.



Solution 1:[1]

Use needs keyword https://docs.gitlab.com/ee/ci/yaml/#needs

vm:build:
  stage: build
  script: echo "Building vm..."

test_1:
  stage: test
  script: echo "test_1"

test_2:
  stage: test
  script: echo "test_2"

test_3:
  stage: test
  script: echo "test_3"

cleanup:
  stage: cleanup
  needs: ["test_1", "test_2", "test_3"]
  script: echo "clea"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yip102011