Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test feature branch #1

Merged
merged 96 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
02c1cd3
Add configure-infrastructure job
longbui98 Oct 24, 2023
643448b
Update image and path in configure-infrastructure jong
longbui98 Oct 24, 2023
d7dbdca
Update path in configure-infrastructure job
longbui98 Oct 24, 2023
6d8ba35
Changing image in configure infrastructure
longbui98 Oct 24, 2023
842de24
Adding job run-migrations
longbui98 Oct 24, 2023
8fe07e4
Adding deploy-frontend job
longbui98 Oct 24, 2023
b9611a4
Update path and create file for storing migration info
longbui98 Oct 24, 2023
95a860f
Update path for migration jod and move migrations_dump file to fronte…
longbui98 Oct 24, 2023
99463dc
Update step to install aws cli in commands
longbui98 Oct 24, 2023
1490097
Update step in commands
longbui98 Oct 24, 2023
95d9cc0
Update step in commands
longbui98 Oct 24, 2023
77f71b3
Update config for jobs
longbui98 Oct 25, 2023
1128c7b
Fixing format in run-migration jobs
longbui98 Oct 25, 2023
b275efb
Configure deploy role
longbui98 Oct 28, 2023
7d6e35c
Update workspace of deploy-infrastructure job
longbui98 Oct 28, 2023
17f99b0
Update workspace of deploy-infrastructure job
longbui98 Oct 28, 2023
50a443c
Update promotion phase
longbui98 Oct 29, 2023
d269a89
Fixing commands in pinelines
longbui98 Oct 29, 2023
ce400e6
Update pineline
longbui98 Oct 29, 2023
9343d1d
Update host
longbui98 Oct 29, 2023
85a2416
Update host in configure-server
longbui98 Oct 29, 2023
a7877d7
Update bucket in kvdb
longbui98 Oct 29, 2023
292a25d
Update deploy-frontend and deploy-backend jobs
longbui98 Oct 29, 2023
f1a05f7
Update image for deploy backend and deploy frontend jobs
longbui98 Oct 29, 2023
3e6171c
Update config.yml
longbui98 Oct 30, 2023
1d8cdf8
Update parameter in revert migration
longbui98 Oct 30, 2023
ef4b08a
Update parameter overrides in deploy infrastructure
longbui98 Oct 30, 2023
eaf2191
Update parameter overrides in deploy infrastructure
longbui98 Oct 30, 2023
b1b8d90
Update image in smokde test job
longbui98 Oct 30, 2023
156f345
Update image in smokde test job
longbui98 Oct 30, 2023
b83b020
Update workspack in smoke test job
longbui98 Oct 30, 2023
3ade0c9
Fixing add ip into inventory file and export BACKEND_IP
longbui98 Oct 31, 2023
0d89813
Update logic to send keystore to kvdb and accessing from it
longbui98 Oct 31, 2023
2e2b235
Update bucket kvdb
longbui98 Oct 31, 2023
72f3547
Update trust store with the latest trusted root
longbui98 Oct 31, 2023
713faf1
Update to check ip backend in inventory file
longbui98 Oct 31, 2023
2c7e487
Update config.yml
longbui98 Oct 31, 2023
93d44be
Update config
longbui98 Oct 31, 2023
ea058fb
Update config.yml
longbui98 Oct 31, 2023
747deb0
Update export BACKEND_IP
longbui98 Oct 31, 2023
bcdeab7
Update export BACKEND_IP
longbui98 Oct 31, 2023
44b2cff
Update export BACKEND_IP
longbui98 Oct 31, 2023
53a97db
Update workspace in config.yml
longbui98 Nov 1, 2023
4eaf514
Update workspace in config.yml
longbui98 Nov 1, 2023
e69eb51
Update path for configure infrastrcture and deploye backend job
longbui98 Nov 1, 2023
791883e
Update attach_workspace in smoke test job
longbui98 Nov 1, 2023
8386b3a
Update config.yml
longbui98 Nov 1, 2023
ca3a0cb
Update config.yml
longbui98 Nov 1, 2023
5a69543
Update config.yml
longbui98 Nov 1, 2023
a4846fe
Update config.yml
longbui98 Nov 1, 2023
149749f
Update install python in configure-server.yml
longbui98 Nov 1, 2023
21e2a1f
Update install for main in configure-server
longbui98 Nov 1, 2023
a8c7764
Update install for main in configure-server
longbui98 Nov 1, 2023
1818e1c
Update image for ec2 in backend.yml
longbui98 Nov 1, 2023
0a054c7
Update apt update command for main in deploy role
longbui98 Nov 1, 2023
5b902e9
Update start for main.yml in deploy role and update package in deploy…
longbui98 Nov 1, 2023
1b8a7d5
Update script for main.yml in deploy role
longbui98 Nov 1, 2023
aca2936
Update script for main.yml in smoke-test job
longbui98 Nov 1, 2023
4d3aeb7
Testing after deploy backend
longbui98 Nov 2, 2023
afcf30a
Update job of role deploy and preparing clean up job in config.yml
longbui98 Nov 2, 2023
17949d0
Update step to start server for deploy role
longbui98 Nov 3, 2023
5567e57
Update step to start server in deploy role
longbui98 Nov 3, 2023
f99c8e9
Add step to add .env in run-migration job
longbui98 Nov 4, 2023
c0a57f7
Testing after add env for backend
longbui98 Nov 4, 2023
9abc544
Update add env into backend in deploy-backend
longbui98 Nov 4, 2023
e14cf17
Update add env to backend
longbui98 Nov 4, 2023
1a50505
Update persitence workspace at run-migrations
longbui98 Nov 5, 2023
a6645fe
Update persist_to_workspace in run-mnigrations job
longbui98 Nov 5, 2023
7f15594
Update add enviroment variable step to deploy role
longbui98 Nov 5, 2023
b4cf993
Update add enviroment variable step to configure-server file
longbui98 Nov 5, 2023
7038a35
Update .env file
longbui98 Nov 5, 2023
caeb3e2
Update .env file
longbui98 Nov 5, 2023
0c1ba3c
Revert .env.sample
longbui98 Nov 5, 2023
bbaa7ac
Add step to add env circleci to env in deploy backend job
longbui98 Nov 5, 2023
266daf2
Add step to add env circleci to env in deploy configure-infrastructur…
longbui98 Nov 5, 2023
488f837
Add step to add env circleci to env in deploy-infrastructure job
longbui98 Nov 5, 2023
25c497b
Test smoke test job
longbui98 Nov 5, 2023
28e4537
Fix smoke test for test fe and test cloudfront-update job
longbui98 Nov 5, 2023
2819235
Fix smoke test for test fe and test cloudfront-update job
longbui98 Nov 5, 2023
6ceecc2
Add remove s3 in destroy environment and update cloudfront update job
longbui98 Nov 5, 2023
fe15f8b
Update config.yml
longbui98 Nov 5, 2023
331ba03
Update config.yml
longbui98 Nov 5, 2023
505ae71
Update config.yml
longbui98 Nov 5, 2023
f323658
Update config.yml
longbui98 Nov 5, 2023
fe204f7
Update config.yml
longbui98 Nov 5, 2023
6f2764c
Update config.yml
longbui98 Nov 5, 2023
907f50d
Update config.yml
longbui98 Nov 5, 2023
29497ad
Update image in cloudfront-update job
longbui98 Nov 6, 2023
1547472
Install aws cli in cloudfront-update job
longbui98 Nov 6, 2023
53dcd3c
Update overdide ID for cloudformation deplot in cleanup job
longbui98 Nov 6, 2023
a9a09ed
Update workspacke in cloudfront-update job
longbui98 Nov 6, 2023
d5775c1
Update name of cloudformation in cloudfront update
longbui98 Nov 6, 2023
adb6ca1
Update cleanup job in config.yml
longbui98 Nov 7, 2023
c123dc5
Update cleanup job and deploy-infrastructure in only master branch
longbui98 Nov 7, 2023
6e2b4f0
Add section to install configure-prometheus-node-exporter in configur…
longbui98 Nov 7, 2023
33cc7f8
Merge branch 'master' into test-feature-branch
longbui98 Nov 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions .circleci/ansible/configure-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

pre_tasks:
- name: "wait 600 seconds for target connection to become reachable/usable."
# Your code here
wait_for_connection:

- name: "install python for Ansible."
# Your code here

# Get the environment variables from CircleCI and add to the EC2 instance
environment:
- TYPEORM_CONNECTION: "{{ lookup('env', 'TYPEORM_CONNECTION')}}"
# Add more env vars here

apt:
name: python3
state: latest

roles:
# Your code here
- configure-server
- configure-prometheus-node-exporter
30 changes: 30 additions & 0 deletions .circleci/ansible/roles/configure-server/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
- name: "update apt packages."
become: yes
apt:
update_cache: yes

- name: "upgrade packages."
become: yes
apt:
upgrade: "yes"

- name: remove dependencies that are no longer required
become: yes
apt:
autoremove: yes

- name: "install dependencies."
become: yes
apt:
name: ["nodejs", "npm"]
state: latest
update_cache: yes

- name: "install pm2"
become: yes
npm:
name: pm2
global: yes
production: yes
state: present
24 changes: 24 additions & 0 deletions .circleci/ansible/roles/deploy/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: copy dist backend folder to ec2 instance
become: yes
copy:
src: ~/project/artifact.tar.gz
dest: /home/ubuntu/artifact.tar.gz

- name: prepare binaries
become: true
shell: |
cd /home/ubuntu
tar xvzf artifact.tar.gz -C .

- name: "start server"
become: true
become_method: sudo
become_user: root
shell: |
cd /home/ubuntu
npm install
pm2 stop default
pm2 start npm -- start
pm2 startup
pm2 save
104 changes: 61 additions & 43 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@ 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: Destroy environments
when: on_fail
command: |
if aws s3 ls | grep "udapeople-${CIRCLE_WORKFLOW_ID:0:7}"; then
aws s3 rm s3://udapeople-${CIRCLE_WORKFLOW_ID:0:7} --recursive
fi
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:
workflow_id:
type: string
default: ${CIRCLE_WORKFLOW_ID:0:7}
steps:
- run:
name: Revert migrations
when: on_fail
command: |
echo "insecure" >> ~/.curlrc
SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_<< parameters.workflow_id >>)
SUCCESS=$(curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7})
if(( $SUCCESS==1 ));
then
cd ~/project/backend
Expand Down Expand Up @@ -155,6 +150,19 @@ jobs:
--tags project=cicd_project \
--stack-name "udapeople-frontend-${CIRCLE_WORKFLOW_ID:0:7}" \
--parameter-overrides ID="${CIRCLE_WORKFLOW_ID:0:7}"
- run:
name: Add env to backend
command: |
cd ~/project/backend
echo TYPEORM_CONNECTION=$TYPEORM_CONNECTION >> .env
echo TYPEORM_ENTITIES=$TYPEORM_ENTITIES >> .env
echo TYPEORM_HOST=$TYPEORM_HOST >> .env
echo TYPEORM_PORT=$TYPEORM_PORT >> .env
echo TYPEORM_USERNAME=$TYPEORM_USERNAME >> .env
echo TYPEORM_PASSWORD=$TYPEORM_PASSWORD >> .env
echo TYPEORM_DATABASE=$TYPEORM_DATABASE >> .env
echo TYPEORM_MIGRATIONS=$TYPEORM_MIGRATIONS >> .env
echo TYPEORM_MIGRATIONS_DIR=$TYPEORM_MIGRATIONS_DIR >> .env
- run:
name: Add back-end ip to ansible inventory
command: |
Expand All @@ -164,6 +172,7 @@ jobs:
- persist_to_workspace:
root: ~/
paths:
- project/backend/.env
- project/.circleci/ansible/inventory.txt
- destroy-environment

Expand Down Expand Up @@ -193,6 +202,8 @@ jobs:
- image: circleci/node:13.8.0
steps:
- checkout
- restore_cache:
keys: [backend-build]
- run:
name: Run migrations
command: |
Expand All @@ -206,12 +217,12 @@ jobs:
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'
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\}
curl --insecure https://kvdb.io/VZNDWCZz5NPBkEr81irH3u/migration_${CIRCLE_WORKFLOW_ID:0:7}
- destroy-environment

deploy-frontend:
Expand All @@ -221,12 +232,15 @@ jobs:
- checkout
- restore_cache:
keys: [frontend-build]
- attach_workspace:
at: ~/
- run:
name: Install dependencies
command: |
apk update
apk add tar gzip
apk add npm nodejs
apk add --no-cache curl
apk add --update ansible aws-cli
- run:
name: Get backend url
Expand All @@ -253,7 +267,7 @@ jobs:
- image: python:3.9.1-alpine3.12
steps:
- checkout
- deploy-frontend:
- attach_workspace:
at: ~/
- add_ssh_keys:
fingerprints: [ad:8d:62:dc:0b:b1:23:7d:90:1f:17:39:51:2c:29:8f]
Expand All @@ -263,6 +277,7 @@ jobs:
apk update
apk add tar gzip
apk add npm nodejs
apk add --no-cache curl
apk add --update ansible aws-cli
- run:
name: Deploy backend
Expand Down Expand Up @@ -298,8 +313,10 @@ jobs:
- run:
name: Backend smoke test.
command: |
export BACKEND_IP=$(cat ~/project/.circleci/ansible/inventory.txt | sed 1d)
echo ${BACKEND_IP}
export API_URL="http://${BACKEND_IP}:3030"
echo "${API_URL}"
echo ${API_URL}
if curl "${API_URL}/api/status" | grep "ok"
then
return 0
Expand All @@ -313,7 +330,7 @@ jobs:
echo ${URL}
if curl -s ${URL} | grep "Welcome"
then
return 1
return 0
else
return 1
fi
Expand All @@ -322,47 +339,48 @@ jobs:

cloudfront-update:
docker:
- image: amazon/aws-cli
- image: python:3.9.1-alpine3.12
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
apk update
apk add npm nodejs
node -v
npm -v
apk add --no-cache curl
apk add --update ansible aws-cli
- 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}" \
--template-file ~/project/.circleci/files/cloudfront.yml \
--stack-name InitialStackUpdate \
--parameter-overrides NewWorkflowID="udapeople-${CIRCLE_WORKFLOW_ID:0:7}" \
--tags project=udapeople
- destroy-environment
- revert-migrations

# cleanup:
# docker:
# # Docker image here
# steps:
# # Checkout code from git
# - run:
# name: Get old stack workflow id
# command: |
# # your code here
# export OldWorkflowID="the id here"
# export STACKS=[] #put the list of stacks here
# - run:
# name: Remove old stacks and files
# command: |
# if [[ "${STACKS[@]}" =~ "${OldWorkflowID}" ]]
# then
# # your code here
# fi

cleanup:
docker:
- image: amazon/aws-cli
steps:
- checkout
- run:
name: Get old stack workflow id
command: |
export OldWorkflowID=$(aws cloudformation \
list-exports --query "Exports[?Name==\`NewWorkflowID\`].Value" \
--no-paginate --output text)
echo OldWorkflowID: "${OldWorkflowID}"
echo CIRCLE_WORKFLOW_ID "${CIRCLE_WORKFLOW_ID:0:7}"
- run:
name: Remove old stacks and files
command: |
aws s3 rm "s3://${OldWorkflowID}" --recursive
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}"

workflows:
default:
Expand All @@ -381,7 +399,7 @@ workflows:
requires: [test-frontend, test-backend, scan-frontend, scan-backend]
filters:
branches:
only: [test-feature-branch]
only: [master]
- configure-infrastructure:
requires: [deploy-infrastructure]
- run-migrations:
Expand All @@ -394,5 +412,5 @@ workflows:
requires: [deploy-backend, deploy-frontend]
- cloudfront-update:
requires: [smoke-test]
# - cleanup:
# requires: [cloudfront-update]
- cleanup:
requires: [cloudfront-update]
2 changes: 1 addition & 1 deletion .circleci/files/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Resources:
SecurityGroups:
- Ref: InstanceSecurityGroup
KeyName: test-key2
ImageId: ami-0fc5d935ebf8bc3bc
ImageId: ami-068663a3c619dd892
Tags:
- Key: Name
Value: !Sub backend-${ID}
10 changes: 5 additions & 5 deletions .circleci/files/cloudfront.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Description: >
Cloudfront distribution for UdaPeople.

Parameters:
WorkflowID:
NewWorkflowID:
Description: Unique identifier.
Type: String

Expand All @@ -20,7 +20,7 @@ Resources:
Properties:
DistributionConfig:
Origins:
- DomainName: !Sub "udapeople-${WorkflowID}.s3.amazonaws.com"
- DomainName: !Sub "udapeople-${NewWorkflowID}.s3.amazonaws.com"
Id: webpage
S3OriginConfig:
OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}"
Expand All @@ -33,8 +33,8 @@ Resources:
ViewerProtocolPolicy: allow-all

Outputs:
WorkflowID:
Value: !Sub ${WorkflowID}
NewWorkflowID:
Value: !Sub ${NewWorkflowID}
Description: URL for website hosted on S3
Export:
Name: WorkflowID
Name: NewWorkflowID
2 changes: 1 addition & 1 deletion backend/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TYPEORM_MIGRATIONS=./src/migrations/*.ts

# Use these values for the local PG database from the Docker Compose file
TYPEORM_HOST=localhost
TYPEORM_PORT=5532
TYPEORM_PORT=5432
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=password
TYPEORM_DATABASE=glee