There’s a lot of things to like about GitLab in my opinion such as it’s API’s, the MR workflow and Terraform integration to name a few. Of course, there’s things to dislike too ;-)
Below are a few tips and notes on working with CI and editing .gitlab-ci.yml
.
For a proper quick start see: https://docs.gitlab.com/ee/ci/quick_start
.
Variables
Predefined vars:
- Package Registry:
$CI_REGISTRY
(docker login) - Docker image:
$CI_REGISTRY_IMAGE
- Build dir:
$CI_PROJECT_DIR
(docker WORKDIR) - Debugging:
$CI_DEBUG_TRACE: "true"
Full list: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
Conditional var:
Example where if INSTALL_ALL is set to "false", a docker image tag is added:
variables:
INSTALL_ALL = "true"
DOCKER_IMAGE: "${CI_REGISTRY_IMAGE}/foo"
workflow:
rules:
- if: $INSTALL_ALL == "false"
variables:
DOCKER_IMAGE: "${CI_REGISTRY_IMAGE}/foo:slim"
.
Jobs
Besides using the debug var mentioned above, this kludge is also useful when debugging. To quickly disable a job add a dot in front of it’s name “my_build_job”:
stages:
- test
- build
test_job:
stage: test
< ... >
.my_build_job:
stage: build
< ... >
.
Scripts
This is one line:
script:
- test -d dir &&
echo "dir exists"
Multi line:
script:
- |
echo "One"
echo "Two"
.
Tags
First add tag(s) to runner in GitLab GUI: ‘Settings > CI/CD > Runners’. Then use those tags in gitlab-ci.yml to select a specific runner for job execution.
E.g. add inside job:
tags:
- docker
- socket
.
Docker in Docker (dind)
Used to build containers. For own self-hosted Runners there’s 2 possible methods using the docker executer: "privileged" or "socket".
This requires changing config.toml, under [runners.docker]
:
Add "/var/run/docker.sock:/var/run/docker.sock"
to volumes
Or privileged = true
The socket method is more secure but can be a bit more difficult to work with. For example when you need access to $CI_PROJECT_DIR
inside container. A solution is available here.
It’s also possible to use the shell executor (also allows using docker compose).
Other alternatives are using Podman/Buildah or Kaniko instead of Docker.
For details see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#enable-docker-commands-in-your-cicd-jobs.