run dependency jobs if condtion is true in gitlab pipeline

Viewed 4252

I want to run dependency job if condition is true. Do we have that feasibility in git lab.When i manual triggered test job with DEPLOY variable then dependencies should run otherwise skip dependencies. I don't want to keep condition at build stage.

build:
 stage: build
 when: manual
 script:
   - echo build
test:
  stage: test
  when: manual
  dependencies:
    - build
    if [ $deploy = 'true' ]
  script: 
   - echo test
1 Answers

The Gitlab documentation is a good starting point, especially the rules section.

The rules keyword can be used to include or exclude jobs in pipelines.

Rules are evaluated in order until the first match. When matched, the job is either included or excluded from the pipeline, depending on the configuration. If included, the job also has certain attributes added to it.

This means you can use rules for such logical involvement, in your case it would look like

build:
 stage: build
 when: manual
 script:
   - echo build
test:
  stage: test
  needs: ['build'] # dependency on previous build stage
  script: 
   - echo test
  rules:
   - if: '$deploy == "true"' # when true, than run automatically
   - if: '$deploy != "true"' # when not true, than run only manually
     when: manual

I am not sure about the second rule, if it is needed or not. But i highly recommend to take a look at following directives in the GitLab documentation:

Related