.gitlab-ci.yml 4.72 KB
Newer Older
1

2
3
variables:

4
5
6
7
8
9
10
  ### Disable cache in Docker builds, as this has occasionally resulted in images not containing what was
  ### expected multiple MRs where being built/retried.
  NO_CACHE: 'true'
  ### Replace RESOURCE with the name of the image you want to build and publish in OpenShift
  ### Important! In order for this template to work, the name of the gitlab repo must match
  ### also the variable name
  RESOURCE: cern-search-rest-api
11
  ### OpenShift namespace and server values
12
13
  NAMESPACE_DEV: cern-search-master
  OPENSHIFT_SERVER_DEV: https://openshift.cern.ch
14
15
  NAMESPACE_PROD: cern-search-master
  OPENSHIFT_SERVER_PROD: https://openshift.cern.ch
16

17

18
19
20
### By default, there are 6 stages that we may use:
### Feel free to adapt this to your specific case.
stages:
21
  - build_base
22
  - build
23
24
25
  - tag_image
  - import_image # This stage is only used when the built image is stored in the GitLab Registry
  - deploy
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
### 'Build Base' stage
### Builds the base docker image and tags it according to the commit message.
build_base_image:
  stage: build_base
  only:
    changes:
      - Dockerfile-base
      - Pipfile
      - Pipfile.lock
  tags:
    - docker-image-build
  script: 'echo "Building base Docker image..."'
  variables:
    DOCKER_FILE: Dockerfile-base
    TO: gitlab-registry.cern.ch/webservices/cern-search/cern-search-rest-api/cern-search-rest-api-base:${CI_COMMIT_SHA}

43
44
45
### 'Build' stage
### Build the image and store it in the registry. It is important that this step
### doesn't override the image the applications are running, as we haven't tested the image yet
46
########## DEV ##########
47
48
49
build_dev_version:
  stage: build
  except:
50
51
52
53
54
55
56
    refs:
      - tags
      - master
    changes:
      - Dockerfile-base
      - Pipfile
      - Pipfile.lock
57
  environment: staging
58
59
  tags:
    - docker-image-build
Pablo Panero's avatar
Pablo Panero committed
60
  script: 'echo "Building Dev/QA Docker image..."'
61
62
  variables:
    BUILD_ARG: build_devel=1
63

64
65
66
67
68
69
70
71
72
73
########## PROD ##########
build_prod_version:
  stage: build
  only:
    - master
  environment: master
  tags:
    - docker-image-build
  script: 'echo "Building Prod Docker image..."'

74
########## ALL ##########
75
76
77
78
79
80
81
### When building tags, use the git tag as the docker tag of the image
build_tagged_version:
  stage: build
  only:
    - tags
  tags:
    - docker-image-build
82
  script: 'echo "Building Docker image..."'
83
84
  variables:
    TO: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
85

86
########## DEV ##########
Pablo Panero's avatar
Pablo Panero committed
87
### If a new tag is pushed it needs to be referenced into the ImageStream
88
tag_image_dev: &tag_image_openshift
89
90
91
92
93
94
  stage: tag_image
  only:
  - tags
  environment: staging
  image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
  script:
95
    - oc tag --source=docker ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG} ${RESOURCE}:${CI_COMMIT_TAG} --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE}
96
  variables:
97
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
98
    NAMESPACE: ${NAMESPACE_DEV}
99
100
101
102
103
104
105
106
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}

########## PROD ##########
### If a new tag is pushed it needs to be referenced into the ImageStream
tag_image_prod:
  <<: *tag_image_openshift
  variables:
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
107
    NAMESPACE: ${NAMESPACE_PROD}
108
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_PROD}
109

110
########## DEV ##########
111
### Import image into OpenShift. Import $CI_COMMIT_TAG if present or 'latest' if not.
112
import_image_dev:
113
  stage: import_image
114
115
116
117
118
  except:
    changes:
      - Dockerfile\-base
      - Pipfile
      - Pipfile.lock
119
  environment: staging
120
121
122
123
  image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
  script:
    - oc import-image ${RESOURCE}:${CI_COMMIT_TAG:-latest} --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE}
  variables:
124
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
125
126
127
128
129
130
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}
    NAMESPACE: ${NAMESPACE_DEV}

########## PROD ##########
### Import image into OpenShift. Import $CI_COMMIT_TAG if present or 'latest' if not.
import_image_prod:
131
132
133
134
135
136
137
  stage: import_image
  environment: master
  only:
  - tags
  image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
  script:
    - oc import-image ${RESOURCE}:${CI_COMMIT_TAG:-latest} --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE}
138
139
140
  variables:
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_PROD}
141
    NAMESPACE: ${NAMESPACE_PROD}
142
143
144
145
146
147
148
149

########## PROD ##########
deploy_production:
  stage: deploy
  environment: production
  only:
  - tags
  when: manual
150
151
  image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
  script:
152
    - oc --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE} tag ${RESOURCE}:${CI_COMMIT_TAG} ${RESOURCE}:stable
153
  variables:
154
155
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_PROD}
156
    NAMESPACE: ${NAMESPACE_PROD}
157
    GIT_STRATEGY: none