Skip to content

Build and Deploy to EC2 #37

Build and Deploy to EC2

Build and Deploy to EC2 #37

name: Build and Deploy to EC2
# workflow_dispatch means this workflow is manually triggered.
# The user will select the branch or tag from the GitHub UI to run this workflow against.
on:
workflow_dispatch:
jobs:
run-tests:
uses: ./.github/workflows/run-tests.yml
build-api:
runs-on: ubuntu-latest
needs: run-tests
defaults:
run:
shell: bash
working-directory: ./api
outputs:
# sdist is a variable that will contain the filename of the API build package
# It will be used in the deployment job to know which file to deploy.
sdist: ${{ steps.package.outputs.sdist }}
steps:
- uses: actions/checkout@main
with:
fetch-depth: 500
fetch-tags: true
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
- name: Upgrade pip
run: python -m pip install --upgrade pip
- name: Build API Deployment Package
id: package
run: |
# It is designed that if $SDIST does not exists then this job will fail.
# $SDIST is used to set this workflow's sdist variable for use in the deploy job
python -m pip install build
SDIST=$(python -m build --sdist | perl -n -e '/^Successfully built (.*)\.tar\.gz$/ && print $1')
[ -n "$SDIST" ] && echo "sdist=$SDIST" >> "$GITHUB_OUTPUT"
- name: Archive API Deployment Package
uses: actions/upload-artifact@v3
with:
name: api
path: api/dist
retention-days: 7
build-app:
runs-on: ubuntu-latest
needs: run-tests
defaults:
run:
shell: bash
working-directory: ./app
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20
cache: "npm"
cache-dependency-path: app/package-lock.json
- name: Run npm CI
run: npm ci
- name: Build app
env:
# This environment variable is read by the app during the build
# It is a required variable to set the API url in the app
VITE_HUU_API_BASE_URL: ${{ vars.VITE_HUU_API_BASE_URL }}
run: npm run build
- name: Archive App
uses: actions/upload-artifact@v3
with:
name: app
path: app/dist
retention-days: 7
deploy-api-app:
needs: [build-api, build-app]
runs-on: ubuntu-latest
steps:
- name: Download all workflow run artifacts
uses: actions/download-artifact@v3
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.HUU_EC2_SSH_KEY }}
# The value below is the server's PUBLIC key. It's a required attribute for this action.
known_hosts: homeunite.us ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu/AdtdkNgDuezmkVeENDtC1Mf2erROKDEslMj+RFwuXj5CuLG2PRNTpzebgVlIJwxrq76+QWEFFG4gub2+mq2N+FlQ/if+R+a3Ym7lS3J25usgBliO6Dgp3Oxuq6n3V3/SopXIZ3/p8zGyBOiEjF8NXXy6y/ByfqT61jhZZR4MuMxdsaTbOI8wYfCAkxJTRn7E3U36iZNxgyxl5LCw97AxxiAzzg+f4GmpY7JuNy0EEqAEdRHPs6LBjrmDw6QLkDVS7AEA64yF8cDqDtNdB4Q/SkmJ0AyggU+fkFJ+wq01+mjtBMfjlaMmk+a2KowCIU6L+Mo2E7FnbQ0vKBg4iY3
- name: rsync over SSH API and App to EC2
run: |
# upload the api and app directories to the EC2 instance
rsync --mkpath -r app [email protected]:github-deploy/
rsync --mkpath -r api [email protected]:github-deploy/
# /home/ubuntu/github-deploy/api contains the sdist
# /home/ubuntu/github-deploy/app contains the frontend app source
- name: Configure EC2
uses: appleboy/[email protected]
env:
SDIST: ${{needs.build-api.outputs.sdist}}
with:
host: homeunite.us
username: ubuntu
key: ${{ secrets.HUU_EC2_SSH_KEY }}
script_stop: true
envs: SDIST
script: |
# TODO: Run database migration scripts
cd /home/ubuntu
./create_archive.bash
# Update database
# Deploy API
cd /home/ubuntu/github-deploy/api
source /opt/dev.homeunite.us/dev-huu-env/bin/activate
pip uninstall -y -r <(pip freeze) || echo "Nothing to uninstall"
pip install --upgrade pip
pip install -r <(tar xfO $SDIST.tar.gz $SDIST/requirements.txt)
pip install "$SDIST.tar.gz[prod]"
deactivate
sudo systemctl restart dev-homeuniteus-api.service
rm -rf /home/ubuntu/github-deploy/api/*
# Deploy front-end app
cd /home/ubuntu/github-deploy/app
sudo rsync -a * /var/www/dev.homeunite.us/html/
rm -r /home/ubuntu/github-deploy/app/*