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

2

9

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.

Lacroix answered 27/7, 2021 at 12:56 Comment(0)
C
3

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

Convenient answered 12/1, 2022 at 12:1 Comment(0)
O
2

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
Ophthalmic answered 14/11, 2021 at 7:36 Comment(2)
But how do you use workflow to control which jobs/stages to run?Besiege
please provide a full example so we understand how to link the workflow to the stageGuff

© 2022 - 2024 — McMap. All rights reserved.