Advanced GitLab CICD
Advanced GitLab CICD
Agenda
Manage Plan Create Verify Package Secure Release Configure Monitor Protect
Epics
Review App
Milestones
Push Fixes
Pipeline
○ Set of one or more jobs. Optionally organized into stages
Stages
○ Collection of jobs to be run in parallel
○ e.g. Build, Test, Deploy
Jobs
○ Scripts that perform tasks
○ e.g. npm test; mvn install; etc.
Environments
○ Where we deploy (Test, Review, Staging, Canary, Prod)
Pipeline Architectures
Pipeline Architectures
Docs
● The building-blocks of your pipeline.
○ Basic
○ Directed Acyclic Graph
○ Parent/Child Pipelines
○ Dynamic Child Pipelines
○ Multi-Project Pipelines
● Simplest pipeline in GitLab
● Jobs run independently, sometimes on
different runners
Basic pipelines ● All Jobs in a Stage must complete
successfully before proceeding to the
next stage
● Control pipeline with pipeline options
Pipeline Architectures - Basic pipeline with options
Other options:
when: delayed
when: on_failure
Jobs
when: always
(DAG)
Pipeline Architectures - Directed Acyclic Graph (DAG)
linux:build:
stage: build Linux path: the linux:rspec job will be
script: echo "Building linux..." run as soon as the linux:build job
mac:build: finishes without waiting for mac:build to
stage: build
script: echo "Building mac..." finish.
lint:
stage: test macOS path: the mac:rspec job will be
needs: []
script: echo "Linting..." run as soon as the mac:build job
linux:rspec: finishes without waiting for linux:build
stage: test
needs: ["linux:build"] to finish.
script: echo "Running rspec on linux..."
mac:rspec:
stage: test
The lint job will run immediately without
needs: ["mac:build"] waiting for build jobs.
script: echo "Running rspec on mac..."
production: The production job runs as soon as all
stage: deploy previous jobs finish.
script: echo "Running production..."
Source: https://docs.gitlab.com/ee/ci/yaml/#needs
Additional example: needs with artifacts
Source: https://docs.gitlab.com/ee/ci/yaml/#needsartifacts
● Run child pipelines independent from
each other.
● Separates entire pipeline configuration
Parent/Child in multiple files to keep things simple.
Pipelines ● Combine with DAG pipelines to
achieve benefits of both.
● Useful to branch out long running
tasks into separate pipelines.
Pipeline Architectures - Parent/Child Pipelines
.gitlab-ci.yml test.gitlab-ci.yml
Reference it later to
actually run the pipeline
Pipeline Architectures - Dynamic Pipelines
Documentation link:
https://docs.gitlab.com/ee/ci/multi_project_pipelines.html
Pipeline Architectures - Multi-Project Pipelines
Hot tip!
Prepopulate the keys and values using URL parameters
.../pipelines/new?ref=<branch>&var[<variable_key>]=<val
ue>
Variables defined in the CI configuration
variables:
GLOBAL_VAR: test
build:
variables:
ENVIRONMENT: staging
script:
- echo “Trying $GLOBAL_VAR on $ENVIRONMENT”
Secrets
deploy:
stage: deploy
secrets:
DATABASE_PASSWORD:
vault: # Translates to secret: `ops/data/production/db`, field: `password`
engine:
name: kv-v2
path: ops
path: production/db
field: password
file: false
# Shortened version:
# vault: production/db/password@ops
Inherited variables
build:
stage: build
script:
- echo "BUILD_VARIABLE=value_from_build_job" >> build.env
artifacts:
reports:
dotenv: build.env
deploy:
stage: deploy
variables:
BUILD_VARIABLE: value_from_deploy_job
script:
- echo "$BUILD_VARIABLE" # Output is: 'value_from_build_job' due to precedence
How variables are processed
Values configured
Order of precedence
Values inherited
from Jobs in previous Stages
CI Jobs
Values in YAML GitLab Runner
using the variables: block
New merge
New commit New branch New tag Manual API call Scheduled
request
Rules: The basics
Job Name
job:
Script: ”echo Hello, Rules!“
rules:
- if: ‘$CI_PIPELINE_SOURCE == “merge_request_event”’
- if: ‘$CI_PIPELINE_SOURCE == “push”’
● This job will not run if this was triggered from a merge request
○ if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
● Otherwise, this job will run if the previous stage was successful
Rules Example 03
● Otherwise, this job will run even if the previous stage had a failure
Rules Example 04
"main" '
● This job will run 3 hours after triggered and will be allowed to fail (will not prevent
further stages from firing)
○ if: '$CI_COMMIT_BRANCH == "main"'
workflow:
rules:
- if: $CI_COMMIT_MESSAGE =~ /-wip$/
when: never
- if: $CI_COMMIT_TAG
when: never
- when: always
● This pipeline will not run if the commit message ends with “-wip”
○ if: '$CI_COMMIT_MESSAGE =~ /-wip$/
● This pipeline will not run if this was triggered by a TAG being applied
○ if: '$CI_COMMIT_TAG
workflow:
rules:
- if: $CI_COMMIT_REF_NAME =~ /-wip$/
variables:
DOCKER_FILE: test.dockerfile
- if: $CI_COMMIT_REF_NAME =- $CI_DEFAULT_BRANCH
variables:
DOCKER_FILE: main.dockerfile
- when: always
● Allows for saving of build artifacts and/or the output of any job
● Are available for use by subsequent jobs
● Can pull in any combination of paths and/or files
● Use exclude to limit what is added
● Use depends to limit what gets downloaded on subsequent jobs
● Use when to determine if artifacts will be stored or not
● Use expire_in to determine when artifacts will be destroyed
artifacts:
when: on_success
paths:
- bin/target
- .exe
exclude:
- throw-away.exe
expire_in: 3mos
https://docs.gitlab.com/ce/ci/yaml/README.html#artifacts
Artifact download by URL
https://gitlab.com/fpotter/examples/c-with-artifact/-/jobs/207917559/artifacts/file/helloworld
Three ways of Build product for the latest CI pipeline run on a specific branch (main):
accessing Artifacts
https://gitlab.com/fpotter/examples/c-with-artifact/-/jobs/artifacts/main/raw/helloworld?job=build
via secure HTTPS
All build products (as a Zip) for the latest CI pipeline run on a specific branch:
https://gitlab.com/fpotter/examples/c-with-artifact/-/jobs/artifacts/main/download?job=build
Artifact download in GitLab UI
In a self-managed GitLab
instance, job artifacts may be
stored in local storage or object
storage
Docker
registry
Dependency
Proxy
Language-specific
package registries
dependencies
build:osx:
stage: build
script: make build:osx
artifacts:
paths:
- binaries/
build:linux:
● Reduce number of artifacts passed from
stage: build previous jobs
script: make build:linux
artifacts: ● Improve job performance by preventing
paths:
- binaries/ large artifacts to download for each job
test:osx:
stage: test ● Empty array will skip downloading
script: make test:osx artifacts
dependencies:
- build:osx
● Only from jobs from previous stages
test:linux:
stage: test
script: make test:linux
dependencies:
- build:linux
deploy:
stage: deploy
script: make deploy
Source: https://docs.gitlab.com/ee/ci/yaml/#dependencies
Include and Extends
Include
● Defines entry names that a job uses extends is going to inherit from
● Alternative to YML anchors, and is more flexible and readable
● Performs a merge
● Supports multilevel inheritance (but more than 3 is not recommended)
● Can merge hidden jobs (.tests) or from regular jobs
Becomes
Using Includes and Extends together
This will run a job called useTemplate that runs echo Hello! as defined in the .template job, and uses
the alpine Docker image as defined in the local job.
Q&A
Contact GitLab