Skip to content

Instantly share code, notes, and snippets.

@Toflar
Last active November 27, 2020 19:57
Show Gist options
  • Save Toflar/8eab7ec29607a18f6d388a91133f9ffa to your computer and use it in GitHub Desktop.
Save Toflar/8eab7ec29607a18f6d388a91133f9ffa to your computer and use it in GitHub Desktop.
image: docker:stable
services:
- docker:dind
stages:
- build
- deploy
variables:
IMAGE: ${CI_REGISTRY_IMAGE}/web:${CI_COMMIT_REF_SLUG}
GKE_SERVICE_ACCOUNT: base64-encoded-json-service-account-file-from-gke
GKE_CLUSTER_NAME: cluster-name
GKE_ZONE: europe-west3-a
GKE_PROJECT: gke-project-name
URL_REVIEW: ${CI_COMMIT_REF_SLUG}.your.domain.com
URL_PRODUCTION: your.domain.com
build:
stage: build
script:
- docker login -u gitlab-ci-token -p ${CI_BUILD_TOKEN} ${CI_REGISTRY}
- docker pull ${IMAGE} || true
- docker build --cache-from ${IMAGE} -t ${IMAGE} -f ./Dockerfile .
- docker push ${IMAGE}
deploy_review:
stage: deploy
image: devth/helm
script:
- init_helm
- helm upgrade
--install
--set web.name="my-super-app-${CI_COMMIT_REF_SLUG}"
--set web.image="${IMAGE}"
--set web.host="${URL_REVIEW}"
--wait
--force
my-super-app-${CI_COMMIT_REF_SLUG}
./k8s-chart
except:
refs:
- master
deploy_production:
stage: deploy
image: devth/helm
script:
- init_helm
- helm upgrade
--install
--set web.name="my-super-app-${CI_COMMIT_REF_SLUG}"
--set web.image="${IMAGE}"
--set web.host="${URL_PRODUCTION}"
--wait
--force
my-super-app-${CI_COMMIT_REF_SLUG}
./k8s-chart
only:
refs:
- master
.functions: &functions |
# Functions
function init_helm() {
mkdir -p /etc/deploy
echo ${GKE_SERVICE_ACCOUNT} | base64 -d > /etc/deploy/sa.json
gcloud auth activate-service-account --key-file /etc/deploy/sa.json --project=${GKE_PROJECT}
gcloud container clusters get-credentials ${GKE_CLUSTER_NAME} --zone ${GKE_ZONE} --project ${GKE_PROJECT}
helm init --service-account tiller --wait --upgrade
}
before_script:
- *functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment