Gitlab CI/CD Notes

GitLab CI/CD is among others (like Github) one service to build, release and deploy software. By default a pipeline to do anything is described in .gitlab-ci.yml in the root folder.

Simple YAML example

The following example describes how .NET code should be treated. The name build-job is randomly chosen. It uses an official microsoft docker image image: mcr.microsoft.com/dotnet/sdk:latest to build and belongs to the stage build-project. There can be more jobs in this stage.

  • The script part describes how the code is treated. It does the actual workload and contains commandline calls executed under a unix machine.
  • Actually 'echo GE_JOB_ID=$CI_JOB_ID >> generate_executables.env' is just here to send the GE_JOB_ID variable to other jobs.
  • The tags can be anything but certain tags have certain meaning. docker references a Docker Runner to execute the job.
  • artifacts are create, uploaded and kept forever so they can be displayed for downloading in the next job.
build-job:
  image: mcr.microsoft.com/dotnet/sdk:latest
  stage: build-project
  script:
    - 'dotnet restore $SOURCE_CODE_PATH/FileEncoding.sln --packages $NUGET_PACKAGES_DIR'
    - 'dotnet build $SOURCE_CODE_PATH/FileEncoding.csproj --packages $NUGET_PACKAGES_DIR --os win -c $BUILD_CONFIGURATION'
    - 'mv -f $BUILD_DIRECTORY/win-x64 $BUILD_CONFIGURATION'
    - 'echo GE_JOB_ID=$CI_JOB_ID >> generate_executables.env'
    - 'find .'
  tags:
    - 'docker'
  artifacts:
    name: 'artifact-$CI_JOB_ID'
    paths:
      - $BUILD_CONFIGURATION/*
    reports:
      dotenv: generate_executables.env
    expire_in: never

The release job serves the purpose to make given artifacts available as a release for downloading.

release-job:
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  stage: create-release
  when: manual
  needs:
    - job: build-job
      artifacts: true
  except:
    - tags
  script:
    - 'echo creating release v$CI_JOB_ID'
  release:
    tag_name: 'v$CI_JOB_ID'
    description: '$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG'
    assets:
      links:
        - name: 'Windows Binaries'
          url: 'https://gitlab.company.com/mfriese/sandbox/-/jobs/${GE_JOB_ID}/artifacts/download'

The list of stages brings order to jobs. Without it would not be clear which to serve first and which one comes next.

stages:
  - 'build-project'
  - 'create-release'

Variables should be obvious. A central place to configure certain settings so they can be reused and easily modified.

variables:
  NUGET_PACKAGES_DIR: '.nuget'
  SOURCE_CODE_PATH: 'FileEncoding'
  BUILD_CONFIGURATION: 'Release'
  BUILD_DIRECTORY: '$SOURCE_CODE_PATH/bin/$BUILD_CONFIGURATION/net6.0'

The cache serves as a storage that will be available in every job. Put files here to have them available in every build.

cache:
  key: '$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG'
  paths:
    - '$NUGET_PACKAGES_DIR'

Links & References