Skip to main content

Tools for working inside Gitlab CI

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gitlab-ci-tools-0.1.0.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

gitlab_ci_tools-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file gitlab-ci-tools-0.1.0.tar.gz.

File metadata

File hashes

Hashes for gitlab-ci-tools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 52335836cec49ce13a443def989e0ad52f25fc664c5e2ef8750d039629861755
MD5 23c681234049b00792937d6d41e8b37e
BLAKE2b-256 d288c6fca2a306565802e5c3f7a26cdf6f86a5a5385e62facb0000133a987c30

See more details on using hashes here.

File details

Details for the file gitlab_ci_tools-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gitlab_ci_tools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d78f7993a84d5b3be801cbeccf8ffac8a68cf19ad624f7390a9360ea8f6babaa
MD5 68e23cf30e5156e78951e10fe1555f9c
BLAKE2b-256 c52c1dc9ebea43d79f41bf832ce4c65ff49a7074341c06bbb3c1889674d9b9e7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page