Manage GitLab configuration as code
GitLab Configuration as Code (GCasC)
Manage GitLab configuration as code to make it easily manageable, traceable and reproducible.
Table of Contents
- Quick start
When configuring your GitLab instance, part of the settings you put in Omnibus or Helm Chart configuration, and the rest you configure through GitLab UI or API. Due to tons of configuration options in UI, making GitLab work as you intend is a complex process.
We intend to let you automate things you do through now UI in a simple way. The Configuration as Code has been designed to configure GitLab based on human-readable declarative configuration files written in Yaml. Writing such a file should be feasible without being a GitLab expert, just translating into code a configuration process one is used to executing in the web UI.
GCasC offers a functionality to configure:
It gives you also a way to:
- include external files or other Yamls using
- inject environment variables into configuration using
!envdirective into your Yaml configuration. Visit our documentation site for detailed information on how to use it.
Configuring your GitLab instance is as simple as this:
settings: elasticsearch: url: http://elasticsearch.mygitlab.com username: !env ELASTICSEARCH_USERNAME password: !env ELASTICSEARCH_PASSWORD recaptcha_enabled: yes terms: '# Terms of Service\n\n *GitLab rocks*!!' plantuml: enabled: true url: 'http://plantuml.url' license: starts_at: 2019-11-17 expires_at: 2019-12-17 plan: premium user_limit: 30 data: !include gitlab.lic
Note: GCasC supports only Python 3+. Because Python 2.7 end of life is January 1st, 2020 we do not consider support for Python 2.
Here you will learn how to quickly start with GCasC.
Important! Any execution of GCasC may override properties you define in your Yaml files. Don't try it directly on your production environment.
Visit our documentation site for detailed information on how to use it.
You can configure client in two ways:
- using configuration file:
By default GCasC is trying to find client configuration file in following paths:
[global] url = https://gitlab.yourdomain.com ssl_verify = true timeout = 5 private_token = <personal_access_token> api_version = 4
"/etc/python-gitlab.cfg", "/etc/gitlab.cfg", "~/.python-gitlab.cfg", "~/.gitlab.cfg",
You can provide a path to your configuration file in
GITLAB_CLIENT_CONFIG_FILE environment variable.
- using environment variables:
GITLAB_CLIENT_URL=<gitlab_url> # path to GitLab, default: https://gitlab.com GITLAB_CLIENT_API_VERSION=<gitlab_api_version> # GitLab API version, default: 4 GITLAB_CLIENT_TOKEN=<personal_access_token> # GitLab personal access token GITLAB_CLIENT_SSL_VERIFY=<ssl_verify> # Flag if SSL certificate should be verified, default: true
Personal access token is mandatory in any client configuration approach and you can configure your it by following these instructions
Additionally you can customize HTTP session to enable mutual TLS authentication. To configure this, you should provide two additional environment variables:
Prepare GitLab configuration
GitLab configuration must be defined in Yaml file. You can provide a configuration in a single file, or you can split it into multiple Yaml files and inject them.
For information how to prepare GitLab configuration Yaml file visit our documentation site.
settings configuration, which defines Application Settings,
the structure is flexible. For example
settings: elasticsearch: url: http://elasticsearch.mygitlab.com username: elastic_user password: elastic_password
settings: elasticsearch_url: http://elasticsearch.mygitlab.com elasticsearch_username: elastic_user elasticsearch_password: elastic_password
are exactly the same and match
This means you can flexibly structure your configuration Yaml, where a map child keys are prefixed by parent key (here
elasticsearch parent key was a prefix for
password keys). You only need to follow available
You can adjust your Yamls by splitting them into multiple or injecting environment variables into certain values using
!env directives respectively. Example is shown below:
settings: elasticsearch: url: http://elasticsearch.mygitlab.com username: !env ELASTICSEARCH_USERNAME password: !env ELASTICSEARCH_PASSWORD terms: !include tos.md license: !include license.yml
settings.elasticsearch.passwordare injected from environment variables
licenseare injected from
tos.mdplain text file and
license.ymlYaml file respectively. In this scenario, your
license.ymlmay look like this:
starts_at: 2019-11-17 expires_at: 2019-12-17 plan: premium user_limit: 30 data: !include gitlab.lic
To run GCasC you can leverage CLI or Docker image. Docker image is a preferred way, because it is simple and does not require from you installing any additional libraries. Also, Docker image was designed that it can be easily used in your CI/CD pipelines.
When running locally, you may benefit from running GCasC in TEST mode (default mode is
APPLY), where no changes
will be applied, but validation will be performed and differences will be logged. Just set
environment variable to
GCasC library is available in PyPI.
To install CLI run
pip install gitlab-configuration-as-code. Then you can simply execute
//TODO add more information on CLI usage
Currently, CLI is limited and does not support passing any arguments to it, but behavior can only be configured using environment variables. Support for CLI arguments may appear in future releases.
Image is available in Docker Hub.
GCasC Docker image working directory is
/workspace. Thus you can quickly launch
docker run -v $(pwd):/workspace hoffmannlaroche/gcasc
It will try to find both GitLab client configuration and GitLab configuration in
/workspace directory. You can modify
the behavior by passing environment variables:
GITLAB_CLIENT_CONFIG_FILEto provide path to GitLab client configuration file
GITLAB_CONFIG_FILEto provide a path to GitLab configuration file
docker run -e GITLAB_CLIENT_CONFIG_FILE=/gitlab/client.cfg -e GITLAB_CONFIG_FILE=/gitlab/config.yml -v $(pwd):/gitlab hoffmannlaroche/gcasc
You can also configure a GitLab client using environment variables. More details about the configuration of GitLab client are available in this documentation.
We provide a few examples to give you a quick starting place to use GCasC. They can be found in
gitlab.cfgis example GitLab client file configuration.
basicis an example GitLab configuration using a single configuration file.
environment_variablesshows how environment variables can be injected into GitLab configuration file using
modularizedshows how you can split single GitLab configuration file into smaller and inject files containing static text using
make to build a basic Docker image quickly.
make you can additionally pass
DOCKER_IMAGE_NAME to change default
gcasc:latest to another image name:
make docker-build DOCKER_IMAGE_NAME=mygcasc:1.0
To get more control over builds you can use
docker build directly:
docker builds -t gcasc[:TAG] .
Dockerfile comes with two build arguments you may use to customize your image by providing
docker build command:
GCASC_PATHdefines the path where GCasC library will be copied. Defaults to
WORKSPACEdefines a working directory when you run GCasC image. Defaults to
make to build source distribution (sdist), Wheel binary distribution and Sphinx documentation.
Both source and Wheel distributions will be placed in
dist directory. Documentation page will be placed
Remember to run tests before building your distribution!
Before submitting a pull request make sure that the tests still succeed with your change. Unit tests run using Github Actions and passing tests are mandatory to get merge requests accepted.
You need to install
tox to run unit tests locally:
# run the unit tests for python 3, python 2, and the flake8 tests: tox # run tests in one environment only: tox -e py37 # run flake8 linter and black code formatter tox -e flake # run black code formatter tox -e black
Instead of using
tox directly, it is recommended to use
# run tests make test # run flake8 linter and black code formatter make lint
Everyone is warm welcome to contribute!
Project is released under Apache License, Version 2.0 license.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size gitlab-configuration-as-code-0.2.tar.gz (41.6 kB)||File type Source||Python version None||Upload date||Hashes View hashes|
Hashes for gitlab-configuration-as-code-0.2.tar.gz