Skip to content

Implement when syntax in .gitlab-ci.yml

Kamil Trzciński requested to merge when-syntax into master

This MR allow to support for special jobs that are executed only in case of failure.

This is a promised feature: https://dev.gitlab.org/gitlab/gitlab-ci/issues/317

I added to .gitlab-ci.yml job definition when. We have 3 whens:

  • on_success - this is default, job is run only when builds from previous stages succeeds
  • on_failure - job is run when one of the builds from previous stage fails
  • always - job is run regardless the status of previous stage, but only after the previous stage completes

EXAMPLE1 This will always clear docker containers at the end of build process:

stages:
- build
- cleanup

build:
  type: build
  script:
  - docker run --name=gitlab gitlab/gitlab-ce:latest

clean docker images:
  type: cleanup
  when: always
  script:
  - docker rm -f -v gitlab

EXAMPLE2 This will clear docker container only when build or test fails:

stages:
- build
- test
- cleanup

build:
  type: build
  script:
  - docker run --name=gitlab gitlab/gitlab-ce:latest

test:
  type: test
  script:
  - # Run tests against gitlab

clean failed gitlab:
  type: cleanup
  when: on_failure
  script:
  - docker rm -f -v gitlab

Please review @dzaporozhets @vsizov

Merge request reports