Gitlab CI Notes
Gitlab CI Notes
Gitlab CI Notes
Tools
pip
twine
poetry
CI Variables
Gitlab Runner
Automation tool that runs scripted commands on the GitLab CI/CD pipeline, allowing the
automation of tasks, tests, builds, and deployments.
Docker Executor
Gitlab Runner can perform tasks using different environments, like Shell, VMs, Docker. We
use the Docker executor which essentially:
CI/CD Variables
A type of environment variables to be used by the runners, when performing tasks. They can
be defined manually in the UI. There are also predefined ones, like for instance
$CI_COMMIT_BRANCH, which gives the name of the branch where the commit will take
place, after the job.
CI/CD variables can be defined per project or per group, like CHIDOS.
Gitlab allows the use of package registries, i.e. a registry where we can store pre-built
python (+npm) libraries, serving as a private PyPI. Although each repo can have its own
package registry, a good practice is to have a unique one per group or instance. This way, all
related packages will be published and can be fetched from the same URL.
.gitlab-ci.yml
A configuration files stored in every repo, which tells gitlab which jobs to trigger. Gitlab runs
these jobs using the available runners, which can shell, docker or VM executor ones. An
example .gitlab-ci.yml is shown below.
stages:
- test
- build
test job:
stage: test
script:
- echo "Running some tests..."
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
Here, we have two jobs, namely the test and the build and publish ones. The first one uses
the default image of the Docker executor (python:3.9), and prints an output to the terminal
and gets triggered only on merge requests. We control the triggering of this job using the
rules parameter and the predefined CI/CD variables.
The "build and publish" job, on the other hand follows the steps:
For CHIDOS, we use the package-registry repo to host all packages. This means that all of
our projects will be published in the same package registry, in the URL https://pyramida.3-
psi.com/api/v4/projects/81/packages/pypi/simple. This URL is defined as a group-level
CI/CD variable with the name ${PACKAGE_REGISTRY}.
Here,
glpat-XXXXXXXXXXXXXXXXXXXX
If needed, we create a new token and copy its contents after the dash ( the ones
marked with X above). This content must be saved within a CI/CD variable with the
name ${PACKAGE_REGISTRY_TOKEN} under CHIDOS/Settings/CI/CD, i.e. we
update the value of the CI/CD variable used by the .gitlab-ci.yml to publish the repos.
Finally, to publish the built package, we use the --cert parameter to pass the certificate
to poetry. Note: It uses the mounted path, not the installed one, which means that
installing the certificate might not be needed in the first place.
The publish job is triggered when the commit branch is named main:
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
Poetry can be used as a package manager and publisher, requiring a single file for
dependencies.
Pip, on the other hand, can only fetch & install dependencies. Building is done by using
Python's build package, and publishing by using Twine.
Case 1: Testing
PIP
Let's consider a case where a repo has requirements both from PyPI (trimesh) and from
chidos (meshmedley, geometry). The associated requirements.txt is:
--index-url https://pyramida.3-
psi.com/api/v4/projects/81/packages/pypi/simple
geometry
meshmedley
trimesh
Here, we used the --index-url setting to make pip default to the private package registry (we
use the URL in this case). Whenever pip cannot find a package in the default registry, it
looks into PyPI. To install those dependencies, we use the command
Here, we skip SSL certificate validation for our package registry (--trusted host flag, only
for pyramida.3-psi.com). As of now, this is the only way to install packages from our own
private package registry.
POETRY
Poetry can be used both interactively and using a pyproject.toml file. Let's consider the
latter.
[tool.poetry]
name = "your-project-name"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
[[tool.poetry.source]]
name = "pypi"
priority = "primary"
[[tool.poetry.source]]
name = "chidos"
url = "https://pyramida.3-psi.com/api/v4/projects/81/packages/pypi/simple"
priority = "explicit"
[tool.poetry.dependencies]
python = "^3.9"
numpy = "^1.21"
meshmedley = {version ="^1.0", source = "chidos"}
geometry = {version ="^1.0", source = "chidos"}
[tool.poetry.dev-dependencies]
ruff = "^0.3"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
sources, which are the registries where poetry will look for the dependencies. PyPI is
primary, chidos is explicit, i.e. it is only used when the dependency explicitly requires it.
tool.poetry.dependencies list the main dependencies of the project. As you may see,
we can explicitly define which source to use for each package.
tool.poetry.dev-dependencies is another "group" of dependencies. Poetry lets us
define as many groups as we want. For instance, in this case, it makes sense to install
ruff ( a linting-formatting tool) when developing the repo, but not on production or on
testing environments. We can do so by