Skip to content

Commit

Permalink
Issue #55: Update CircleCI config to automatically deploy application
Browse files Browse the repository at this point in the history
The build config now creates a new Docker image, tags it with latest, pushes it to
Google Container Registry and deploys to Google Compute Engine.
  • Loading branch information
CarlosRodrigo authored and rodrigoperazzo committed Mar 7, 2019
1 parent a8fc5c1 commit 68ad40f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,31 @@ jobs:

# run tests!
#- run: yarn test
deploy:
docker:
- image: google/cloud-sdk
working_directory: ~/sticky-sessions-web
steps:
- checkout
- run:
name: Setup Google Cloud SDK
command: scripts/setup.sh
- setup_remote_docker:
docker_layer_caching: true
- run:
name: Docker Build, Push and Deploy
command: scripts/deploy.sh

workflows:
version: 2
build_and_deploy:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only:
- dev
- master
52 changes: 52 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh

if [ -z "$IMAGE_NAME" ]; then
echo "IMAGE_NAME env variable is empty. Exiting."
exit 1
fi

if [ -z "$GOOGLE_PROJECT_ID" ]; then
echo "GOOGLE_PROJECT_ID env variable is empty. Exiting."
exit 1
fi

# Authenticate to Google Container Registry
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://gcr.io

if [ "$CIRCLE_BRANCH" = "issue_55_cd" ]; then
echo "Deploying application to Staging environment"

# Injects firebase DB file
echo $FIREBASE_DB_STAG | base64 -di > src/db/firebaseDB/keys/index.js

# Builds Docker image
docker build -t ${IMAGE_NAME} .

# Tags newly created image
docker tag ${IMAGE_NAME} gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:stag

# Pushes image to Container Registry
docker push gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:stag

# Deploys application
gcloud --quiet compute ssh stick-sessions --zone us-east1-b -- "cd /home/app && ./restart_service.sh gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:stag"
elif [ "$CIRCLE_BRANCH" = "master" ]; then
echo "Deploying application to Production environment"

# Injects firebase DB file
echo $FIREBASE_DB | base64 -di > src/db/firebaseDB/keys/index.js

# Builds Docker image
docker build -t ${IMAGE_NAME} .

# Tags newly created image
docker tag ${IMAGE_NAME} gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:${CIRCLE_SHA1}
docker tag ${IMAGE_NAME} gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:latest

# Pushes image to Container Registry
docker push gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:${CIRCLE_SHA1}
docker push gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:latest

# Deploys application
gcloud --quiet compute ssh stick-sessions --zone us-east1-b -- "cd /home/app && ./restart_service.sh gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE_NAME}:latest"
fi
20 changes: 20 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

if [ -z "$GCLOUD_SERVICE_KEY" ]; then
echo "GCLOUD_SERVICE_KEY env variable is empty. Exiting."
exit 1
fi

if [ -z "$GOOGLE_PROJECT_ID" ]; then
echo "GOOGLE_PROJECT_ID env variable is empty. Exiting."
exit 1
fi

# Set service account key
echo $GCLOUD_SERVICE_KEY | base64 -di > ${HOME}/gcloud-service-key.json

# Authenticate gcloud
gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json

# Set project ID
gcloud --quiet config set project ${GOOGLE_PROJECT_ID}

0 comments on commit 68ad40f

Please sign in to comment.