To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

.gitlab-ci.yml 3.46 KB
Newer Older
Bengt Giger's avatar
Bengt Giger committed
1
2
3
4
5
6
7
8
9
10
# This is a GitLab CI configuration to build the project as a docker image
# The file is generic enough to be dropped in a project containing a working Dockerfile
# Author: Florent CHAUVEAU <florent.chauveau@gmail.com>
# Mentioned here: https://blog.callr.tech/building-docker-images-with-gitlab-ci-best-practices/

# do not use "latest" here, if you want this to work in the future
image: docker:19.03.0-dind

stages:
  - build
Bengt Giger's avatar
Bengt Giger committed
11
  - push
Bengt Giger's avatar
Bengt Giger committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# Use this if your GitLab runner does not use socket binding
services:
  - docker:19.03.0-dind

variables:
  DOCKER_HOST: tcp://localhost:2375
  DOCKER_TLS_CERTDIR: ""

.docker-script-before: &docker-script-before
  # docker login asks for the password to be passed through stdin for security
  # we use $CI_JOB_TOKEN here which is a special token provided by GitLab
  - echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY

Build docker image:
  before_script:
    - *docker-script-before
  stage: build
  tags:
    - k8s-runner
  except:
    - tags
  script:
    # fetches the latest image (not failing if image is not found)
36
37
38
39
    # - docker pull $CI_REGISTRY_IMAGE:latest || true
    # We don't pull the image in this case, because 99% comes from the
    # prebuilt LaTeX image anyway.
    #
Bengt Giger's avatar
Bengt Giger committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    # builds the project, passing proxy variables, and vcs vars for LABEL
    # notice the cache-from, which is going to use the image we just pulled locally
    # the built image is tagged locally with the commit SHA, and then pushed to
    # the GitLab registry
    - >
      docker build
      --pull
      --build-arg http_proxy=$http_proxy
      --build-arg https_proxy=$https_proxy
      --build-arg no_proxy=$no_proxy
      --build-arg VCS_REF=$CI_COMMIT_SHA
      --build-arg VCS_URL=$CI_PROJECT_URL
      --build-arg VCS_TAG=$CI_COMMIT_SHORT_SHA
      --cache-from $CI_REGISTRY_IMAGE:latest
      --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
      .
Bengt Giger's avatar
Bengt Giger committed
56
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
Bengt Giger's avatar
Bengt Giger committed
57

58
59
# Tag the current build with tag 'latest'
Push latest:
60
61
  before_script:
    - *docker-script-before
62
63
64
65
66
67
68
69
70
71
72
  variables:
    # Again, we do not need the source code here. Just playing with Docker.
    GIT_STRATEGY: none
  stage: push
  tags:
    - k8s-runner
  script:
    - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
    - docker push $CI_REGISTRY_IMAGE:latest
    
Bengt Giger's avatar
Bengt Giger committed
73
74
75
# The goal here is to Docker tag any Git tag
# GitLab will start a new pipeline everytime a Git tag is created, which is pretty awesome
Push tag:
76
77
  before_script:
    - *docker-script-before
Bengt Giger's avatar
Bengt Giger committed
78
79
80
81
82
83
84
85
86
87
88
89
90
  variables:
    # Again, we do not need the source code here. Just playing with Docker.
    GIT_STRATEGY: none
  stage: push
  tags:
    - k8s-runner
  only:
    # We want this job to be run on tags only.
    - tags
  script:
    - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
91
92
93

# Images with version tag are also available as 'stable'
Push stable:
94
95
  before_script:
    - *docker-script-before
96
97
98
99
100
101
102
103
104
105
106
107
108
  variables:
    # Again, we do not need the source code here. Just playing with Docker.
    GIT_STRATEGY: none
  stage: push
  tags:
    - k8s-runner
  only:
    # We want this job to be run on tags only.
    - tags
  script:
    - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:stable
    - docker push $CI_REGISTRY_IMAGE:stable