Skip to content

Instantly share code, notes, and snippets.

@inno-asiimwe
Created February 15, 2021 09:01
Show Gist options
  • Save inno-asiimwe/0e19513e7974529bb4907644701dfec6 to your computer and use it in GitHub Desktop.
Save inno-asiimwe/0e19513e7974529bb4907644701dfec6 to your computer and use it in GitHub Desktop.
version: 2
defaults: &defaults
docker:
- image: circleci/python:3.6.2-stretch-browsers
environment:
FLASK_APP: app.py
FLASK_ENV: testing
PGHOST: 127.0.0.1
PGUSER: root
- image: circleci/postgres:10.8-alpine
environment:
POSTGRES_USER: root
POSTGRES_DB: cranecloud_test_db
working_directory: ~/crane-cloud
attach_workspace: &attach_workspace
attach_workspace:
at: ~/crane-cloud
filters:
filters:
branches:
only:
- master
- develop
- /(release|hotfix)\/v[0-9].[0-9].[0-9]/
persist_to_workspace: &persist_to_workspace
persist_to_workspace:
root: .
paths: .
build_and_push_image: &build_and_push_image
run:
name: build image
command: |
export IMG_TAG=$(echo $CIRCLE_SHA1 | cut -c -7)
docker login -u _json_key -p "$(echo $GCLOUD_SERVICE_KEY)" https://gcr.io
docker build -f docker/prod/Dockerfile -t gcr.io/$GCP_PROJECT_ID/cranecloud-backend:$IMG_TAG .
docker push gcr.io/$GCP_PROJECT_ID/cranecloud-backend:$IMG_TAG
configure_gcloud: &configure_gcloud
run:
name: configure gcloud default settings
command: |
echo $GCLOUD_SERVICE_KEY > ~/crane-cloud/auth_key.json
gcloud auth activate-service-account --key-file ~/crane-cloud/auth_key.json
gcloud container clusters get-credentials $cluster --zone us-central1-a --project $GCP_PROJECT_ID
deploy_to_kubernetes: &deploy_to_kubernetes
run:
name: update deployment image
command: |
export IMG_TAG=$(echo $CIRCLE_SHA1 | cut -c -7)
./kubectl set image deployment/cranecloud-backend cranecloud-backend=gcr.io/$GCP_PROJECT_ID/cranecloud-backend:$IMG_TAG --record -n cranecloud
jobs:
test_and_report:
<<: *defaults
steps:
- checkout
- *attach_workspace
- restore_cache:
keys:
- o-{{ .Branch }}-{{ checksum "Pipfile.lock" }}
- o-{{ .Branch }}
- o
- run:
name: install dependencies
command: |
sudo pip install pipenv
pipenv install
- save_cache:
key: o-{{ .Branch }}-{{ checksum "Pipfile.lock" }}
paths:
- ".venv"
- "/usr/local/bin"
- "/usr/local/lib/python3.6/site-packages"
- run:
name: run tests
command: pipenv run nosetests --with-coverage --cover-package=app
- run:
name: upload tests coverage
command: pipenv run codecov
- *persist_to_workspace
build_and_deploy_staging:
docker:
- image: google/cloud-sdk
environment:
GOOGLE_APPLICATION_CREDENTIALS: /root/crane-cloud/auth_key.json
cluster: staging-cluster
working_directory: ~/crane-cloud
steps:
- *attach_workspace
- setup_remote_docker
- run:
name: install kubectl
command: |
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod u+x ./kubectl
- *configure_gcloud
- *build_and_push_image
- *deploy_to_kubernetes
build_and_deploy_production:
docker:
- image: google/cloud-sdk
environment:
GOOGLE_APPLICATION_CREDENTIALS: /root/crane-cloud/auth_key.json
cluster: production-cluster
working_directory: ~/crane-cloud
steps:
- *attach_workspace
- setup_remote_docker
- run:
name: install kubectl
command: |
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod u+x ./kubectl
- *configure_gcloud
- *build_and_push_image
- *deploy_to_kubernetes
workflows:
version: 2
cranecloud_deployment:
jobs:
- test_and_report
- build_and_deploy_staging:
requires:
- test_and_report
filters:
branches:
only: develop-new
- build_and_deploy_production:
requires:
- test_and_report
filters:
branches:
only: master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment