Links
- docs.gitlab.com/ee/ci/examples
- CI/CD Templates
- Example projects
- docs.gitlab.com/ee/ci/yaml
- learnxinyminutes.com/docs/yaml
Knowledge
- Pipeline
- Pipeline is what you’re defining in the
.gitlab-ci.yml
file, and is what happens when the contents of the file are run on a runner. - A pipeline consists of Jobs and Stages
- Creating a Git tag or adding a Pipeline schedule, do not trigger Pipelines unless you add rules for those cases
- Stage
- Defines order of execution
- Typical stages are
build
, test
, deploy
- If there are runners available, jobs in a single stage run in parallel
- Job
- Job specifies tasks for each stage
- Example Jobs are
compile
or unit test
and integration test
- Each Job belongs to a Stage
- Variables
- Scopes
- Types
- Predefined Variables
- Set by GitLab, they provide info about current job, pipeline, environment
- Usually prefixed with
$CI_
- Custom Variables
- Created and managed in GitLab UI, API, or in configuration files
- Visibility modifiers
- Protected
- Only available to jobs running on protected branches or tags
- Masked
- Values are hidden in job logs
- CI/CD Component
- Reusable pipeline configuration unit
- Use a CI/CD Component to compose an entire pipeline configuration or a small part of a larger pipeline
- You can add a component to your pipeline configuration with
include:component
script
- Each command executes as if it was run as a CLI command.
- By default, if a command fails or returns an error, the job is flagged as failed and no more commands run.
artifacts
- If you want files generated in one job to be used in another job, save them as artifacts.
- Example config:
1
2
3
| artifacts:
paths:
- "build/"
|
stages
stages
lists Stages and their order- Jobs in the same Stage can run in parallel
- Jobs in later Stages wait for jobs in earlier Stages to complete
- If a Job fails, the whole Stage is considered failed and Jobs in later Stages do not start
allow_failure
- Jobs which fail won’t stop the whole Pipeline
dependencies
- Use to fetch Artifacts from other Jobs
- Set
dependencies: []
to speed up Job by not fetching Artifacts
- Hidden Jobs
- Jobs that start with dot
.
are never added to Pipeline - Useful for common configurations
extends
- Used to apply configuration
- Mostly used to apply Hidden Jobs
default
- Sets keyword defaults when given configuration not defined in Job
- E.g. in
default: image: ruby:3.0
the image
will be set to ruby:3.0
for all Jobs without defined image
- Otherwise
image
will be overridden with image
specified in Job
- Some tips
- You can validate
.gitlab-ci.yml
file on GitLab- There’s also available JSON Schema
- Use
needs
to make dependencies - Use
rules
to add conditions - Use
cache
and artifacts
to persist files - Use
default
to add configuration for all jobs- Often used to define
before_script
and after_script
- Use
extends
and default
to avoid duplication
YAML Features
- Anchor
&
- Used to duplicate content across document (duplicate or inherit properties)
- Valid only in files where defined
- Use Anchors with Hidden Jobs to provide templates for Jobs
- In case of duplicated keys, the latest included one key wins (overriding previous ones)
- Example:
.job_template &job_configuration
- Merging
<<
- Pastes referenced content
- Example:
<<: *job_configuration
- Including Anchor
*
Anchor used to build arrays with multiple components
1
2
3
4
5
6
7
8
| .default_scripts: &default_scripts
- ./default-script1.sh
- ./default-script2.sh
job1:
script:
- *default_scripts
- ./job-script.sh
|
Anchor with merging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| .job_template: &job_configuration
image: ruby:2.6
services:
- postgres
- redis
test1:
<<: *job_configuration
script:
- test1 project
test2:
<<: *job_configuration
script:
- test2 project
|
Anchors for scripts
You can use YAML Anchors with script
, before_script
, and after_script
to use predefined commands in multiple jobs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| .some-script-before: &some-script-before
- echo "Execute this script first"
.some-script: &some-script
- echo "Execute this script second"
- echo "Execute this script too"
.some-script-after: &some-script-after
- echo "Execute this script last"
job1:
before_script:
- *some-script-before
script:
- *some-script
- echo "Execute something, for this job only"
after_script:
- *some-script-after
job2:
script:
- *some-script-before
- *some-script
- echo "Execute something else, for this job only"
- *some-script-after
|
Configurations
Maven config
- Environment variables make Maven use repository homedir instead of the user home for config and dependencies
.m2/repository
folder is where all the Maven files are storedtarget
folder is where application is created
1
2
3
4
5
6
7
8
| variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
|