.gitlab-ci.yml 4.55 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
50
build_dev_version:
  stage: build
  except:
    - tags
Pablo Panero's avatar
Pablo Panero committed
51
    - master
52
  environment: staging
53
54
  tags:
    - docker-image-build
Pablo Panero's avatar
Pablo Panero committed
55
  script: 'echo "Building Dev/QA Docker image..."'
56
57
  variables:
    BUILD_ARG: build_devel=1
58

59
60
61
62
63
64
65
66
67
68
########## PROD ##########
build_prod_version:
  stage: build
  only:
    - master
  environment: master
  tags:
    - docker-image-build
  script: 'echo "Building Prod Docker image..."'

69
########## ALL ##########
70
71
72
73
74
75
76
### 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
77
  script: 'echo "Building Docker image..."'
78
79
  variables:
    TO: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
80

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

105
########## DEV ##########
106
### Import image into OpenShift. Import $CI_COMMIT_TAG if present or 'latest' if not.
107
import_image_dev:
108
109
  stage: import_image
  environment: staging
110
111
112
113
  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:
114
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
115
116
117
118
119
120
    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:
121
122
123
124
125
126
127
  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}
128
129
130
  variables:
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_PROD}
131
    NAMESPACE: ${NAMESPACE_PROD}
132
133
134
135
136
137
138
139

########## PROD ##########
deploy_production:
  stage: deploy
  environment: production
  only:
  - tags
  when: manual
140
141
  image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
  script:
142
    - oc --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE} tag ${RESOURCE}:${CI_COMMIT_TAG} ${RESOURCE}:stable
143
  variables:
144
145
    TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
    OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_PROD}
146
    NAMESPACE: ${NAMESPACE_PROD}
147
    GIT_STRATEGY: none