.gitlab-ci.yml 7.12 KB
Newer Older
David Mendez's avatar
David Mendez committed
1 2 3
variables:
  DOCKER_TLS_CERTDIR: ""
  GIT_STRATEGY: clone
4 5
  REGISTRY_USER: chembl/research
  APPLICATION: lina-web
David Mendez's avatar
David Mendez committed
6
  SERVER_IMAGE_TAG: ${CI_REGISTRY}/${REGISTRY_USER}/${APPLICATION}:${CI_COMMIT_SHORT_SHA}
David Mendez's avatar
David Mendez committed
7 8 9
  RELEASE_IMAGE: $CI_REGISTRY/$REGISTRY_USER/$APPLICATION:$CI_BUILD_REF_NAME
  DOCKER_IMAGE: $CI_REGISTRY/$REGISTRY_USER/$APPLICATION
  DOCKER_DRIVER: overlay
10 11
  CONFIGS_FOLDER: run_config
  # Config staging
12
  ENV_STAGING_FILE_PATH: ${CONFIGS_FOLDER}/staging/.env
13
  INGRESS_STAGING_FILE_PATH: ${CONFIGS_FOLDER}/staging/ingress.yml
14 15 16 17 18 19
  # Config prod hx
  ENV_PROD_HX_FILE_PATH: ${CONFIGS_FOLDER}/prod_hx/.env
  INGRESS_PROD_HX_FILE_PATH: ${CONFIGS_FOLDER}/prod_hx/ingress.yml
  # Config prod hh
  ENV_PROD_HH_FILE_PATH: ${CONFIGS_FOLDER}/prod_hh/.env
  INGRESS_PROD_HH_FILE_PATH: ${CONFIGS_FOLDER}/prod_hh/ingress.yml
David Mendez's avatar
David Mendez committed
20 21 22

stages:
  - build
David Mendez's avatar
David Mendez committed
23
  - test
24
  - prepare_config
David Mendez's avatar
David Mendez committed
25
  - deploy
David Mendez's avatar
David Mendez committed
26

27
build:
28 29 30 31
  image:
    name: docker/compose:1.25.5
    entrypoint: ["/bin/sh", "-c"]
  stage: build
32 33 34 35
  services:
    - docker:dind
  tags:
    - dind
36 37
  variables:
    DOCKER_HOST: tcp://docker:2375
David Mendez's avatar
David Mendez committed
38
  script:
39
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
David Mendez's avatar
David Mendez committed
40 41 42 43
    - echo Building ${SERVER_IMAGE_TAG}
    - echo building ${SERVER_IMAGE_TAG}
    - docker build --tag ${SERVER_IMAGE_TAG} .
    - docker push ${SERVER_IMAGE_TAG}
David Mendez's avatar
David Mendez committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

# build-prod:
#   image:
#     name: docker/compose:1.25.5
#     entrypoint: ["/bin/sh", "-c"]
#   stage: build
#   only:
#     - master
#     # - master
#   variables:
#     DOCKER_HOST: tcp://docker:2375
#   script:
#     - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
#     - echo Building ${DOCKER_IMAGE}:${CI_COMMIT_SHORT_SHA}
#     - docker-compose build production
#     - echo Deploying ${DOCKER_IMAGE}:${CI_COMMIT_SHORT_SHA}
#     - docker push ${DOCKER_IMAGE}:${CI_COMMIT_SHORT_SHA}    

David Mendez's avatar
David Mendez committed
62 63 64 65
unit-tests:
  stage: test
  script:
    - echo "ToDo UNIT TESTS"
David Mendez's avatar
David Mendez committed
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
prepare_config_staging:
  stage: prepare_config
  image:
    name: alpine/git
    entrypoint: ["/bin/sh", "-c"]
  only:
    - staging
  script:
    - set -x
    - rm -rf ${CONFIGS_FOLDER} || true
    - mkdir -p ${CONFIGS_FOLDER}
    - git clone https://${CONFIG_DEPLOY_GITLAB_USERNAME}:${CONFIG_DEPLOY_GITLAB_TOKEN}@${CONFIGURATIONS_REPO} ${CONFIGS_FOLDER}
  tags:
    - docker
  artifacts:

    paths:
84
      - ${ENV_STAGING_FILE_PATH}
85 86 87
      - ${INGRESS_STAGING_FILE_PATH}
      

David Mendez's avatar
David Mendez committed
88
deploy-staging:
David Mendez's avatar
David Mendez committed
89 90 91 92
  image:
    name: lachlanevenson/k8s-kubectl:latest
    entrypoint: ["/bin/sh", "-c"]
  only:
93
    - staging
David Mendez's avatar
David Mendez committed
94 95
  stage: deploy
  script:
96
    - set -x
David Mendez's avatar
David Mendez committed
97 98 99
    - echo "$KUBE_CA_PEM_HX" > "$(pwd)/kube.ca.pem"
    - kubectl config set-cluster ${KUBE_CLUS_NAME_HX} --server="${KUBE_URL_HX}" --certificate-authority="$(pwd)/kube.ca.pem"
    - kubectl config set-credentials ${KUBE_USER_HX} --token="${KUBE_TOKEN_HX}"
100 101 102 103
    - kubectl config set-context ${CHEMBL_NS_STAGING} --cluster=${KUBE_CLUS_NAME_HX} --user=${KUBE_USER_HX}
    - kubectl config use-context ${CHEMBL_NS_STAGING}
    - echo ${CHEMBL_NS_STAGING} ${KUBE_URL_HX} ${KUBE_CLUS_NAME_HX} ${KUBE_USER_HX}
    - kubectl get pods -n ${CHEMBL_NS_STAGING}
David Mendez's avatar
David Mendez committed
104
    - kubectl create secret generic ${RUN_CONFIG_SECRET_NAME_STAGING} --from-file=.env=${ENV_STAGING_FILE_PATH} -o yaml -n ${CHEMBL_NS_STAGING} --dry-run=client | kubectl replace -f -
David Mendez's avatar
David Mendez committed
105 106
    - cat ${ENV_STAGING_FILE_PATH}
    - echo ${CHEMBL_NS_STAGING} ${KUBE_URL_HX} ${KUBE_CLUS_NAME_HX} ${KUBE_USER_HX}
David Mendez's avatar
David Mendez committed
107
    - kubectl get pods -n ${CHEMBL_NS_STAGING}
David Mendez's avatar
David Mendez committed
108
    - sed -i "s~<SERVER_IMAGE_TAG>~${SERVER_IMAGE_TAG}~" k8s-deployment.yml
David Mendez's avatar
David Mendez committed
109 110 111 112
    - sed -i "s~<HTTP_PROXY>~${HTTP_PROXY_HX}~g" k8s-deployment.yml
    - sed -i "s~<HTTPS_PROXY>~${HTTPS_PROXY_HX}~g" k8s-deployment.yml
    - sed -i "s~<NO_PROXY>~${NO_PROXY_HX_DEFAULT}~g" k8s-deployment.yml
    - sed -i "s~<ENV_SECRET_NAME>~${RUN_CONFIG_SECRET_NAME_STAGING}~g" k8s-deployment.yml
113
    - cat k8s-deployment.yml
David Mendez's avatar
David Mendez committed
114
    - kubectl apply -n ${CHEMBL_NS_STAGING} -f k8s-deployment.yml
115 116 117 118
    - cat ${INGRESS_STAGING_FILE_PATH}
    - kubectl apply -n ${CHEMBL_NS_STAGING} -f ${INGRESS_STAGING_FILE_PATH}


David Mendez's avatar
David Mendez committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
prepare_config_prod:
  stage: prepare_config
  image:
    name: alpine/git
    entrypoint: ["/bin/sh", "-c"]
  only:
    - master
  script:
    - set -x
    - rm -rf ${CONFIGS_FOLDER} || true
    - mkdir -p ${CONFIGS_FOLDER}
    - git clone https://${CONFIG_DEPLOY_GITLAB_USERNAME}:${CONFIG_DEPLOY_GITLAB_TOKEN}@${CONFIGURATIONS_REPO} ${CONFIGS_FOLDER}
  tags:
    - docker
  artifacts:
134

David Mendez's avatar
David Mendez committed
135 136 137 138 139
    paths:
      - ${ENV_PROD_HX_FILE_PATH}
      - ${INGRESS_PROD_HX_FILE_PATH}
      - ${ENV_PROD_HH_FILE_PATH}
      - ${INGRESS_PROD_HH_FILE_PATH}
140

David Mendez's avatar
David Mendez committed
141

142 143 144 145 146 147 148 149
deploy-prod-hx:
  image:
    name: lachlanevenson/k8s-kubectl:latest
    entrypoint: ["/bin/sh", "-c"]
  stage: deploy
  only:
    - master
  script:
David Mendez's avatar
Typo  
David Mendez committed
150
    - set -x
151 152 153
    - echo "$KUBE_CA_PEM_HX" > "$(pwd)/kube.ca.pem"
    - kubectl config set-cluster ${KUBE_CLUS_NAME_HX} --server="${KUBE_URL_HX}" --certificate-authority="$(pwd)/kube.ca.pem"
    - kubectl config set-credentials ${KUBE_USER_HX} --token="${KUBE_TOKEN_HX}"
David Mendez's avatar
Typo  
David Mendez committed
154 155 156 157 158 159
    - kubectl config set-context ${CHEMBL_NS_PROD} --cluster=${KUBE_CLUS_NAME_HX} --user=${KUBE_USER_HX}
    - kubectl config use-context ${CHEMBL_NS_PROD}
    - echo ${CHEMBL_NS_PROD} ${KUBE_URL_HX} ${KUBE_CLUS_NAME_HX} ${KUBE_USER_HX}
    - kubectl get pods -n ${CHEMBL_NS_PROD}
    - kubectl create secret generic ${RUN_CONFIG_SECRET_NAME_PROD_HX} --from-file=.env=${ENV_PROD_HX_FILE_PATH} -o yaml -n ${CHEMBL_NS_PROD} --dry-run=client | kubectl apply -f -
    - cat ${ENV_PROD_HX_FILE_PATH}
David Mendez's avatar
David Mendez committed
160 161 162
    - echo ${CHEMBL_NS_PROD} ${KUBE_URL_HX} ${KUBE_CLUS_NAME_HX} ${KUBE_USER_HX}
    - kubectl get pods -n ${CHEMBL_NS_PROD}
    - sed -i "s~<SERVER_IMAGE_TAG>~${SERVER_IMAGE_TAG}~" k8s-deployment.yml
David Mendez's avatar
David Mendez committed
163 164 165
    - sed -i "s~<HTTP_PROXY>~${HTTP_PROXY_HX}~g" k8s-deployment.yml
    - sed -i "s~<HTTPS_PROXY>~${HTTPS_PROXY_HX}~g" k8s-deployment.yml
    - sed -i "s~<NO_PROXY>~${NO_PROXY_HX_DEFAULT}~g" k8s-deployment.yml
David Mendez's avatar
David Mendez committed
166 167 168 169 170
    - sed -i "s~<ENV_SECRET_NAME>~${RUN_CONFIG_SECRET_NAME_PROD_HX}~g" k8s-deployment.yml
    - cat k8s-deployment.yml
    - kubectl apply -n ${CHEMBL_NS_PROD} -f k8s-deployment.yml
    - cat ${INGRESS_PROD_HX_FILE_PATH}
    - kubectl apply -n ${CHEMBL_NS_PROD} -f ${INGRESS_PROD_HX_FILE_PATH}
171 172

  
David Mendez's avatar
David Mendez committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

# deploy-prod-hh:
#   image:
#     name: lachlanevenson/k8s-kubectl:latest
#     entrypoint: ["/bin/sh", "-c"]
#   stage: deploy
#   script:
#     - echo $KUBE_URL_HH
#     - echo "$KUBE_CA_PEM_HH" > "$(pwd)/kube.ca.pem"
#     - kubectl config set-cluster ${KUBE_CLUS_NAME_HH} --server="${KUBE_URL_HH}" --certificate-authority="$(pwd)/kube.ca.pem"
#     - kubectl config set-credentials ${KUBE_USER_HH} --token="${KUBE_TOKEN_HH}"
#     - kubectl config set-context ${KUBE_NAMESPACE_PROD} --cluster=${KUBE_CLUS_NAME_HH} --user=${KUBE_USER_HH}
#     - kubectl config use-context ${KUBE_NAMESPACE_PROD}
#     - echo ${KUBE_NAMESPACE_DEV} ${KUBE_URL_HH} ${KUBE_CLUS_NAME_HH} ${KUBE_USER_HH}
#     - kubectl get pods -n ${KUBE_NAMESPACE_PROD}
#     - sed -i "s~<VERSION>~${CI_COMMIT_SHORT_SHA}~" k8s-deployment.yml
#     - sed -i "s~<DOCKER_IMAGE>~${DOCKER_IMAGE}~" k8s-deployment.yml
#     - kubectl apply -n ${KUBE_NAMESPACE_PROD} -f k8s-deployment.yml
#   when: manual
#   only:
#     - master

# after_script:
#   - echo "End of script"