Skip to content

Instantly share code, notes, and snippets.

@mousetree
Created July 10, 2018 16:18
Show Gist options
  • Save mousetree/d8308b4eb8c6c38b0760aba6f4c2b5ef to your computer and use it in GitHub Desktop.
Save mousetree/d8308b4eb8c6c38b0760aba6f4c2b5ef to your computer and use it in GitHub Desktop.
CircleCI v2.0 config for deployment to Google Kubernetes Engine (GKE)
version: 2
jobs:
build_and_test:
docker:
- image: circleci/node:10
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run:
name: Install dependencies
command: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run:
name: Run tests
command: npm test
- store_test_results:
path: test-results
deploy_to_staging:
docker:
- image: google/cloud-sdk
environment:
- PROJECT_NAME: "my-app"
- GOOGLE_PROJECT_ID: "xxx"
- GOOGLE_COMPUTE_ZONE: "europe-west3-a"
- GOOGLE_CLUSTER_NAME: "cluster-1"
steps:
- checkout
- run:
name: Setup Google Cloud SDK
command: |
apt-get install -qq -y gettext
echo $GCLOUD_SERVICE_KEY > ${HOME}/gcloud-service-key.json
gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json
gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
gcloud --quiet config set compute/zone ${GOOGLE_COMPUTE_ZONE}
gcloud --quiet container clusters get-credentials ${GOOGLE_CLUSTER_NAME}
- setup_remote_docker
- run:
name: Docker build and push
command: |
docker build \
--build-arg COMMIT_REF=${CIRCLE_SHA1} \
--build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \
-t ${PROJECT_NAME} .
docker tag ${PROJECT_NAME} eu.gcr.io/${GOOGLE_PROJECT_ID}/${PROJECT_NAME}:${CIRCLE_SHA1}
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://eu.gcr.io
docker push eu.gcr.io/${GOOGLE_PROJECT_ID}/${PROJECT_NAME}:${CIRCLE_SHA1}
- run:
name: Deploy to Kubernetes
command: |
envsubst < ${HOME}/project/k8s.yml > ${HOME}/patched_k8s.yml
kubectl apply -f ${HOME}/patched_k8s.yml
kubectl rollout status deployment/${PROJECT_NAME}
workflows:
version: 2
build_test_deploy:
jobs:
- build_and_test
- deploy_to_staging:
requires:
- build_and_test
filters:
branches:
only: master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment