Skip to content

Commit

Permalink
Updated config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
longbui98 committed Nov 1, 2023
1 parent f54310c commit f28ac3a
Showing 1 changed file with 148 additions and 107 deletions.
255 changes: 148 additions & 107 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@ version: 2.1
commands:
destroy-environment:
description: Destroy back-end and front-end cloudformation stacks given a workflow ID.
parameters:
workflow_id:
type: string
default: ${CIRCLE_WORKFLOW_ID:0:7}
steps:
- run:
name: Install AWS CLI
command: |
apk -v --update add python3 py-pip
pip install awscli
- run:
name: Destroy environments
when: on_fail
command: |
aws cloudformation delete-stack --stack-name udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}
aws cloudformation delete-stack --stack-name udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}
revert-migrations:
description: Revert the last migration if successfully run in the current workflow.
parameters:
# Add parameter here
workflow_id:
type: string
default: ${CIRCLE_WORKFLOW_ID:0:7}
steps:
- run:
name: Revert migrations
# Add when this will run
when: on_fail
command: |
# Curl command here to see if there was a successful migration associated with the workflow id, store result in SUCCESS variable
SUCCESS = 1
echo "insecure" >> ~/.curlrc
SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>)
if(( $SUCCESS==1 ));
then
# cd ~/project/backend
# npm install
# Add revert code here. You can find this in the Getting Started section.
exit 1
fi
cd ~/project/backend
npm install
npm run migration:revert
fi
jobs:
build-frontend:
docker:
Expand Down Expand Up @@ -147,37 +146,36 @@ jobs:
--template-file .circleci/files/backend.yml \
--tags project=cicd_project \
--stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \
--parameter-overrides ID="udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}"
--parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}"
- run:
name: Ensure front-end infrastructure exist
command: |
aws cloudformation deploy \
--template-file .circleci/files/frontend.yml \
--tags project=cicd_project \
--stack-name "udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}" \
--parameter-overrides ID="udapeople-backend-${CIRCLE_WORKFLOW_ID:0:7}"
--stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \
--parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}"
- run:
name: Add back-end ip to ansible inventory
command: |
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].PublicIpAddress' \
--output text >> inventory
--output text >> ~/project/.circleci/ansible/inventory.txt
- persist_to_workspace:
root: ~/
paths:
- project/.circleci/ansible/inventory.txt
- destroy-environment

configure-infrastructure:
docker:
- image: python:3.7-alpine3.11
steps:
- checkout
- add_ssh_keys:
fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f]
- persist_to_workspace:
root: ~/
paths:
- project/.circleci
- attach_workspace:
at: ~/
- run:
name: Install dependencies
command: |
Expand All @@ -186,122 +184,165 @@ jobs:
name: Configure server
command: |
cd .circleci/ansible
cat inventory.txt
ansible-playbook -i inventory.txt configure-server.yml
- destroy-environment

run-migrations:
docker:
- image: circleci/node:13.8.0
steps:
- checkout
- persist_to_workspace:
root: ~/
paths:
- project/frontend
- run:
name: Run migrations
command: |
npm install
cd backend
npm install
npm run migrations > migrations_dump.txt
- run:
name: Send migration results to memstash
command: |
if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt
then
curl https://kvdb.io/TwniJT5UFxFZM66gXv8KdL/migration_${CIRCLE_WORKFLOW_ID:0:7} -d '1'
fi
cat ~/project/backend/migrations_dump.txt
echo "insecure" >> ~/.curlrc
if grep -q "has been executed successfully." ~/project/backend/migrations_dump.txt
then
curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\} -d '1'
fi
- run:
name: Accessing key from kvdb.io
command: |
curl https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_$\{CIRCLE_WORKFLOW_ID:0:7\}
- destroy-environment

deploy-frontend:
docker:
- image: amazon/aws-cli
- image: python:3.9.1-alpine3.12
steps:
- checkout
- persist_to_workspace:
root: ~/
paths:
- project/frontend
- restore_cache:
keys: [frontend-build]
- run:
name: Install dependencies
command: |
sudo yum update -y
sudo apt install -y tar
tar --version
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
sudo yum install -y nodejs
node -v
npm -v
apk update
apk add tar gzip
apk add npm nodejs
apk add --update ansible aws-cli
- run:
name: Get backend url
command: |
export BACKEND_IP=$(aws ec2 describe-instances --query 'Reservations[*].Instances[*].PublicIpAddress')
cat ~/project/.circleci/ansible/inventory.txt
export BACKEND_IP=$(cat ~/project/.circleci/ansible/inventory.txt | sed 1d)
export API_URL="http://${BACKEND_IP}:3030"
echo "API_URL = ${API_URL}"
echo API_URL="http://${BACKEND_IP}:3030" >> frontend/.env
cat frontend/.env
- run:
name: Deploy frontend objects
command: |
cd frontend
npm install
npm run build
tar -czvf artifact-"${CIRCLE_WORKFLOW_ID:0:7}".tar.gz dist
aws s3 cp dist s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7} --recursive
- destroy-environment

# deploy-backend:
# docker:
# # Docker image here that supports Ansible
# steps:
# # Checkout code from git
# # Add ssh keys with fingerprint
# # attach workspace
# - run:
# name: Install dependencies
# command: |
# # your code here
# - run:
# name: Deploy backend
# command: |
# # your code here
# # Here's where you will add some code to rollback on failure
- destroy-environment
- revert-migrations

deploy-backend:
docker:
- image: python:3.9.1-alpine3.12
steps:
- checkout
- deploy-frontend:
at: ~/
- add_ssh_keys:
fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f]
- run:
name: Install dependencies
command: |
apk update
apk add tar gzip
apk add npm nodejs
apk add --update ansible aws-cli
- run:
name: Deploy backend
command: |
cd backend
npm i
npm run build
cd ..
tar -C backend -czvf artifact.tar.gz .
cd .circleci/ansible
echo "Contents of the inventory.txt file is -------"
cat inventory.txt
ansible-playbook -i inventory.txt deploy-backend.yml
- destroy-environment
- revert-migrations

# smoke-test:
# docker:
# # Lightweight Docker image
# steps:
# # Checkout code from git
# - run:
# name: Install dependencies
# command: |
# # your code here
# - run:
# name: Get backend url
# command: |
# # your code here
# - run:
# name: Backend smoke test.
# command: |
# # your code here
# - run:
# name: Frontend smoke test.
# command: |
# # your code here
# # Here's where you will add some code to rollback on failure
smoke-test:
docker:
- image: python:3.9.1-alpine3.12
steps:
- checkout
- attach_workspace:
at: ~/
- run:
name: Install dependencies
command: |
apk update
apk add npm nodejs
apk add --no-cache curl
apk -v --update add --no-cache groff less python3 py3-pip
pip3 install --upgrade pip
pip3 install awscli
- run:
name: Backend smoke test.
command: |
export API_URL="http://${BACKEND_IP}:3030"
echo "${API_URL}"
if curl "${API_URL}/api/status" | grep "ok"
then
return 0
else
return 1
fi
- run:
name: Frontend smoke test.
command: |
URL="http://udapeople-${CIRCLE_WORKFLOW_ID:0:7}.s3-website-us-east-1.amazonaws.com/#/employees"
echo ${URL}
if curl -s ${URL} | grep "Welcome"
then
return 1
else
return 1
fi
- destroy-environment
- revert-migrations

# cloudfront-update:
# docker:
# # Docker image here that supports AWS CLI
# steps:
# # Checkout code from git
# - run:
# name: Install dependencies
# command: |
# # your code here
# - run:
# name: Update cloudfront distribution
# command: |
# # your code here
# # Here's where you will add some code to rollback on failure
cloudfront-update:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run:
name: Install dependencies
command: |
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
sudo yum install -y nodejs
node -v
npm -v
- run:
name: Update cloudfront distribution
command: |
aws cloudformation deploy \
--template-file .circleci/files/cloudfront.yml \
--stack-name InitialStack \
--parameter-overrides WorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \
--tags project=udapeople
- destroy-environment
- revert-migrations

# cleanup:
# docker:
Expand Down Expand Up @@ -347,11 +388,11 @@ workflows:
requires: [configure-infrastructure]
- deploy-frontend:
requires: [run-migrations]
# - deploy-backend:
# requires: [run-migrations]
# - smoke-test:
# requires: [deploy-backend, deploy-frontend]
# - cloudfront-update:
# requires: [smoke-test]
- deploy-backend:
requires: [run-migrations]
- smoke-test:
requires: [deploy-backend, deploy-frontend]
- cloudfront-update:
requires: [smoke-test]
# - cleanup:
# requires: [cloudfront-update]

0 comments on commit f28ac3a

Please sign in to comment.