Gitlab CI/CD Trigger only a single stage in gitlab-ci.yml file on a scheduled pipeline - continuous-integration

I want to run a single stage in gitlab from a yml file that contains a lot of stages. I don't want to have to add this to every single stage to avoid running all the stages.
except:
refs:
- schedules

Instead of explicitly defining the except tag for each job. You can define it once as an anchor.
.job_template: &job_definition
except:
refs:
- schedules
test1:
<<: *job_definition
script:
- test1 project

If you don't want to add except in each job, use only instead of except
https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-advanced
below there is an example with variables
only_with_variable:
script: ls -la
only:
variables:
- $VAR == "1234"
after that if you schedule a pipeline, you have the option to add variables to them.
in the example, you just need to add the VAR variable with value = 1234

You can use the following to run the stage only on a scheduled job
build-app:
stage: build-app
only:
- schedules

Related

Is gitlab-ci.yml extends keyword always parsed first in a job?

Here is a sample of gitlab-yml file:
.job1:
script:
- |-
echo "1"
echo "2"
extends:
- .job2
- .job3
- .job4
allow_failure: true
I would like to run script object first but seems that extends object has priority and always runs first.
Any idea how I can make jobs within executes to run after my scripts have executed within the same job?

Pass file variable to gitlab job

I am having trouble with dynamically passing one of two file based variables to a job.
I have defined two file variables in my CI/CD settings that contain my helm values for deployments to developement and production clusters. They are typical yaml syntax, their content does not really matter.
baz:
foo: bar
I have also defined two jobs for the deployment that depend on a general deployment template .deploy.
.deploy:
variables:
DEPLOYMENT_NAME: ""
HELM_CHART_NAME: ""
HELM_VALUES: ""
before_script:
- kubectl ...
script:
- helm upgrade $DEPLOYMENT_NAME charts/$HELM_CHART_NAME
--install
--atomic
--debug
-f $HELM_VALUES
The specialization happens in two jobs, one for dev and one for prod.
deploy:dev:
extends: .deploy
variables:
DEPLOYMENT_NAME: my-deployment
HELM_CHART_NAME: my-dev-chart
HELM_VALUES: $DEV_HELM_VALUES # from CI/CD variables
deploy:prod:
extends: .deploy
variables:
DEPLOYMENT_NAME: my-deployment
HELM_CHART_NAME: my-prod-chart
HELM_VALUES: $PROD_HELM_VALUES # from CI/CD variables
The command that fails is the one in the script tag of .deploy. If I pass in the $DEV_HELM_VALUES or $PROD_HELM_VALUES, the deployment is triggered. However if I put in the $HELM_VALUES as described above, the command fails (Error: "helm upgrade" requires 2 arguments, which is very misleading).
The problem is that the $HELM_VALUES that are accessed in the command are already the resolved content of the file, whereas passing the $DEV_HELM_VALUES or the $PROD_HELM_VALUES directly works with the -f syntax.
This can be seen using echo in the job's output:
echo "$DEV_HELM_VALUES"
/builds/my-company/my-deployment.tmp/DEV_HELM_VALUES
echo "$HELM_VALUES"
baz:
foo: bar
How can I make sure the $HELM_VALUES only point to one of the files, and do not contain the files' content?

How to use variables in gitlab-ci.yml file

I'm trying to use variables in my gitlab-ci.yml file. This variable is passed as a parameter to a batch file that'll either only build or build and deploy based on parameter passed in. I've tried many different ways to pass my variable into the batch file but each time the variable is treated more like a static string instead.
I've read gitlabs docs on variables but cant seem to make it work.
- build
variables:
BUILD_PUBLISH_CONFIG_FALSE: 0
BUILD_PUBLISH_CONFIG_TRUE: 1
# BUILD ===============================
build: &build
stage: build
tags:
- webdev
script:
- ./build.bat %BUILD_CONFIG%
build:branch:
<<: *build
variables:
BUILD_CONFIG: $BUILD_PUBLISH_CONFIG_FALSE
only:
- /^(feature|hotfix|release)\/.+$/
build:branch:
<<: *build
variables:
BUILD_CONFIG: $BUILD_PUBLISH_CONFIG_TRUE
only:
- /^(stage)\/.+$/
build:branch:
<<: *build
variables:
BUILD_CONFIG: $BUILD_PUBLISH_CONFIG_TRUE
only:
- /^(master)\/.+$/
When watching gitlab's ci script execute, I expect ./build.bat 0, or ./build.bat 1.
Each time it prints out as ./build.bat %BUILD_CONFIG%
When you place variables inside job, that mean that you want to create new variable (and thats not correct way to do it). You want to output content of variable setup on top? Can u maybe add that to echo? or something like that? I didn't get it what you are trying to achieve.
https://docs.gitlab.com/ee/ci/variables/#gitlab-ciyml-defined-variables

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

I want to setup gitlab to run a job when a branch when the branch name matches some criteria.
This is my current yml and the job is run when a branch is created that end in '-rc'.
However it also runs if I create a tag ending '-rc'. How can I stop the job running when a tag is created (I have tried exclude - tags).
stages:
- release_to_qa
qa:
stage: release_to_qa
when:
manual
#except:
# - tags
only:
- branches
- /^*-rc$/
tags:
- pro1
- shared
script:
echo "hello world"
you can use only & except
job:
# use regexp
only:
- /^issue-.*$/
# use special keyword
except:
- branches
https://docs.gitlab.com/ee/ci/yaml/#only-and-except-simplified

GitLab continuous integration to run jobs in several branches

My .gitlab-ci.yml file contains the following job:
job1:
script:
- pwd
only:
- master
Using only I make this dummy job job1 run the command pwd only when it gets a push into the branch master. From the docs:
only and except are two parameters that set a refs policy to limit
when jobs are built:
only defines the names of branches and tags for which the job will be built.
Now I would like to run this on multiple tags, so following the docs:
only and except allow the use of regular expressions.
I tried to say:
job1:
script:
- pwd
only:
- (master|my_test_branch)
But it does not work at all: neither in master nor in my_test_branch. What is wrong with the regular expression?
Why not just to use multiple values (more readable in my opinion):
only:
- master
- my_test_branch
I did not find any documentation about it, but apparently regular expressions in .gitlab-ci.yml need to be enclosed in / /. Thus, /(master|my_test_branch)/ works.
All together:
job1:
script:
- pwd
only:
- /(master|my_test_branch)/

Resources