How to define rules in gitlab "stages" so that no need to define separate rules for jobs

9k Views Asked by At

Suppose there are 4 jobs are define in stages .

stages:

  • build
  • test
  • deploy
  • upload

Now test, deploy and upload only run when pipeline trigger manually. We can define rule on separate jobs for this but it will be repetitive.(do not want to use that)

Is it possible to define rule on "stages" so that one job will run on automatic trigger and rest 3 job will run on manual trigger of pipeline.

2

There are 2 best solutions below

2
Subhakant Priyadarsan On

I know this is a late answer, but I found the solution while running into this scenario.

Use workflow to control pipeline behavior.(Introduced in GitLab 12.5) https://docs.gitlab.com/ee/ci/yaml/index.html#workflow

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
0
Johannes On

I'm afraid at this moment it is not possible to apply a rule to an entire stage.

A possible workaround is to have all the jobs of a stage in a seperate template file. This include action can have a rule [1].

example: template.yml

job1:
  stage: test
  script:
    - run_tests

job2:
  stage: deploy
  script:
    - do_deployment

job3:
  stage: upload
  script:
    - do_the_uploading

.gitlab-ci.yml:

include:
  - local: 'template.yml'
    rule:
      - your_rule_here

stages:
  - build
  - test
  - deploy
  - upload

build_job:
  stage: build
  script:
    - gcc sourcecode.cpp

[1] https://docs.gitlab.com/ee/ci/yaml/includes.html