Gitlab CI-CD

Лев Коваленко — Senior DS Engineer

Gitlab runners

Gitlab pipelines

  • Branch pipelines
  • Merge request pipelines
  • Merge result pipelines
  • Merge trains

Executor

CI-CD pipeline

Файл /.gitlab-ci.yml:

stages:
  - build
  - test
  - deploy


default:
  image: alpine


build_a:
  stage: build
  script:
    - echo "This job builds something."


build_b:
  stage: build
  script:
    - echo "This job builds something else."


test_a:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."


test_b:
  stage: test
  script:
    - echo "This job tests something else. It will only run when all jobs in the"
    - echo "build stage are complete too. It will start at about the same time as test_a."


deploy_a:
  stage: deploy
  script:
    - echo "This job deploys something. It will only run when all jobs in the"
    - echo "test stage complete."
  environment: production


deploy_b:
  stage: deploy
  script:
    - echo "This job deploys something else. It will only run when all jobs in the"
    - echo "test stage complete. It will start at about the same time as deploy_a."
  environment: production

Keywords

Keywords Значение
stages Список основных стадий пайплайна
default Дефолтные параметры пайплайна
variables Переменные среды
image Docker образ для запуска
rules Правила исполнения запуска
cache Кеш, сохраняемый после задачи
before_script Действия перед исполнением скрипта
script Команды необходимые для исполнения задачи
after_script Действия после исполнения скрипта
artifacts Сохраняемые артефакты

Variables

variables:
  GLOBAL_VAR: "A global variable"


job1:
  variables:
    JOB_VAR: "A job variable"
  script:
    - echo "Variables are '$GLOBAL_VAR' and '$JOB_VAR'"


job2:
  script:
    - echo "Variables are '$GLOBAL_VAR' and '$JOB_VAR'"


job3:
  variables: {}
  script:
    - echo This job does not need any variables

Secret Variables

Rules

build-dev:
  stage: build
  script: echo "Feature branch, so building dev version..."


build-prod:
  stage: build
  script: echo "Default branch, so building prod version..."




test:
  needs: ['build-dev']
  interruptible: false
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      needs: ['build-prod']
      changes:
        - Dockerfile
      when: manual
      allow_failure: true
      interruptible: true
      variables:
        IS_A_FEATURE: "true"

Cache

cache-job:
  script:
    - echo "This job uses a cache."
  cache:
    key: dependencies-cache-$CI_COMMIT_REF_SLUG
    paths:
      - .poetry
cache-job:
  script:
    - echo "This job uses a cache."
  cache:
    key:
      files:
        - pyproject.toml
        - poetry.lock
    paths:
      - .poetry
    when: 'always'
cache-job:
  script:
    - echo "This job uses a cache."
  cache:
    untracked: true

Script

job:
  before_script:
    - echo "Execute this command before any 'script:' commands."
  script:
    - echo "An example script section."
  after_script:
    - echo "Execute this command after the `script` section completes."

Artifacts

publish-lib:
  artifacts:
    expire_in: never
    paths:
      - $CI_PROJECT_DIR/dist


pages:
  stage: pages
  script:
    - echo "Publish docs to gitlab pages"
    - cp -r docs  public
  artifacts:
    paths:
      - public