Skip to content

Instantly share code, notes, and snippets.

@IbraheemAlSaady
Last active November 8, 2019 18:49
Show Gist options
  • Save IbraheemAlSaady/b0c92c06971c1182e8410894031580ca to your computer and use it in GitHub Desktop.
Save IbraheemAlSaady/b0c92c06971c1182e8410894031580ca to your computer and use it in GitHub Desktop.
CircleCI 2 Google Cloud Kubernetes Deployment
version: 2
jobs:
build:
working_directory: ~/your-project-name
docker:
- image: circleci/node:9.11
steps:
- checkout
- setup_remote_docker
- run:
name: making sure we're in the root directory
command: ls -a
- run:
name: Does docker even exist
command: docker -v
- run:
name: npm install
command: npm install
- run:
name: building docker image
command: docker build --rm=false -t asia.gcr.io/${PROJECT}/${DOCKER_IMAGE}:$CIRCLE_SHA1 .
- run:
name: Mkdir docker-cache
command: mkdir -p docker-cache
- run:
name: Save docker image to cache directory
command: docker save -o docker-cache/built-image.tar asia.gcr.io/${PROJECT}/${DOCKER_IMAGE}:$CIRCLE_SHA1
- persist_to_workspace:
root: .
paths:
- docker-cache
- save_cache:
key: node-modules-{{ checksum "package.json" }}
paths:
- node_modules
test:
working_directory: ~/your-project-name
docker:
- image: circleci/node:9.11
- image: circleci/redis
- image: mysql:5.7
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_USER: root
MYSQL_DATABASE: your-database-name
steps:
- checkout
- restore_cache:
keys:
- node-modules-{{ checksum "package.json" }}
- run:
name: installing mysql client from apt
command: sudo apt install -y mysql-client
- run:
name: migrate and seed
command: npm run db:migrate && npm run db:seed:run
- run:
name: npm test
command: npm test
deploy-staging:
working_directory: ~/your-project-name
docker:
- image: google/cloud-sdk
steps:
- checkout
- setup_remote_docker
- attach_workspace:
at: .
- run:
name: Load docker image from cache
command: docker load --input docker-cache/built-image.tar
- run:
name: Auth gCloud SDK
command: bash ./deploy/gcloud-auth.sh
- run:
name: tag docker image with latest
command: docker tag asia.gcr.io/${PROJECT}/${DOCKER_IMAGE}:$CIRCLE_SHA1 asia.gcr.io/${PROJECT}/${DOCKER_IMAGE}:latest
- run:
name: Running staging.sh
command: bash ./deploy/staging.sh
workflows:
version: 2
build_test_deploy:
jobs:
- build:
context: your-context-name
- test:
requires:
- build
- deploy-staging:
context: your-context-name
requires:
- test
filters:
branches:
only: feature/circleci-v2
#!/bin/sh
set -e
echo ${GOOGLE_AUTH} > ${HOME}/gcp-key.json
gcloud auth activate-service-account --key-file ${HOME}/gcp-key.json
gcloud --quiet config set project ${PROJECT}
gcloud config set compute/zone ${ZONE}
gcloud auth configure-docker
#!/bin/sh
set -e
# Setup basic gcloud config
gcloud --quiet config set container/cluster $KUBE_CLUSTER
gcloud --quiet container clusters get-credentials $KUBE_CLUSTER
# Push the new docker image
gcloud docker -- push asia.gcr.io/${PROJECT}/${DOCKER_IMAGE}
# setting current context namespace
kubectl config set-context $(kubectl config current-context) --namespace=$CLUSTER_STAGING_NAMESPACE
# Display config
kubectl config view
kubectl config current-context
# Replace deployment image
kubectl set image deployment/${KUBE_DEPLOYMENT_NAME_STAGING} ${KUBE_DEPLOYMENT_CONTAINER_NAME_STAGING}=asia.gcr.io/${PROJECT}/${DOCKER_IMAGE}:$CIRCLE_SHA1 --namespace=$CLUSTER_STAGING_NAMESPACE
@mrjumpy
Copy link

mrjumpy commented Jul 6, 2018

Hi ~
I think should be gcloud auth configure-docker --quiet
you can check that gcloud auth configure-docker result

ref:
WARNING: `gcloud docker` will not be supported for Docker client versions above 18.03.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment