Reputation: 13
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.
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:
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
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