.gitlab-ci.yml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. image: roffe/kubectl:latest
  2. variables:
  3. CI_REGISTRY: parity.azurecr.io
  4. CI_REGISTRY_USER: parity
  5. AUTO_DEVOPS_DOMAIN: poc-3.polkadot.io
  6. .kubernetes: &kubernetes
  7. tags:
  8. - kubernetes
  9. stages:
  10. - dockerize
  11. - test
  12. - review
  13. - staging
  14. - production
  15. - cleanup
  16. before_script:
  17. - export DOCKER_IMAGE=$CI_REGISTRY/$CI_PROJECT_PATH_SLUG
  18. - export DOCKER_TAG=$CI_COMMIT_REF_SLUG-$VERSION
  19. - export DOCKER_IMAGE_FULL_NAME=$DOCKER_IMAGE:$DOCKER_TAG
  20. dockerize:
  21. stage: dockerize
  22. environment:
  23. name: infrastructure_build
  24. tags:
  25. - kubernetes-parity-build
  26. image: docker:git
  27. services:
  28. - docker:dind
  29. variables:
  30. DOCKER_DRIVER: overlay2
  31. DOCKER_HOST: tcp://localhost:2375
  32. script:
  33. - echo $DOCKER_IMAGE
  34. - echo $DOCKER_TAG
  35. - echo $DOCKER_IMAGE_FULL_NAME
  36. - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
  37. - docker build -t "$DOCKER_IMAGE_FULL_NAME" .
  38. - docker push "$DOCKER_IMAGE_FULL_NAME"
  39. only:
  40. - master
  41. review:
  42. stage: review
  43. <<: *kubernetes
  44. script:
  45. - setup_kubernetes
  46. - deploy
  47. environment:
  48. name: review/$CI_COMMIT_REF_NAME
  49. url: https://$CI_ENVIRONMENT_SLUG.$AUTO_DEVOPS_DOMAIN
  50. on_stop: stop_review
  51. only:
  52. refs:
  53. - branches
  54. kubernetes: active
  55. except:
  56. - master
  57. stop_review:
  58. stage: cleanup
  59. <<: *kubernetes
  60. variables:
  61. GIT_STRATEGY: none
  62. script:
  63. - setup_kubernetes
  64. - delete
  65. environment:
  66. name: review/$CI_COMMIT_REF_NAME
  67. action: stop
  68. when: manual
  69. allow_failure: true
  70. only:
  71. refs:
  72. - branches
  73. kubernetes: active
  74. except:
  75. - master
  76. staging:
  77. stage: staging
  78. <<: *kubernetes
  79. script:
  80. - setup_kubernetes
  81. - deploy
  82. environment:
  83. name: staging
  84. url: https://staging.$AUTO_DEVOPS_DOMAIN
  85. only:
  86. refs:
  87. - master
  88. kubernetes: active
  89. production:
  90. stage: production
  91. <<: *kubernetes
  92. script:
  93. - setup_kubernetes
  94. - deploy
  95. environment:
  96. name: production
  97. url: https://$AUTO_DEVOPS_DOMAIN
  98. when: manual
  99. only:
  100. refs:
  101. - master
  102. kubernetes: active
  103. # ---------------------------------------------------------------------------
  104. .auto_devops: &auto_devops |
  105. # Auto DevOps variables and functions
  106. [[ "$TRACE" ]] && set -x
  107. export DOCKER_IMAGE=$CI_REGISTRY/$CI_PROJECT_PATH_SLUG
  108. export DOCKER_TAG=$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
  109. export DOCKER_IMAGE_FULL_NAME=$DOCKER_IMAGE:$DOCKER_TAG
  110. export AUTODEVOPS_HOST=$(echo $CI_ENVIRONMENT_URL | awk -F/ '{print $3}')
  111. function build() {
  112. if [[ -n "$CI_REGISTRY_USER" ]]; then
  113. echo "Logging to GitLab Container Registry with CI credentials..."
  114. docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
  115. echo ""
  116. fi
  117. echo "Building Dockerfile-based application..."
  118. docker build -t "$DOCKER_IMAGE_FULL_NAME" .
  119. echo "Pushing to GitLab Container Registry..."
  120. docker push "$DOCKER_IMAGE_FULL_NAME"
  121. echo ""
  122. }
  123. function setup_kubernetes() {
  124. kubectl describe namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE"
  125. kubectl create secret -n "$KUBE_NAMESPACE" \
  126. docker-registry gitlab-registry \
  127. --docker-server="$CI_REGISTRY" \
  128. --docker-username="$CI_REGISTRY_USER" \
  129. --docker-password="$CI_REGISTRY_PASSWORD" \
  130. --docker-email="$GITLAB_USER_EMAIL" \
  131. -o yaml --dry-run | kubectl replace -n "$KUBE_NAMESPACE" --force -f -
  132. }
  133. function deploy() {
  134. cat ./deployment.template.yml | envsubst | kubectl apply -n "$KUBE_NAMESPACE" -f -
  135. }
  136. function delete() {
  137. kubectl -n "$KUBE_NAMESPACE" delete "deploy/$CI_ENVIRONMENT_SLUG-backend"
  138. kubectl -n "$KUBE_NAMESPACE" delete "svc/$CI_ENVIRONMENT_SLUG-service"
  139. kubectl -n "$KUBE_NAMESPACE" delete "ing/$CI_ENVIRONMENT_SLUG-ingress"
  140. }
  141. before_script:
  142. - *auto_devops