acri
acri

Reputation: 13

Gitlab jobs to be ignored when variable is set

I have a few jobs in my yaml files, and I was finding a way to only run some of the jobs when a particular scheduled is run (i.e. when a certain variable is set).

e.g. ** I have created a new schedule called 'Hourly Schedule', and created a variable called $HOURLY, which is set to TRUE.

HOURLY SCHEDULE

I also have another schedule called 'Daily Schedule' DAILY SCHEDULE

stages:          
  - build
  - test
  - deploy

build-job:       
  stage: build
  script:
    - echo "Build."

test-job:  
  stage: test   
  script:
    - echo "Test."
    
deploy-job:   
  stage: deploy  
  script:
    - echo "Deploy."

hourly-deploy-job:   
  stage: deploy
  script:
    - echo "Hourly Deploy."
  rules:
    - if: $HOURLY

My question is:

  1. How do I make 'build-job', 'test-job', and 'deploy-job' to run during 'commit', 'push', or 'Daily Schedule'
  2. And during 'Hourly Schedule', only the 'hourly-deploy-job' should run (and not other jobs).

I know I can do the following:

stages:          
  - build
  - test
  - deploy

.hourly_deploy: &hourly_deploy
  except:
    variables:
      - $HOURLY == "TRUE"

build-job:       
  <<: *hourly_deploy
  stage: build
  script:
    - echo "Build."

test-job:  
  <<: *hourly_deploy
  stage: test   
  script:
    - echo "Test."
    
deploy-job:  
  <<: *hourly_deploy 
  stage: deploy  
  script:
    - echo "Deploy."

hourly-deploy-job:   
  stage: deploy
  script:
    - echo "Hourly Deploy."
  rules:
    - if: $HOURLY

However, I ideally do a case only only on the 'hourly-deploy-job'. This is because my yaml might potentially be bigger at some point, and if I'm not careful, I might forget to add the <<: *hourly_deploy tag on the new job, which means that job will run during Hourly Schedule.

Upvotes: 1

Views: 783

Answers (1)

23w4ertfgyhj
23w4ertfgyhj

Reputation: 703

This is not an easy thing to achieve, and I think you should rethink your CICD strategy and use templates.

You mentioned this pipeline might grow and using templates might help you a lot.

Here is my example that works but it's not pretty.

stages:
  - build
  - test
  - deploy
  - hourly-deploy-job

build-job:
  stage: build
  rules:
    - if: ($CI_PIPELINE_SOURCE == "push")
      when: always
    - if: ($CI_PIPELINE_SOURCE == "schedule") && ($DAILY == "true")
      when: always
  script:
    - echo "Build."

test-job:
  stage: test
  rules:
    - if: ($CI_PIPELINE_SOURCE == "push")
      when: always
    - if: ($CI_PIPELINE_SOURCE == "schedule") && ($DAILY == "true")
      when: always
  script:
    - echo "Test."

deploy-job:
  stage: deploy
  rules:
    - if: ($CI_PIPELINE_SOURCE == "push")
      when: always
    - if: ($CI_PIPELINE_SOURCE == "schedule") && ($DAILY == "true")
      when: always
  script:
    - echo "Deploy."

hourly-deploy-job:
  stage: hourly-deploy-job
  rules:
    - if: ($CI_PIPELINE_SOURCE == "schedule") && ($HOURLY == "true")
      when: always
  script:
    - echo "Hourly Deploy."

Source:

Upvotes: 1

Related Questions