Created
August 21, 2019 23:52
-
-
Save siu-issiki/bf400e0fd55c53c16419d10d6bc2540f to your computer and use it in GitHub Desktop.
CI/CD example for CircleCI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CI/CD example written by @siu_issiki | |
# | |
# Set bellow environments before running this flow: | |
# - AWS_ECR_ENDPOINT | |
# - AWS_DEFAULT_REGION | |
# - AWS_ACCESS_KEY_ID | |
# - AWS_SECRET_ACCESS_KEY | |
# - CONTAINER_IMAGE_NAME | |
# - DOCKERFILE_PATH | |
# - SLACK_WEBHOOK | |
version: 2.1 | |
workflows: | |
build: | |
jobs: | |
- build | |
- test: | |
requires: | |
- build | |
- dev_deploy: | |
requires: | |
- test | |
filters: | |
branches: | |
only: | |
- develop | |
- /^dev-deploy\/.*/ | |
- prd_deploy: | |
requires: | |
- test | |
filters: | |
branches: | |
only: | |
- master | |
executors: | |
default: | |
machine: | |
image: circleci/classic:latest | |
docker_layer_caching: true | |
jobs: | |
build: | |
executor: default | |
steps: | |
- checkout | |
- restore_image | |
- run: | |
name: Build application Docker image | |
command: | | |
docker build --cache-from=${CONTAINER_IMAGE_NAME} --file=${DOCKERFILE_PATH} -t ${CONTAINER_IMAGE_NAME} . | |
- run: | |
name: Save Docker image layer cache | |
command: | | |
mkdir -p ~/caches | |
docker save -o ~/caches/cache.tar ${CONTAINER_IMAGE_NAME} | |
- save_cache: | |
key: v1-{{ .Branch }}-{{ .Revision }} | |
paths: | |
- ~/caches/cache.tar | |
test: | |
executor: default | |
steps: | |
- checkout | |
- restore_image | |
- run: | |
name: Build test environment | |
command: | | |
cp .env.example .env | |
make | |
- run: | |
name: Run test | |
command: make test | |
- slack/notify-on-failure | |
dev_deploy: | |
executor: default | |
steps: | |
- checkout | |
- restore_image | |
- install_dependencies | |
- run: | |
name: Setting environments | |
command: | | |
echo "export AWS_ECS_CLUSTER_NAME=${CIRCLE_PROJECT_REPONAME}-stg" >> $BASH_ENV | |
echo "export AWS_ECS_MIGRATION_TASK_NAME=${CIRCLE_PROJECT_REPONAME}_migration_stg" >> $BASH_ENV | |
echo "export TAG=latest" >> $BASH_ENV | |
- ecs_deploy | |
- slack/status: | |
success_message: Staging Deploy Completed | |
prd_deploy: | |
executor: default | |
steps: | |
- checkout | |
- restore_image | |
- install_dependencies | |
- run: | |
name: Setting environments | |
command: | | |
echo "export AWS_ECS_CLUSTER_NAME=${CIRCLE_PROJECT_REPONAME}-prd" >> $BASH_ENV | |
echo "export AWS_ECS_MIGRATION_TASK_NAME=${CIRCLE_PROJECT_REPONAME}_migration_prd" >> $BASH_ENV | |
echo "export TAG=release" >> $BASH_ENV | |
- ecs_deploy | |
- slack/status: | |
success_message: Production Deploy Completed | |
commands: | |
install_dependencies: | |
steps: | |
- run: | |
name: Install dependencies | |
command: | | |
pip install \ | |
docker-compose==1.12.0 \ | |
awscli==1.16.193 | |
restore_image: | |
steps: | |
- restore_cache: | |
keys: | |
- v1-{{ .Branch }}-{{ .Revision }} | |
- v1-{{ .Branch }} | |
- v1 | |
paths: | |
- ~/caches/cache.tar | |
- run: | |
name: Load Docker image layer cache | |
command: | | |
set +o pipefail | |
docker load -i ~/caches/cache.tar | true | |
ecs_deploy: | |
steps: | |
- deploy: | |
name: Push application Docker image and deploy | |
command: | | |
# push | |
login="$(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION})" | |
${login} | |
docker tag ${CONTAINER_IMAGE} "${AWS_ECR_ENDPOINT}/${CIRCLE_PROJECT_REPONAME}:${TAG}" | |
docker push "${AWS_ECR_ENDPOINT}/${CIRCLE_PROJECT_REPONAME}:${TAG}" | |
# DB migration | |
taskArn="$(aws ecs run-task --cluster $AWS_ECS_CLUSTER_NAME --task-definition $AWS_ECS_MIGRATION_TASK_NAME --region $AWS_DEFAULT_REGION | jq -r '.tasks[].taskArn')" | |
taskId=${taskArn##*/} | |
lastStatus="$(aws ecs describe-tasks --cluster $AWS_ECS_CLUSTER_NAME --task $taskId --region $AWS_DEFAULT_REGION | jq -r '.tasks[].lastStatus')" | |
while [ "$lastStatus" != STOPPED ] | |
do | |
echo "wait" | |
sleep 1s | |
lastStatus="$(aws ecs describe-tasks --cluster $AWS_ECS_CLUSTER_NAME --task $taskId --region $AWS_DEFAULT_REGION | jq -r '.tasks[].lastStatus')" | |
done | |
# deploy | |
aws ecs update-service --cluster $AWS_ECS_CLUSTER_NAME --service $AWS_ECS_SERVICE_NAME --force-new-deployment | |
orbs: | |
slack: circleci/slack@3.3.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment