I have to run a pipeline based on some conditional which I want to evaluate in .gitlab-ci.yml
config. file. Basically, I want to create jobs based on if a condition is true. Below is my current .gitlab-ci.yml
.
# This is a test to run multiple pipeline with sing .gitlab-ci.yml file.
# identifier stage will identify which pipeline (A or B) to run and only jobs
# of that pipeline would be executed and rest would be skipped.
# variables:
# PIPE_TYPE: "$(mkdir identifier; echo 'B' > identifier/type.txt; cat identifier/type.txt)"
# PIPE_TYPE: "B"
stages:
#- identify
- build
- test
#identify:
# stage: identify
# before_script:
# - mkdir "identifier"
# - echo "B" > identifier/type.txt
# script:
# - PIPE_TYPE=$(cat identifier/type.txt)
# - echo $PIPE_TYPE
# artifacts:
# paths:
# - identifier/type.txt
before_script:
# - mkdir "identifier"
# - echo "B" > identifier/type.txt
# - export PIPE_TYPE=$(cat identifier/type.txt)
- export PIPE_TYPE="B"
build_pipeline_A:
stage: build
only:
refs:
- master
variables:
- $PIPE_TYPE == "A"
script:
- echo $PIPE_TYPE
- echo "Building using A."
- mkdir "buildA"
- touch buildA/info.txt
artifacts:
paths:
- buildA/info.txt
build_pipeline_B:
stage: build
only:
refs:
- master
variables:
- $PIPE_TYPE == "B"
script:
- echo "Building using B."
- mkdir "buildB"
- touch buildB/info.txt
artifacts:
paths:
- buildB/info.txt
test_pipeline_A:
stage: test
script:
- echo "Testing A"
- test -f "buildA/info.txt"
only:
refs:
- master
variables:
- $PIPE_TYPE == "A"
dependencies:
- build_pipeline_A
test_pipeline_B:
stage: test
script:
- echo "Testing B"
- test -f "buildB/info.txt"
only:
refs:
- master
variables:
- $PIPE_TYPE == "B"
dependencies:
- build_pipeline_B
Here, I have two pipelines A
with jobs build_pipeline_A
and test_pipeline_A
and second pipeline as B
with build_pipeline_B
and test_pipeline_B
jobs.
First I thought I can create a job identify
which would evaluate some logic and write which pipeline to be used in a file (identifier/type.txt
) job and update PIPE_TYPE
variable. This variable can be used in all the jobs under only:variables
testing and would create the job if PIPE_TYPE
is equal to job’s pipeline type, unfortunately, this didn’t work.
In second try, I thought of using global variables
and try to evaluate the expression there and set it to PIPE_TYPE
this didn’t work either.
In my last try I used a before_script
which would evaluate the expression and set it in PIPE_TYPE
in hopes of on:variables
will able to pick PIPE_TYPE
value but no luck with this approach too.
I ran out of ideas at this point and decided to post the question.
my test’s .gitlab-ci.yaml file, it’s a public repo. so please feel free to poke around it.