Automation using Gitlab CI

 

 

Nils Werner, Lorenz Schmidt

nils.werner@audiolabs-erlangen.com

 

International Audio Laboratories Erlangen

Interactive Workshop

Content

Motivation

Gitlab CI Basics

Examples

Motivation

Many tasks in programming are

Boring tasks

Automation to the rescue

In modern workflows

Gitlab CI Basics

Basic Terminology

Gitlab CI Infrastructure

Setting up Gitlab CI

  1. Create .gitlab-ci.yml file
  2. Push to Gitlab
  3. Profit

Creating a .gitlab-ci.yml file

What data do you need?

.gitlab-ci.yml Example

# .gitlab-ci.yml
test:3.8:                     # Task name
  image: python:3.8           # Task environment (Docker image)
  before_script:              # Task environment setup
    - apt-get install -yy libsndfile1-dev
    - pip install -U pip wheel
    - pip install -e .
  script:                     # Task execution
    - py.test

Advanced Features

Multiple parallel tasks

test:3.8:
  image: python:3.8
  script:
  # ...

test:3.9:
  image: python:3.9
  script:
  # ...

Multiple sequential tasks

stages:
  - tests
  - docs

test:3.8:
  stage: tests
  script:
  # ...

docs:
  stage: docs
  script:
  # ...

Saving artifacts

build:
  # ...

  artifacts:
    paths:
      - ClassicThesis.pdf

Reusing artifacts

build:
  # ...

  artifacts:
    paths:
      - ClassicThesis.pdf

test:
  # ...

  dependencies:
    - build           # `ClassicThesis.pdf` will be available

Environment Variables

variables:                  # Environment variables set in code
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

test:3.8:
   # ...

   script:
     - pip install numpy    # pip uses the variable $PIP_CACHE_DIR by default

Secret Variables

test:3.8:
   # ...

   script:
     - python $SECRET        # Defined in Gitlab settings, not in code

Runner selection

train:
    tags:                     # Runners with all tags will run this
      - cuda11
      - gtx1080
    # ...

    script:
      - python train.py

Website publishing

pages:                           # task name `pages` is important
  # ...                          # script must produce output in public/

  artifacts:
    paths:
    - public

Your repo https://git.audiolabs.uni-erlangen.de/$user/$repo/

Will be visible under https://$user.pages.audiolabs.uni-erlangen.de/$repo/

Reusing definitions

.job_template: &job_definition
  # ...
  script:
    - py.test

test:3.8:
  image: python:3.8
  <<: *job_definition

test:3.9:
  image: python:3.9
  <<: *job_definition

Caching

cache:
  paths:
    - downloaded-data/  # Will be restored on subsequent runs

test:3.8:
  image: python:3.8
  script:
  # ...

Run conditions

test:3.8:
  image: python:3.8
  script:
  # ...

  only:
  - master

Only runs on the master branch

Examples

Best practices

How to get started

# .gitlab-ci.yml
test:3.8:                     # Task name
  image: python:3.8           # Task environment (Docker image)
  before_script:              # Task environment setup
    - apt-get install -yy libsndfile1-dev
    - pip install -U pip wheel
    - pip install -e .
  script:                     # Task execution
    - py.test

Read More

https://docs.gitlab.com/ee/ci/
https://git.audiolabs.uni-erlangen.de/nils/ci-examples

Contact Me:

nils.werner@​audiolabs-erlangen.com

@nlswrnr

https://www.audiolabs-erlangen.de/fau/assistant/werner

Thank you!

Discussion points