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

chore: setup ci cd #154

Merged
merged 11 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
93 changes: 93 additions & 0 deletions .github/workflows/build-and-deploy-agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Build and Deploy Agent

on:
push:
branches:
- dev
tags:
- '*'

env:
environment: ${{ (github.ref == 'refs/heads/dev' && 'dev') || (startsWith(github.ref, 'refs/tags/v') && 'UN') }}

jobs:
install_and_build:
runs-on: ubuntu-latest

permissions:
id-token: write

env:
CI: false

environment: ${{ (github.ref == 'refs/heads/dev' && 'dev') || (startsWith(github.ref, 'refs/tags/v') && 'UN') }}

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@v3
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: Overwrite configuration
if: ${{ env.environment == 'dev' }}
run: cp -f packages/cli/default/default-dev.yml packages/cli/default/default.yml

- name: Create env file
if: ${{ env.environment == 'dev' }}
run: |
touch packages/encrypted-storage/.env
echo DATABASE_URL=${{ secrets.DATABASE_URL}} >> packages/encrypted-storage/.env
echo DATABASE_TYPE=postgres >> packages/encrypted-storage/.env
echo DATABASE_DATABASE=${{ secrets.DATABASE_DATABASE}} >> packages/encrypted-storage/.env
cat packages/encrypted-storage/.env
cp packages/encrypted-storage/.env packages/revocation-list-2020/.env

- 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

environment: ${{ (github.ref == 'refs/heads/dev' && 'dev') || (startsWith(github.ref, 'refs/tags/v') && 'UN') }}

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@v3
with:
role-to-assume: ${{ secrets.AWS_ROLE_GITHUB_ACTION_ARN }}
aws-region: ${{ vars.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: 114 additions & 0 deletions .github/workflows/deploy-to-s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: Deploy to S3

on:
push:
branches:
- dev
tags:
- '*'

env:
environment: ${{ (github.ref == 'refs/heads/dev' && 'dev') || (startsWith(github.ref, 'refs/tags/v') && 'UN') }}

jobs:
install_and_build:
runs-on: ubuntu-latest

env:
CI: false

environment: ${{ (github.ref == 'refs/heads/dev' && 'dev') || (startsWith(github.ref, 'refs/tags/v') && 'UN') }}

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

- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

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

- name: pnpm version
run: pnpm --version

- name: Install dependencies
run: pnpm install

- name: 'Create env file'
run: |
touch .env
echo REACT_APP_ENCRYPTED_ENDPOINT=${{ vars.REACT_APP_ENCRYPTED_ENDPOINT}} >> .env
echo REACT_APP_QRCODE_VERIFY_ENDPOINT=${{ vars.REACT_APP_QRCODE_VERIFY_ENDPOINT }} >> .env
echo REACT_APP_SCHEMA_URL=${{ vars.REACT_APP_SCHEMA_URL }} >> .env
echo REACT_APP_REMOTE_AGENT_API_KEY=${{ vars.REACT_APP_REMOTE_AGENT_API_KEY }} >> .env
echo REACT_APP_DEFAULT_AGENT_ID=${{ vars.REACT_APP_DEFAULT_AGENT_ID }} >> .env
cat .env

- name: Build explorer
run: pnpm build:js

- name: Archive artifacts
uses: actions/upload-artifact@v3
with:
name: explorer
path: packages/demo-explorer/build/explorer

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

permissions:
id-token: write

environment: ${{ (github.ref == 'refs/heads/dev' && 'dev') || (startsWith(github.ref, 'refs/tags/v') && 'UN') }}

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

- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: explorer
path: packages/demo-explorer/build/explorer

- name: Install AWS CLI
run: |
sudo apt-get -q install -y python3-pip
pip3 install awscli --upgrade --user

- name: Configure AWS Credentials
if: ${{ env.environment == 'dev' }}
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.AWS_ROLE_GITHUB_ACTION_ARN }}
aws-region: ${{ vars.AWS_REGION }}

- name: Configure AWS Credentials
if: ${{ env.environment == 'UN' }}
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}

- name: Deploy to S3
run: aws s3 sync packages/demo-explorer/build/explorer/ s3://${{ vars.AWS_S3_BUCKET_NAME }}/vckit/explorer/ --delete

- name: Invalidate CloudFront
run: |
DISTRIBUTION_ID=${{ vars.AWS_CLOUDFRONT_DISTRIBUTION_ID }}
INVALIDATION_PATH="/vckit/explorer/*"

# Create a new CloudFront invalidation
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths $INVALIDATION_PATH
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ data

local-database*.json

/scratch
/scratch

# logs
/logs
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
9 changes: 9 additions & 0 deletions aws/scripts/application_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
source /home/ec2-user/.bashrc
cd /home/ec2-user/agent_server

if [ ! -f /home/ec2-user/agent.yml ]; then
pnpm vckit config --filename /home/ec2-user/agent.yml
fi
# Start server
pm2 start pnpm -- vckit server --config /home/ec2-user/agent.yml
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
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
Loading