Kamafeather
Kamafeather

Reputation: 9835

Run a CI job only if another manual job was triggered

I have a .gitlab-ci.yml with 2 deploys and a job to check that the deployed APIs are reachable.

stages:
  - deploy-stage
  - deploy-prod
  - check

service:stage:
  stage: deploy-stage

service:production:
  stage: deploy-prod
  when: manual

check:stage:
  stage: check

check:production:
  stage: check
  dependencies: service:production

At the moment, even though the specified dependencies, I'm having the check:production running even when the service:production job is skipped (I did not manually trigger it).

I could add allow_failure: false to service:production, so that check:production is not run (indirectly, because the whole pipeline gets halted), but I would prefer a way to express more explicitly the direct dependency of check:productionservice:production.


How to configure check:production to run automatically, only when service:production was manually triggered?

Upvotes: 2

Views: 1681

Answers (1)

Adam Marshall
Adam Marshall

Reputation: 7649

You can use the needs keyword to state that one job needs another.

stages:
  - deploy-stage
  - deploy-prod
  - check

service:stage:
  stage: deploy-stage

service:production:
  stage: deploy-prod
  when: manual

check:stage:
  stage: check

check:production:
  stage: check
  dependencies: service:production
  needs: ['service:production']

In this example, check:production won't run if service:production has failed, is a manual job and hasn't been run yet, was skipped, or was cancelled.

Needs can also be used to tell jobs to run before other, unrelated jobs from previous stages finish. This means that check:production can start once service:production finishes, even if service:stage is still running.

Here's the docs for more information on this and other keywords: https://docs.gitlab.com/ee/ci/yaml/#needs

You can use the dependencies keyword for similar results, but if the other job fails or is an untriggered manual job, the dependent job will still run, and might fail depending on the outcome of the first job. Needs is a newer, and improved option.

Upvotes: 3

Related Questions