Skip to content

GitLab CI

GitLab 8.0 부터는 GitLab-ci가 GitLab에 통합되었다.

Categories

Examples

Pipeline

Gitlab_cicd_pipeline_infograph.png

Keywords

키워드

설명

하위 키워드

after_script

작업 후 실행되는 명령 집합을 재정의합니다.

allow_failure

작업 실패를 허용합니다. 실패한 작업으로 인해 파이프 라인이 실패하지 않습니다.

artifacts

성공시 작업에 첨부 할 파일 및 디렉토리 목록입니다.

artifacts:paths, artifacts:exclude, artifacts:expose_as, artifacts:name, artifacts:untracked, artifacts:when, artifacts:expire_in, artifacts:reports

before_script

작업 전에 실행되는 명령 집합을 재정의합니다.

cache

후속 실행 사이에 캐시되어야하는 파일 목록입니다.

cache:paths, cache:key, cache:untracked, cache:policy

coverage

주어진 작업에 대한 코드 검사 설정.

dependencies

아티팩트를 가져올 작업 목록을 제공하여 특정 작업에 전달되는 아티팩트를 제한하십시오.

environment

작업이 배포되는 환경의 이름입니다.

environment:name, environment:url, environment:on_stop, environment:auto_stop_in, environment:action

except

작업이 생성되지 않는시기를 제어합니다.

extends

이 작업이 상속하는 구성 항목입니다.

image

Docker 이미지를 사용하십시오.

image:name, image:entrypoint

include

외부 YAML 파일을 포함합니다.

include:local, include:file, include:template, include:remote

inherit

모든 작업이 상속하는 전역 기본값을 선택합니다.

interruptible

새로운 실행으로 중복 될 때 작업을 취소 할 수 있는지 여부를 정의합니다.

needs

단계 순서보다 일찍 작업을 실행하십시오.

only

작업 생성시기를 제어합니다.

pages

GitLab 페이지에서 사용할 작업 결과를 업로드합니다.

parallel

병렬로 실행해야하는 작업 인스턴스 수입니다.

release

러너에게 릴리스 객체 를 생성하도록 지시 합니다.

resource_group

작업 동시성을 제한합니다.

retry

실패시 작업을 자동 재 시도 할 수있는시기 및 횟수입니다.

rules

작업의 선택한 속성 및 생성 여부를 평가하고 결정하기위한 조건 목록입니다. only/except 와 함께 사용 불가

script

러너가 실행하는 쉘 스크립트입니다.

secrets

CI / CD는 작업에 필요한 비밀입니다.

services

Docker 서비스 이미지를 사용하십시오.

services:name, services:alias, services:entrypoint, services:command

stage

작업 단계를 정의합니다.

tags

러너를 선택하는 데 사용되는 태그 목록입니다.

timeout

프로젝트 전체 설정보다 우선하는 사용자 지정 작업 수준 제한 시간을 정의합니다.

trigger

다운 스트림 파이프 라인 트리거를 정의합니다.

variables

작업 수준에서 작업 변수를 정의합니다.

when

작업 실행시기.

when:manual, when:delayed

Simple example

해당 Project의 .gitlab-ci.yml파일에 관련 설정을 추가하면 된다.

image: ubuntu:18.04

stages:
  - preview
  - build
  - test
  - deploy
  - review

default-preview:
  stage: preview
  script:
    - "echo Current directory: $PWD"
    - "echo Directory listing:"
    - "ls -la"

default-build:
  stage: build
  script:
    - "echo Empty build stage"

default-test:
  stage: test
  script:
    - "echo Empty test stage"

default-deploy:
  stage: deploy
  script:
    - "echo Empty deploy stage"

default-review:
  stage: review
  script:
    - "echo Empty review stage"

Tag only

only에 추가하는 방법:

my-job:
  stage: preview
  only:
    - tags

rules에 조건으로 추가하는 방법:

my_job:
  rules:
    - if: $CI_COMMIT_BRANCH == 'master' && $CI_COMMIT_TAG != null

Enable Debug logging

To enable debug logging (tracing), set the CI_DEBUG_TRACE variable to true:

job_name:
  variables:
    CI_DEBUG_TRACE: "true"

Multiline scripts

|(YAML#Literal Block Scalar)는 개행 마다 새로운 명령으로 처리된다.

job:
  script:
    - |
      echo "First command line."
      echo "Second command line."
      echo "Third command line."

>(YAML#Folded Block Scalar)는 빈 라인이 존재해야 새로운 명령으로 처리된다.

job:
  script:
    - >
      echo "First command line
      is split over two lines."

      echo "Second command line."

recc example

image: python:3.7

services:
  - postgres:13
  - redis:6
  - name: "docker.elastic.co/elasticsearch/elasticsearch:7.11.1"
    alias: "elasticsearch"
    command: [ "bin/elasticsearch", "-Expack.security.enabled=false", "-Ediscovery.type=single-node" ]

variables:
  POSTGRES_USER: "recc"
  POSTGRES_PASSWORD: "0000"
  POSTGRES_HOST_AUTH_METHOD: "trust"

stages:
  - test
  - deploy-python
  - deploy-docker

test-core:
  stage: test
  variables:
    RECC_TEST_HTTP_HOST: 0.0.0.0
    RECC_TEST_HTTP_PORT: 10000
    RECC_TEST_DB_HOST: postgres
    RECC_TEST_DB_PORT: 5432
    RECC_TEST_DB_USER: recc
    RECC_TEST_DB_PW: "0000"
    RECC_TEST_DB_NAME: testdb
  only:
    - master
    - '/^core-.*$/'
  before_script:
    - bash core/requirements.sh
  script:
    - cd core
    - bash docker-tester.sh
  coverage: '/\d+\%\s*$/'

test-fe:
  image: node:14.15
  stage: test
  only:
    - master
    - '/^fe-.*$/'
  before_script:
    - apt-get -qq update
    - apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb
  script:
    - cd fe
    - yarn
    - yarn test:unit
    - yarn test:e2e
    - yarn lint

deploy-core-python:
  stage: deploy-python
  only:
    - '/^core-.*$/'
  before_script:
    - bash core/requirements.sh
    - pip install devpi-client
  script:
    - cd core
    - devpi use "${DEVPI_URL}"
    - devpi login --password "${DEVPI_PW}" "${DEVPI_ID}"
    - devpi upload --with-docs --formats bdist_wheel
    - devpi logout

deploy-core-docker:
  stage: deploy-docker
  only:
    - '/^core-.*$/'
  before_script:
    - apt-get -qq update
    - apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
    - curl -fsSL "https://download.docker.com/linux/debian/gpg" | apt-key add -
    - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
    - apt-get -qq update
    - apt-get install -y docker-ce-cli
  script:
    - cd core
    - RECC_VERSION=$(python -c 'from recc.util.version import version_text; print(version_text);')
    - RECC_IMAGE_CURRENT=${REGISTRY_HOST}/answer/recc:${RECC_VERSION}
    - RECC_IMAGE_LATEST=${REGISTRY_HOST}/answer/recc:latest
    - echo "Current recc version is ${RECC_IMAGE_CURRENT}"
    - echo "${REGISTRY_PW}" | docker login --password-stdin -u "${REGISTRY_ID}" "${REGISTRY_HOST}"
    - docker build --tag ${RECC_IMAGE_CURRENT} .
    - docker tag "${RECC_IMAGE_CURRENT}" "${RECC_IMAGE_LATEST}"
    - docker push "${RECC_IMAGE_CURRENT}"
    - docker push "${RECC_IMAGE_LATEST}"
    - docker rmi "${RECC_IMAGE_CURRENT}"
    - docker rmi "${RECC_IMAGE_LATEST}"
    - docker logout

How to install GitLab CI

CentOS7에서 설치하는 방법은 아래와 같다:

$ sudo gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
## GitLab CI 주소를 입력한다. (예: http://your.domain.com/ci)
Please enter the gitlab-ci token for this runner:
## GitLab CI에 접근하기 위한 토큰을 입력한다. (예: 8e9e7eb3e6adfba5aaf9)
## 토큰은 GitLab에서 확인 가능하다.
Please enter the gitlab-ci description for this runner:
## Runner에 대한 설명을 적는다.
Please enter the gitlab-ci tags for this runner (comma separated):
## Runner에 TAG를 적용한다.
INFO[0023] 849a78b3 Registering runner... succeeded
Please enter the executor: docker, docker-ssh, ssh, shell, parallels:
## 실행기를 설정한다.
INFO[0032] Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

상세한 내용은 다음과 같이 정리한다: Gitlab_ci_-_docker_runner_setting.pdf

Caching

Inherit global configuration, but override specific settings per job

You can override cache settings without overwriting the global cache by using anchors. For example, if you want to override the policy for one job:

cache: &global_cache
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - public/
    - vendor/
  policy: pull-push

job:
  cache:
    # inherit all global cache settings
    <<: *global_cache
    # override the policy
    policy: pull

Caching Python dependencies

Assuming your project is using pip to install the Python dependencies, the following example defines cache globally so that all jobs inherit it. Python libraries are installed in a virtual environment under venv/, pip’s cache is defined under .cache/pip/ and both are cached per-branch:

#
# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Python.gitlab-ci.yml
#
image: python:latest

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python -V               # Print out python version for debugging
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
  script:
    - python setup.py test
    - pip install flake8
    - flake8 .

Caching Node.js (yarn, npm) dependencies

npm case:

#
# https://gitlab.com/gitlab-org/gitlab/tree/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml
#
image: node:latest

# Cache modules in between jobs
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .npm/

before_script:
  - npm ci --cache .npm --prefer-offline

test_async:
  script:
    - node ./specs/start.js ./specs/async.spec.js

yarn case:

variables:
  YARN_CACHE_FOLDER: "$CI_PROJECT_DIR/.cache/yarn"

cache:
  paths:
    - .cache/yarn

Variables

...

Troubleshooting

use elasticsearch in gitlab-ci

Elasticsearch#use elasticsearch in gitlab-ci 항목 참조.

Your runner is outdated, please upgrade your runner

GitLab CI 에서 실패가 떠서 들어가 보면 다음과 같은 에러가 출력될 수 있다.

Your runner is outdated, please upgrade your runner

즉, Runner 를 업그레이드 해야 한다.

See also

Favorite site

Documentation

Guide

References


  1. Gitlab-ci-multi-runner_advanced-configuration.pdf 

  2. Advanced configuration 

  3. GitLab_Documentation_-_Quick_Start.pdf 

  4. GitLab_Documentation_-Configuration_of_your_builds_with.gitlab-ci.yml.pdf