GitLab

字数 705 · 2021-07-02

使用 Wiki 方便实时更改

CI

The pipeline starts when the commit is committed.

Pipelines

Pipelines are the top-level component of CI/CD.

  • Pipelines
    • Jobs - define what to do
    • Stages - define when to run the jobs

Jobs are executed by runners.

Multiple jobs in the same stage are executed in parallel, if there are enough concurrent runners.

A typical pipeline might consist of four stages:

  • build stage
    • compile job
  • test stage
    • test1 job
    • test2 job
  • staging stage
    • deploy-to-stage job
  • production stage
    • deploy-to-prod job

Stages

default pipeline:

  • .pre
  • build
  • test
  • deploy
  • .post

if a job dose not specify a stage, the job is assigned the test stage.

Jobs

  • rules
    • if
    • only
    • except

workflow

Use workflow to control pipeline behavior.

Variables

Predefined

  • $CI_PIPELINE_SOURCE
    • trigger

https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

Cache

Use cache for dependencies, like packages you download from the internet.

Cache is stored where GitLab Runner is installed.

  • Define cache per job by using the cache keyword.
  • Subsequent pipelines can use the cache.
  • Subsequent jobs in the same pipeline can use the cache.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
# jobs in each branch use the same cache
cache:
  key: $CI_COMMIT_REF_SLUG
1
2
3
4
5
6
7
# Compute the cache key from the lock file
cache:
  key:
    files:
      - package-lock.json
  paths:
    - .npm/

Artifacts

Use artifacts to pass intermediate build results between stages.

Artifacts are generated by a job, stored in GitLab, and can be downloaded.

Runner

Install

1
2
3
4
docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

Register

1
docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

Page

Registry

if use HTTP, nginx should forward HTTPS to HTTP.

KAS

kubectl would never send credentials over HTTP.

1
2
3
4
5
6
gitlab_rails['gitlab_kas_external_url'] = 'ws://git.example/-/kubernetes-agent/'
gitlab_rails['gitlab_kas_internal_url'] = 'grpc://localhost:8153'

gitlab_rails['gitlab_kas_external_k8s_proxy_url'] = 'https://git.example/-/kubernetes-agent/k8s-proxy/'

gitlab_kas_external_url "https://git.example"
1
kubectl --insecure-skip-tls-verify get pods

Misc

1
2
3
4
Array.prototype.map.call(
    document.querySelectorAll('div.js-contrib-calendar rect.user-contrib-cell'),
    rect => +rect.getAttribute('title').split(' ')[0] || 0)
  .reduce((a, b) => a + b, 0)