GitLab CI
Table of Contents
Overview
image: ruby:2.1
services:
- postgres
before_script:
- bundle install
after_script:
- rm secrets
stages:
- build
- test
- deploy
job1:
stage: build
script:
- execute-script-for-job1
only:
- master
tags:
- docker
Reference
beforescript
- Used to define the command that should be run before all jobs.
- This has to be an array or a multi-line string.
afterscript
- Used to define the command that should be run after all jobs.
- This has to be an array or a multi-line string.
stages
- Used to define stages that can be used by jobs.
- First, all jobs of
build
are executed in parallel. - If all jobs of
build
succeed, thetest
jobs are executed in parallel. - If all jobs of
test
succeed, thedeploy
jobs are executed in parallel. - If all jobs of
deploy
succeed, the commit is marked assuccess
. - If any of the previous jobs fails, the commit is marked as
failed
and no jobs of further stage are executed.
types
- Alias for stages
variables
- All variables are set as environment variables in the build environment
- These variables can be later used in all executed commands and scripts.
variables:
DATABASE_URL: "postgres://postgres@postgres/my_database"
LS_CMD: 'ls $FLAGS $$TMP_DIR'
FLAGS: '-al'
job_name:
script:
- echo $CI_JOB_ID # Defined by CI
- echo $DATABASE_URL # Defined in `variables`
- 'eval $LS_CMD' # will execute 'ls -al $TMP_DIR'
Note: Integers (as well as strings) are legal both for variable's name and value. Floats are not legal and cannot be used.
cache
By default caching is enabled and shared between pipelines and jobs, starting from GitLab 9.0
cache:
paths:
- my/files
rspec:
script: test
cache:
key: rspec
paths:
- binaries/ # Locally defined cache overrides globally defined options.
Jobs
job_name:
script:
- rake spec
- coverage
stage: test
only:
- master
except:
- develop
tags:
- ruby
- postgres
allow_failure: true
script
only and except
- In the example below, job will run only for refs that start with
issue-
, whereas allbranches
will be skipped:
Job variables
- it overrides the global YAML job variables and predefined ones
tags
- Used to select specific Runners from the list of all Runners that are allowed to run this project.
artifacts
- Used to specify a list of files and directories which should be attached to the job after success.
dependencies
- Used in conjunction with
artifacts
and allows you to define theartifacts
to pass between different jobs.
Note that artifacts from all previous stages are passed by default.
build:osx:
stage: build
script: make build:osx
artifacts:
paths:
- binaries/
build:linux:
stage: build
script: make build:linux
artifacts:
paths:
- binaries/
test:osx:
stage: test
script: make test:osx
dependencies:
- build:osx # only downloads artifacts from build:osx
test:linux:
stage: test
script: make test:linux
dependencies:
- build:linux # only downloads artifacts from build:linux
# downloads both ~artifacts~ because of the stage precedence
deploy:
stage: deploy
script: make deploy
Special YAML features
Anchors
.job_template: &job_definition # Hidden key that defines an anchor named 'job_definition'
image: ruby:2.1
services:
- postgres
- redis
test1:
<<: *job_definition # Merge the contents of the 'job_definition' alias
script:
- test1 project
test2:
<<: *job_definition # Merge the contents of the 'job_definition' alias
script:
- test2 project
Terminology
Topics
cache vs artifacts,dependencies
artifacts
are created during a run and can be used by the following JOBS of that very same currently active run.caches
can be used by following RUNS of that very same JOB (a script in a stage, like 'build' in my example).