GitLab CI
lastmod 2025-12-08created 2018-08-2838 edits
Since GitLab 8.0, GitLab-ci has been integrated into GitLab.
Categories
- GitLab
- GitLab:Kubernetes - How to integrate with k8s, etc. ...
- GitLab:PackageRegistry
- GitLabCI:Artifacts
- GitLabCI:Release
- GitLabCI:Rules
- GitLabCI:Python
- GitLabCI:Yarn
Examples
- AWS:CLI:Amplify#How to deploy from the command line - How to deploy to Amplify using the AWS CLI.
Pipeline
Gitlab_cicd_pipeline_infograph.png
Keywords
| Keyword | Description | Subkeywords |
| after_script | Override a set of commands that are executed after the job. | |
| allow_failure | Allow the job to fail. A failed job does not cause the pipeline to fail. | |
| List of files and directories to attach to a job on success. | | |
| before_script | Override a set of commands that are executed before the job. | |
| cache | List of files that should be cached between subsequent runs. | |
| coverage | Code coverage settings for a given job. | |
| dependencies | Restrict which artifacts are passed to a specific job by providing a list of jobs to fetch artifacts from. | |
| environment | Name of an environment to which the job deploys. | |
| except | Control when jobs are not created. | |
| extends | Configuration entries that this job inherits from. | |
| image | Use Docker images. | |
| include | Include external YAML files. | |
| inherit | Select which global defaults all jobs inherit. | |
| interruptible | Defines whether a job can be canceled when made redundant by a newer run. | |
| needs | Execute jobs earlier than the stage ordering. | |
| only | Control when jobs are created. | |
| pages | Upload the result of a job to use with GitLab Pages. | |
| parallel | How many instances of a job should be run in parallel. | |
| release | Instructs the runner to generate a release object. | |
| resource_group | Limit job concurrency. | |
| retry | When and how many times a job can be auto-retried in case of a failure. | |
| rules | List of conditions to evaluate and determine selected attributes of a job, and whether or not it's created. Cannot be used together with | |
| script | Shell script that is executed by a runner. | |
| secrets | The CI/CD secrets the job needs. | |
| services | Use Docker services images. | |
| stage | Defines a job stage. | |
| tags | List of tags that are used to select a runner. | |
| timeout | Define a custom job-level timeout that takes precedence over the project-wide setting. | |
| trigger | Defines a downstream pipeline trigger. | |
| variables | Define job variables on a job level. | |
| when | When to run the job. | |
Simple example
- Build and test automation with Docker and Gitlab CI
- Gitlab-CI configuration & .gitlab-ci.yml example
Just add the relevant settings to the project's .gitlab-ci.yml file.
image: ubuntu:18.04
stages:
- preview
- build
- test
- deploy
- review
default-preview:
stage: preview
script:
- "echo Current directory: $PWD"
- "echo Directory listing:"
- "ls -la"
default-build:
stage: build
script:
- "echo Empty build stage"
default-test:
stage: test
script:
- "echo Empty test stage"
default-deploy:
stage: deploy
script:
- "echo Empty deploy stage"
default-review:
stage: review
script:
- "echo Empty review stage"
Tag only
Adding it to only:
Adding it as a condition in rules:
Enable Debug logging
To enable debug logging (tracing), set the CI_DEBUG_TRACE variable to true:
Multiline scripts
With | (YAML#Literal Block Scalar), each newline is treated as a new command.
With > (YAML#Folded Block Scalar), a blank line is required to start a new command.
recc example
image: python:3.7
services:
- postgres:13
- redis:6
- name: "docker.elastic.co/elasticsearch/elasticsearch:7.11.1"
alias: "elasticsearch"
command: [ "bin/elasticsearch", "-Expack.security.enabled=false", "-Ediscovery.type=single-node" ]
variables:
POSTGRES_USER: "recc"
POSTGRES_PASSWORD: "0000"
POSTGRES_HOST_AUTH_METHOD: "trust"
stages:
- test
- deploy-python
- deploy-docker
test-core:
stage: test
variables:
RECC_TEST_HTTP_HOST: 0.0.0.0
RECC_TEST_HTTP_PORT: 10000
RECC_TEST_DB_HOST: postgres
RECC_TEST_DB_PORT: 5432
RECC_TEST_DB_USER: recc
RECC_TEST_DB_PW: "0000"
RECC_TEST_DB_NAME: testdb
only:
- master
- '/^core-.*$/'
before_script:
- bash core/requirements.sh
script:
- cd core
- bash docker-tester.sh
coverage: '/\d+\%\s*$/'
test-fe:
image: node:14.15
stage: test
only:
- master
- '/^fe-.*$/'
before_script:
- apt-get -qq update
- apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb
script:
- cd fe
- yarn
- yarn test:unit
- yarn test:e2e
- yarn lint
deploy-core-python:
stage: deploy-python
only:
- '/^core-.*$/'
before_script:
- bash core/requirements.sh
- pip install devpi-client
script:
- cd core
- devpi use "${DEVPI_URL}"
- devpi login --password "${DEVPI_PW}" "${DEVPI_ID}"
- devpi upload --with-docs --formats bdist_wheel
- devpi logout
deploy-core-docker:
stage: deploy-docker
only:
- '/^core-.*$/'
before_script:
- apt-get -qq update
- apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
- curl -fsSL "https://download.docker.com/linux/debian/gpg" | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
- apt-get -qq update
- apt-get install -y docker-ce-cli
script:
- cd core
- RECC_VERSION=$(python -c 'from recc.util.version import version_text; print(version_text);')
- RECC_IMAGE_CURRENT=${REGISTRY_HOST}/answer/recc:${RECC_VERSION}
- RECC_IMAGE_LATEST=${REGISTRY_HOST}/answer/recc:latest
- echo "Current recc version is ${RECC_IMAGE_CURRENT}"
- echo "${REGISTRY_PW}" | docker login --password-stdin -u "${REGISTRY_ID}" "${REGISTRY_HOST}"
- docker build --tag ${RECC_IMAGE_CURRENT} .
- docker tag "${RECC_IMAGE_CURRENT}" "${RECC_IMAGE_LATEST}"
- docker push "${RECC_IMAGE_CURRENT}"
- docker push "${RECC_IMAGE_LATEST}"
- docker rmi "${RECC_IMAGE_CURRENT}"
- docker rmi "${RECC_IMAGE_LATEST}"
- docker logout
How to install GitLab CI
- gitlab-ci-multi-runner project site
- Install using GitLab's repository for Debian/Ubuntu/CentOS/RedHat (preferred)
- What is the registration token? #77
Installation on CentOS7 is as follows:
$ sudo gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
## GitLab CI 주소를 입력한다. (예: http://your.domain.com/ci)
Please enter the gitlab-ci token for this runner:
## GitLab CI에 접근하기 위한 토큰을 입력한다. (예: 8e9e7eb3e6adfba5aaf9)
## 토큰은 GitLab에서 확인 가능하다.
Please enter the gitlab-ci description for this runner:
## Runner에 대한 설명을 적는다.
Please enter the gitlab-ci tags for this runner (comma separated):
## Runner에 TAG를 적용한다.
INFO[0023] 849a78b3 Registering runner... succeeded
Please enter the executor: docker, docker-ssh, ssh, shell, parallels:
## 실행기를 설정한다.
INFO[0032] Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Details are summarized here: Gitlab_ci_-_docker_runner_setting.pdf
Caching
Inherit global configuration, but override specific settings per job
You can override cache settings without overwriting the global cache by using anchors. For example, if you want to override the policy for one job:
cache: &global_cache
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- public/
- vendor/
policy: pull-push
job:
cache:
# inherit all global cache settings
<<: *global_cache
# override the policy
policy: pull
Caching Python dependencies
Assuming your project is using pip to install the Python dependencies, the following example defines cache globally so that all jobs inherit it. Python libraries are installed in a virtual environment under venv/, pip’s cache is defined under .cache/pip/ and both are cached per-branch:
#
# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
#
image: python:latest
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
paths:
- .cache/pip
- venv/
before_script:
- python -V # Print out python version for debugging
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
test:
script:
- python setup.py test
- pip install flake8
- flake8 .
Caching Node.js (yarn, npm) dependencies
npm case:
#
# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml
#
image: node:latest
# Cache modules in between jobs
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- .npm/
before_script:
- npm ci --cache .npm --prefer-offline
test_async:
script:
- node ./specs/start.js ./specs/async.spec.js
yarn case:
Variables
Troubleshooting
use elasticsearch in gitlab-ci
See Elasticsearch#use elasticsearch in gitlab-ci.
Your runner is outdated, please upgrade your runner
If GitLab CI shows a failure, you may find the following error when you open it.
In other words, the Runner needs to be upgraded.
See also
Favorite site
Documentation
- Getting started GitLab CI
- Configuration of your jobs with .gitlab-ci.yml
- Using Docker Images
- GitLab Documentation: Quick Start 3
- GitLab Documentation: Configuration of your builds with .gitlab-ci.yml 4