gitlab-ci: add support for job:when and rules:

Release notes

Previously it was impossible to configure a when: keyword for a CI job that had rules: this limits our users when defining their jobs in their CI/CD pipelines, in this release we've dropped that limitation by supporting job:when and rules: keyword allowing you to define jobs in your pipeline in a flexible way.

Summary

Documentation for Rules:If states:

If rules:when is not included in the configuration at all, the behavior defaults to job:when, which continues to default to on_success

To me, this suggests I can leave the when statement out of the if rules and specify the when statement at a job level. However, when I try this I get a YAML lint error.

My use case is that I am using the extends keyword to apply some custom rules to multiple jobs. However, only the deployment job should be manual (see sample .gitlab-ci.yml file below). I can get around this by making .custom-rule-manual and explicitly setting when: manual but then the if statement is duplicated, which I'd like to avoid.

Steps to reproduce

The following .gitlab-ci.yml file fails linting with the error jobs:deployjob config key may not be used with `rules`: when. I am using the GitLab.com CI Lint.

testJob:
  stage: test
  script:
    - echo Testing
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $STAGE_BRANCH'
    
deployJob:
  stage: deploy
  when: manual
  script:
    - echo Deploying
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $STAGE_BRANCH'

Proposal

  • The expected behavior is that we should add support for jobs:when and rules to support the above configuration
  • There was a discussion regarding the implicit and the explicit when (should one override the other?), however, eventually we agreed that implicit when should not override explicit when here and here.
  • Here is a short POC proposal
  • The change will not contain Breaking changes
Edited by Dov Hershkovitch