How to change variable in gitlab cicd pipeline depending on branch - gitlab-ci

I need to call a bash script from my gitlab cicd pipeline. When I call it, the input parameter needs to change depending on whether or not this is a merge into master. Basically what I want is this:
if master:
test:
stage: test
script:
- INPUT="foo"
- $(myscript.sh $INPUT)
if NOT master:
test:
stage: test
script:
- INPUT=""
- $(myscript.sh $INPUT)
I'm trying to figure out a way to set INPUT depending on which branch the pipeline is running on. I know there are rules as well as only/except, but they don't seem to allow you to set variable, only test them. I know the brute force way is to just write this twice, one with "only master" and another with "except master", but I would really like to avoid that.
Thanks

I implement this kind of thing using yml anchors to extend tasks.
I find it easier to read and customization can include other things, not only variables.
For example:
.test_my_software:
stage: test
script:
- echo ${MESSAGE}
- bash test.sh
test stable:
<<: *test_my_software
variables:
MESSAGE: testing stable code!
only:
- /stable.*/
test:
<<: *test_my_software
variables:
MESSAGE: testing our code!
except:
- /stable.*/
you get the idea...

Why not have two jobs to run the script and use rules to control when they are ran against master:
test:
stage: test
script:
- if [$CI_COMMIT_REF_NAME == 'master']; then export INPUT="foo"; else INPUT=""; fi
- $(myscript.sh $INPUT)

Related

Gitlab CI pipeline syntax query

We have a Gitlab CI pipeline (.gitlab-ci.yml file)
In that file there are 3 stages
stages:
build
validate
deploy:stage
Firstly, there are 2 jobs
validate:rules:
stage: validate
validate:charts:
stage: validate
Secondly, there is a job that does not have a stage announced inside,
.deploy: &deploy
Later, there is another job:
job-abc:
<<: *deploy
stage: deploy:stage
Q1: can we call same stage in 2 jobs as it has been done in case of validate:rules and validate:charts.
Q2: what is the generic purpose of using double colon in job names such as validate:rules.
Q3: It will be helpful if someone can explain ".deploy: &deploy" and "<<: *deploy" above.

GitLab-CI: run job only when a branch is made

In gitlab runner, I want to run job by separating branch creation and branch update.
(.yml example)
creat job:
stage: create
script:
- echo 'branch checkout'
only:
- ????
update job:
stage: deply
script:
- echo 'branch update'
only:
- branches
You might be able to check CI_COMMIT_BEFORE_SHA. If it is all zeros, then there is no previous commit for that ref. (It is also all zeros for a merge request.)
See predefined variables.

Unable to pass variables through gitlab bridge to another pipeline

Here is my simple yaml file
image: my/docker/image
stages:
- print
- testvarbridge
variables:
INCOMING_VAR: $ENV_VAR
print_these:
stage: print
script:
- echo $INCOMING_VAR
- export $INCOMING_VAR
testvarbridge:
stage: testvarbridge
variables:
TEST_VAR: $INCOMING_VAR
trigger:
project: my-project/pipeline-two
branch: ci-cd
the $ENV_VAR is a variable in the project for testing... it just says "this_is_the_variable"
When I trigger the pipleine..the print stage correctly prints:
echo $INCOMING_VAR
this_is_the_variable
But when the second pipeline is triggered, it is just set up to do a simple echo command of the variable that is passed in.. it echo's this:
echo TEST_VAR
$ENV_VAR
As you can see, the when the testvarbridge stage sets up the variable TEST_VAR, it is grabbing the $ENV_VAR variable up top as a literal string. It does not evaluate it and grab the value associated with that variable. Am I missing something?
This was reported 3 years ago in gitlab-org/gitlab-runner issue 1809: "Use variable inside other variable".
A workaround is to set vars in a before_script instead of variables.
So the example given in the issue would work if written as:
before_script:
- export VAR1="${CI_PIPELINE_ID}"
- export VAR2="test-${VAR1}"
Update February 2020: Philippe Charrière adds:
the issue is not closed - the milestone is 13.0 (for May 2020)
Update Sept. 2021,
One last validation is pending and if that's solved then we are shipping this thing in 14.3.
So, soon (14.3, Sept. 2021):
this-job-name-123:
except:
variables:
- $CI_COMMIT_MESSAGE =~ /Skip $CI_JOB_NAME/i
script:
- echo The job runs unless the commit message contains "Skip this-job-name-123"

Multiple extends or multiple stages?

I want to have a CI to deploy two commands ("bash X" and "bash Y") on different production servers (server 1, server 2, server 3, etc.).
I looked for multiple stages but it don't seems to answer my question.
I don't really care if it runs in parallel or B after A. (the manual section is for debugging)
I don't know how to do it : I tried with multiple extends but it only takes the last one (bashB) in my pipeline.
stages:
- get_password
- bashA
- bashB
get_password:
stage: get_password
# Steps
.bashA:
stage: bashA
script:
- lorem ipsum
when: manual
only:
changes:
- script/bashA.sh
.bashB:
stage: bashB
script:
- ipsum loreem
when: manual
only:
changes:
- script/bashB.sh
# SRV1
deploy-srv1:
extends:
- .bashA
- .bashB
variables:
SRV_1: urlsrv1
# SRV2
deploy-srv2:
extends:
- .bashA
- .bashB
variables:
SRV_1: urlsrv2
I just want to be able to deploy bashA and bash B on X servers (I just took 2 servers for example).
When using multiple extend in GitLab, some of the values will not be merged, but overwritten. If you check the documentation here:
https://docs.gitlab.com/ee/ci/yaml/#extends
They write:
The algorithm used for merge is “closest scope wins”, so keys from the last member will always shadow anything defined on other levels
You are not alone in wanting a feature to be able to merge scripts instead of overwriting them. Here's an open issue on GitLab to do what you described:
https://gitlab.com/gitlab-org/gitlab/issues/16376
In the meantime, and only looking at the example you provided, you can get something like what you want by manually merging bashA and bashB into one job:
stages:
- get_password
- bash
get_password:
stage: get_password
# Steps
.bash_both:
stage: bash
script:
- lorem ipsum
- ipsum loreem
when: manual
only:
changes:
- script/bashA.sh
- script/bashB.sh
# SRV1
deploy-srv1:
extends:
- .bash_both
variables:
SRV_1: urlsrv1
# SRV2
deploy-srv2:
extends:
- .bash_both
variables:
SRV_1: urlsrv2

Way to use anchors/references in job.<spec>.script for DRYness

I'm fairly new to using gitlab-ci and as such, I've run into a problem where the following fails ci-lint because of my use of anchors/references:
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://localhost:2375
.install_thing1: &install_thing1
- do things
- to install
- thing1
.install_thing2: &install_thing2
- do things to
- install thing2
.setup_thing1: &setup_things1
variables:
VAR: var
FOO: bar
script:
- all
- the
- things
before_script:
...
stages:
- deploy-test
- deploy-stage
- deploy-prod
test:
stage: deploy-test
variables:
RUN_ENV: "test"
...
only:
- tags
- branches
script:
- *install_thing1
- *install_thing2
- *setup_thing1
- other stuff
...
test:
stage: deploy-stage
variables:
RUN_ENV: "stage"
...
only:
- master
script:
- *install_thing1
- *install_thing2
- *setup_thing1
- other stuff
When I attempt to lint the gitlab-ci.yml, I get the following error:
Status: syntax is incorrect
Error: jobs:test:script config should be a string or an array of strings
The error eludes to just needing an array for the script piece, which I believe I have. Use of the <<: *anchor pragma causes an error as well.
So, how can one accomplish what I'm trying to do here where I don't have to repeat the code in every -block?
You can fix it and even make it more DRY, take a look at the Auto DevOps template Gitlab created.
It can fix your issue and even more improve your CI file, just have a template job like their auto_devops job, include it in a before_script and then you can combine and call multiple functions in a script block.
The anchors only give you limited flexibility.
(This concept made it possible for me to have one CI file for 20+ projects and a centralized functions file I wget and load in my before_script.)