Skip to main content
Build status License PyPi package

Scripts for getting useful information (e.g. the git hash of the last successful build) from within Gitlab CI

Overview

Often when using Gitlab CI you need information that is not available from the environment variables provided by the CI runner. This package contains a collection of scripts for obtaining information from the Gitlab API

License

gitlab-ci-tools is licensed under the simplfied (2-clause) BSD licence. See the LICENSE file for details.

Installation

The scripts are written in Python 3, so this will need to be installed prior to the following:

pip3 install gitlab-ci-tools

You can install the package on your local computer to test out the scripts (see the Usage section for details), however typically you will want to install this package into the Gitlab CI environment.

The simplest way to accomplish this is to add the following to the top of your .gitlab-ci.yml:

before_script:
    - pip3 install gitlab-ci-tools

Usage

Installing gitlab-ci-tools installs a bunch of scripts that you can run from the command line. See the next section for a list of the installed tools.

In order for the scripts to be able to access the Gitlab API you must provide a CI secret variable GITLAB_API_TOKEN that contains a personal access token with api scope (check out these instructions to find out how to generate one).

Security Considerations

Unfortunately Gitlab’s API permissions are not very granular, so anyone with access to this token is, effectively, you (at least as far as Gitlab is concerned). If people you don’t trust have access to your CI (e.g. they can make CI-triggering commits to your repository) this is a bad idea. There are several outstanding issues on Gitlab (e.g. #29566 and #41084) that attempt to address the problem of authenticated API access from within CI runners.

List of Tools

last-good-build

Print the Git hash of the last successful build for the current branch. An useful example would be generating a PDF of the diff between two Latex documents:

before_script:
  pip install gitlab-ci-tools
  LAST_GOOD_BUILD=$(last-good-build || echo '')

report diff:
  script:
    - git show $LAST_GOOD_BUILD:important_doc.tex > old_doc.tex
    - latexdiff old_doc.tex important_doc.tex > diff_doc.tex
    - latexmk -pdf diff_doc.tex
  artifacts:
    paths:
      - diff_doc.pdf

Contributing

I would be happy to add more tools here, not just ones that I find useful. It would be good if the script were to follow a similar structure to the existing ones (i.e. provide a main() function, and expose a command line that can be driven with command-line options or with environment variables), but I am happy to guide anyone through this process if need be.

Once there is more than a single script we can start to think about how best to separate out common functionlity so that we can reduce boilerplate.

Metadata

Release files for gitlab-ci-tools 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gitlab-ci-tools 0.1.0
File Size Uploaded
gitlab-ci-tools-0.1.0.tar.gz 21.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gitlab-ci-tools 0.1.0
File Interpreter ABI Platform
gitlab_ci_tools-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.9 kB

Release files / gitlab-ci-tools-0.1.0.tar.gz

Download URL gitlab-ci-tools-0.1.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
52335836cec49ce13a443def989e0ad52f25fc664c5e2ef8750d039629861755
BLAKE2b-256 checksum
How to use checksums
d288c6fca2a306565802e5c3f7a26cdf6f86a5a5385e62facb0000133a987c30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / gitlab_ci_tools-0.1.0-py3-none-any.whl

Download URL gitlab_ci_tools-0.1.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d78f7993a84d5b3be801cbeccf8ffac8a68cf19ad624f7390a9360ea8f6babaa
BLAKE2b-256 checksum
How to use checksums
c52c1dc9ebea43d79f41bf832ce4c65ff49a7074341c06bbb3c1889674d9b9e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page