Skip to content

Commit

Permalink
chore: setup CICD
Browse files Browse the repository at this point in the history
Signed-off-by: Nam Hoang <[email protected]>
  • Loading branch information
namhoang1604 committed Aug 29, 2023
1 parent 494139a commit 770802f
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 135 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/build-and-deploy-agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Build and Deploy Agent

on:
push:
branches:
- demo

jobs:
install_and_build:
runs-on: ubuntu-latest

permissions:
id-token: write

env:
CI: false

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install AWS CLI
run: |
sudo apt-get -q install -y python3-pip
pip3 install awscli --upgrade --user
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_GITHUB_ACTION_ARN }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Remove Explorer
run: rm -rf packages/demo-explorer packages/react-components packages/vckit-oa-renderers

- name: Compress Agent Server
run: tar -czf agent-server.tar.gz *

- name: Upload Artifact to S3
run: aws s3 cp agent-server.tar.gz s3://${{ vars.AGENT_SERVER_AWS_S3_BUCKET_NAME }}/agent-server.tar.gz


deploy:
needs: install_and_build
runs-on: ubuntu-latest

permissions:
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install AWS CLI
run: |
sudo apt-get -q install -y python3-pip
pip3 install awscli --upgrade --user
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_GITHUB_ACTION_ARN }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Deploy to EC2
run: aws deploy create-deployment --application-name ${{ vars.AWS_DEPLOY_APP_NAME }} --deployment-group-name ${{ vars.AWS_DEPLOY_GROUP_NAME }} --s3-location bucket=${{ vars.AGENT_SERVER_AWS_S3_BUCKET_NAME }},key=agent-server.tar.gz,bundleType=tgz --ignore-application-stop-failures




114 changes: 0 additions & 114 deletions .github/workflows/build-and-deploy.yml

This file was deleted.

12 changes: 9 additions & 3 deletions .github/workflows/deploy-to-s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
node-version: '18'

- name: Install pnpm
run: npm install -g [email protected].2
run: npm install -g [email protected].0

- name: pnpm version
run: pnpm --version
Expand All @@ -47,8 +47,14 @@ jobs:
echo REACT_APP_DEFAULT_AGENT_ID=${{ vars.REACT_APP_DEFAULT_AGENT_ID }} >> .env
cat .env
- name: Build
run: pnpm build
- name: Build example documents
run: cd packages/example-documents && pnpm build

- name: Build renderer
run: cd packages/renderer && pnpm build

- name: Build explorer
run: cd packages/demo-explorer && pnpm build

- name: Archive artifacts
uses: actions/upload-artifact@v3
Expand Down
26 changes: 26 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/agent_server
hooks:
ApplicationStop:
- location: aws/scripts/application_stop.sh
timeout: 300
runas: root
BeforeInstall:
- location: aws/scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: aws/scripts/after_install.sh
timeout: 1200
runas: root
ApplicationStart:
- location: aws/scripts/application_start.sh
timeout: 300
runas: root
ValidateService:
- location: aws/scripts/validate_service.sh
timeout: 300
runas: root
6 changes: 6 additions & 0 deletions aws/scripts/after_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
# Install dependencies
source /home/ec2-user/.bashrc
cd /home/ec2-user/agent_server
pnpm install
pnpm build
6 changes: 6 additions & 0 deletions aws/scripts/application_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
source /home/ec2-user/.bashrc
cd /home/ec2-user/agent_server
pnpm vckit config
# Start server
pm2 start pnpm -- vckit server
15 changes: 15 additions & 0 deletions aws/scripts/application_stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
# Restart codedeploy agent
sudo service codedeploy-agent stop
sudo rm -rf /opt/codedeploy-agent/deployment-root/*
sudo service codedeploy-agent start

# Stop server
pm2 stop all

# Delete old source
if [ -d /home/ec2-user/agent_server ]; then
sudo rm -rf /home/ec2-user/agent_server
fi

sudo mkdir -vp /home/ec2-user/agent_server
17 changes: 17 additions & 0 deletions aws/scripts/before_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
if git --version; then
echo "git already installed"
else
sudo yum install git -y
fi

if node --version; then
echo "node already installed"
else
sudo yum install gcc-c++ make -y
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 16
npm install -g [email protected]
npm install pm2 -g
fi
7 changes: 0 additions & 7 deletions aws/scripts/delete_old_source.sh

This file was deleted.

6 changes: 0 additions & 6 deletions aws/scripts/install_dependencies.sh

This file was deleted.

5 changes: 0 additions & 5 deletions aws/scripts/start_server.sh

This file was deleted.

18 changes: 18 additions & 0 deletions aws/scripts/validate_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -x

NUMBER_OF_ATTEMPTS=10
SLEEP_TIME=3

for i in `seq 1 $NUMBER_OF_ATTEMPTS`;
do
HTTP_CODE=`curl --insecure --write-out '%{http_code}' -o /dev/null -m 10 -q -s http://localhost:3332/open-api.json`
if [ "$HTTP_CODE" == "200" ]; then
echo "app server is running."
exit 0
fi
echo "Attempt to curl endpoint returned HTTP Code $HTTP_CODE. Backing off and retrying."
sleep $SLEEP_TIME
done
echo "Server did not come up after expected time. Failing."
exit 1

0 comments on commit 770802f

Please sign in to comment.