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

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?

Related

error in gitlab yaml - include and extends

I'm having a gitlab yaml file whose before_scripts section needs to be used in another gitlab yaml. I'm doing something like this:
include:
- remote: 'https://gitlab.xxx.net/awsxxx/job-template/-/blob/master/.gitlab-
ci-template.yml'
extends:
- before_script
The relevant contents of the above url file are:
before_script:
- echo "foo"
- echo "bar"
This is not working, returns that the syntax is incorrect. Can you please help me correct this? Note: There are multiple extends and multiple parent 'include' and so I'm using the '-' format for extends and include here
I guess the error you're getting is because you can only use the extends keyword on a job, see the relavant page https://docs.gitlab.com/ee/ci/yaml/#extends. Are you trying to somehow append the remote yaml with your own before_script ? You should be able to reuse the job name from the remote yaml and do the before script there like:
include:
- remote: 'https://gitlab.xxx.net/awsxxx/job-template/-/blob/master/.gitlab-
ci-template.yml'
job to overwrite from ci-template:
before_script:
- echo "foo"
- echo "bar"

How to add commands to before_script inherited from template

Let's say I have a template which contains something like this:
.some-scripts: &some-scripts |
set -e
function somefunction() {
}
.template-job:
before_script:
- *some-scripts
- echo "Example command"
- somefunction
build-job:
extends: .template-job
stage: build
script:
- mvn build
This template is included in another gitlab-ci.yml and I am looking to add some specific commands to the before_script of my build-job without overriding the before_script of the template-job. Is it possible and how?
I found what I was looking for, I needed to use a reference tag.
Here's what I came up with:
build-job:
stage: build
before_script:
- !reference [.template-job, before_script]
- mycommand
- mysecondcommand
script:
- mvn build
Currently you cannot extend a before_script, just overwrite it. But there is an open issue regarding the extending behavior.
As a workaround you could just add the additional commands to your script section as before_script, script and after_script are ultimatley merged together to one block on execution.
.template-job:
before_script:
- echo "Example command"
- echo "Second command"
build-job:
extends: .template-job
stage: build
script:
- echo "third command"
- echo "fourth command"
- mvn build

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

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

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

Resources