.gitlab-ci.yml 6.91 KB
Newer Older
1 2 3 4 5 6 7 8 9
image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_HOST: tcp://localhost:2375


10

11 12
build:
  stage: build
13
  variables:
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
14
    RUN_TEST: "1"
15 16 17 18 19 20 21 22 23
    POSTGRES_HOST: "db-test"
    STORAGE_SUFFIX: "-test"
    POSTGRES_PASSWORD: "example"
    DOCKER_HOST: tcp://localhost:2375
    LC_COLLATE: POSIX
  services:
    - docker:dind
    - name: postgres:10.1
      alias: db-test
24
  script:
25
    - if [ $CI_COMMIT_REF_SLUG == "master" ]; then export RUN_TEST="1"; fi
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
26
#    - export
27
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
28
    # pull the latest build on master
29
    - docker pull "$CI_REGISTRY_IMAGE:latest" || true
30
    # pull the latest build on this branch
31
    - docker pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" || true
32
    # build the image while passing commit SHA and tagging the image with it
33 34 35 36 37
    - docker build
      --build-arg CI_COMMIT_REF_SLUG
      --build-arg CI_COMMIT_SHA
      --build-arg CI_COMMIT_SHORT_SHA
      --cache-from "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
38
      --cache-from "$CI_REGISTRY_IMAGE:latest"
39 40
      --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
      --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
41
      --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
42
      ./src/viralhostrange/
43
    # Test if the build image is passing all the tests
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
44
    - mkdir persistent_volume && chmod 777 persistent_volume
45 46 47 48
    - if [ $RUN_TEST == "1" ]; then apk update && apk add postgresql-client; fi
    - if [ $RUN_TEST == "1" ]; then until pg_isready -h ${POSTGRES_HOST}; do echo waiting; sleep 2; docker ps; done; fi
#    - exit 1
    - if [ $RUN_TEST == "1" ]; then docker run --network host -v $(pwd)/persistent_volume:/code/persistent_volume -e "STORAGE_SUFFIX=$STORAGE_SUFFIX" -e "POSTGRES_PASSWORD=$POSTGRES_PASSWORD" "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" test; fi
49
    - if [ $RUN_TEST == "1" ]; then mv persistent_volume/htmlcov ./htmlcov; fi
50 51
    # Tests have been passed, so pushing
    # if we are on master branch, tag the build with latest and push it
52
    - if [ $CI_COMMIT_REF_SLUG == "master" ]; then docker tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA" "$CI_REGISTRY_IMAGE:latest"; docker push "$CI_REGISTRY_IMAGE:latest"; fi
53
    # push with the tag of the branch name
54
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
55
      # push with the tag of the commit sha
56
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
57 58
      # push with the tag of the commit short sha
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
59 60
  tags:
    - k8s
61 62
  artifacts:
    paths:
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
63 64
    - htmlcov
    expire_in: 1 week
65 66


67 68

deploy_prod:
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
69 70
  only:
    - master
71
  stage: deploy
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
72 73 74
  image: registry-gitlab.pasteur.fr/dsi-tools/docker-images:docker_kubernetes_image
  variables:
    CI_DEBUG_TRACE: "false"
75
    NAMESPACE: "viralhostrange-prod"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
76
    PUBLIC_URL: "viralhostrangedb.pasteur.cloud"
77
    STORAGE_SUFFIX: "-${CI_COMMIT_REF_SLUG}"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
78
    DEBUG: "True"
79
    DISPOSABLE: "Never"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
80
  environment:
81
    name: "viralhostrange-prod/$CI_COMMIT_REF_SLUG"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
82
    url: "https://viralhostrangedb.pasteur.cloud"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
83
  script:
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
84
    - echo $NAMESPACE
85
    - echo $CI_COMMIT_REF_SLUG
86
    - echo $STORAGE_SUFFIX
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
87
    - kubectl get pods -n $NAMESPACE
88
    - yum install gettext -y
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
89
    - kubectl delete secret registry-gitlab -n $NAMESPACE --ignore-not-found=true
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
90
    - kubectl create secret docker-registry -n $NAMESPACE registry-gitlab --docker-server=$CI_REGISTRY --docker-username=$DEPLOY_USER --docker-password=$DEPLOY_TOKEN --docker-email=$GITLAB_USER_EMAIL
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
91 92 93 94
    - envsubst < k8s/config-map.yaml | kubectl apply -n $NAMESPACE -f -
    - envsubst < k8s/kubernetes-storage.yaml | kubectl apply -n $NAMESPACE -f -
    - envsubst < k8s/manifest-postgres.yaml | kubectl apply -n $NAMESPACE -f -
    - envsubst < k8s/manifest.yaml | kubectl apply -n $NAMESPACE -f -
95
    # - envsubst < k8s/manifest-webhost-autoscale.yaml | kubectl apply -n $NAMESPACE -f -
96
    - envsubst < k8s/kubernetes-cronjob.yaml | kubectl apply -n $NAMESPACE -f -
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
97 98
  tags:
    - k8s
99

Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
100 101


102 103 104
deploy_dev:
  except:
    - master
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
105 106 107 108
  stage: deploy
  image: registry-gitlab.pasteur.fr/dsi-tools/docker-images:docker_kubernetes_image
  variables:
    CI_DEBUG_TRACE: "false"
109
    NAMESPACE: "viralhostrange-dev"
110 111
    PUBLIC_URL: "viralhostrangedb-${CI_COMMIT_REF_SLUG}.pasteur.cloud"
    STORAGE_SUFFIX: "-${CI_COMMIT_REF_SLUG}"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
112
    DEBUG: "True"
113
    DISPOSABLE: "Always"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
114
  environment:
115 116
    name: "viralhostrange-dev/$CI_COMMIT_REF_SLUG"
    url: "https://viralhostrangedb-${CI_COMMIT_REF_SLUG}.pasteur.cloud"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
117
    on_stop: stop_and_delete_in_dev
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
118
  script:
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
119
    - echo $NAMESPACE
120
    - echo $CI_COMMIT_REF_SLUG
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
121
    - echo $STORAGE_SUFFIX
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
122
    - kubectl get pods -n $NAMESPACE
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
123 124 125
    - yum install gettext -y
    - kubectl delete secret registry-gitlab -n $NAMESPACE --ignore-not-found=true
    - kubectl create secret docker-registry -n $NAMESPACE registry-gitlab --docker-server=$CI_REGISTRY --docker-username=$DEPLOY_USER --docker-password=$DEPLOY_TOKEN --docker-email=$GITLAB_USER_EMAIL
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
126 127 128 129 130
    - . ./k8s/init_db_from_prod.sh
    - envsubst < k8s/config-map.yaml | kubectl apply -n $NAMESPACE -f -
    - envsubst < k8s/kubernetes-storage.yaml | kubectl apply -n $NAMESPACE -f -
    - envsubst < k8s/manifest-postgres.yaml | kubectl apply -n $NAMESPACE -f -
    - envsubst < k8s/manifest.yaml | kubectl apply -n $NAMESPACE -f -
131
    # - envsubst < k8s/manifest-webhost-autoscale.yaml | kubectl apply -n $NAMESPACE -f -
132
    - envsubst < k8s/kubernetes-cronjob.yaml | kubectl apply -n $NAMESPACE -f -
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
133 134 135
  tags:
    - k8s

136 137


Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
138
stop_and_delete_in_dev:
139 140
  except:
    - master
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
141 142
  stage: deploy
  when: manual
143
  image: registry-gitlab.pasteur.fr/dsi-tools/docker-images:docker_kubernetes_image
144
  variables:
145
    GIT_STRATEGY: none # important to not checkout source when branch is deleted
146
    NAMESPACE: "viralhostrange-dev"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
147
  environment:
148
    name: "viralhostrange-dev/$CI_COMMIT_REF_SLUG"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
149
    action: stop
150
  script:
151
    - echo "Removing $CI_COMMIT_REF_SLUG"
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
152
    - kubectl --namespace=$NAMESPACE --wait=true delete hpa -l branch=branch-$CI_COMMIT_REF_SLUG
153
    - kubectl --namespace=$NAMESPACE --wait=true delete deploy,replicasets -l branch=branch-$CI_COMMIT_REF_SLUG -l role=front
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
154
    - kubectl --namespace=$NAMESPACE --wait=true delete deploy,replicasets -l branch=branch-$CI_COMMIT_REF_SLUG
155
    - kubectl --namespace=$NAMESPACE --wait=true delete cronjob -l branch=branch-$CI_COMMIT_REF_SLUG
156 157 158 159
    - kubectl --namespace=$NAMESPACE --wait=true delete ing,svc -l branch=branch-$CI_COMMIT_REF_SLUG
    - kubectl --namespace=$NAMESPACE --wait=true delete po -l branch=branch-$CI_COMMIT_REF_SLUG
    - kubectl --namespace=$NAMESPACE --wait=true delete pvc -l branch=branch-$CI_COMMIT_REF_SLUG
    - kubectl --namespace=$NAMESPACE --wait=true delete secrets -l branch=branch-$CI_COMMIT_REF_SLUG
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
160
    - kubectl --namespace=$NAMESPACE --wait=true delete configmaps -l branch=branch-$CI_COMMIT_REF_SLUG
161 162 163
  tags:
    - k8s

Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
164

Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  
pages:
  image: python:3.5
  stage: deploy
  script:
  - pip install -r doc/requirements.txt
  - cd doc
  - make html
  - mv _build/html/ ../public
  artifacts:
    paths:
    - public
  only:
  - master