Deploy API Server #68
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy API Server | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'package.json' | |
- 'bun.lockb' | |
- 'tsconfig.json' | |
- 'Dockerfile' | |
- 'prisma/**' | |
- 'src/**' | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DK_USERNAME }} | |
password: ${{ secrets.DK_TOKEN }} | |
- name: Create .env file with Secrets | |
uses: SpicyPizza/[email protected] | |
with: | |
envkey_DB_DATABASE: ${{ secrets.DB_DATABASE }} | |
envkey_DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
envkey_DB_HOST: ${{ secrets.DB_HOST }} | |
envkey_DB_PORT: ${{ secrets.DB_PORT }} | |
envkey_DB_USER: ${{ secrets.DB_USER }} | |
envkey_DK_IMAGE: ${{ secrets.DK_IMAGE }} | |
envkey_DK_USERNAME: ${{ secrets.DK_USERNAME }} | |
envkey_NODE_ENV: ${{ secrets.NODE_ENV }} | |
envkey_JWT_SECRET: ${{ secrets.JWT_SECRET }} | |
envkey_PORT: ${{ secrets.PORT }} | |
envkey_TZ: ${{ secrets.TZ }} | |
- name: Build and push | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: ${{ secrets.DK_USERNAME }}/${{ secrets.DK_IMAGE }}:latest | |
- name: Deploy Docker to EC2 | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ubuntu | |
key: ${{ secrets.EC2_KEY }} | |
script_stop: true | |
script: | | |
sudo docker pull mysql:8.1.0 | |
sudo docker pull ${{ secrets.DK_USERNAME }}/${{ secrets.DK_IMAGE }}:latest | |
sudo docker rm -f ${{ secrets.DK_IMAGE }} | |
sudo docker rm -f ${{ secrets.DK_IMAGE }}-db | |
sudo docker run --name ${{ secrets.DK_IMAGE }}-db \ | |
-e MYSQL_DATABASE=${{ secrets.DB_DATABASE }} -e MYSQL_ROOT_PASSWORD=${{ secrets.DB_PASSWORD }} -e TZ=${{ secrets.TZ }} \ | |
-v mysql-data:/var/lib/mysql -p ${{ secrets.DB_PORT }}:3306 -d mysql:8.1.0 --default-authentication-plugin=mysql_native_password | |
sudo docker run --name ${{ secrets.DK_IMAGE }} \ | |
--link ${{ secrets.DK_IMAGE }}-db:db -p ${{ secrets.PORT }}:${{ secrets.PORT }} \ | |
-d ${{ secrets.DK_USERNAME }}/${{ secrets.DK_IMAGE }}:latest /home/bun/app/wait-for-db.sh | |
sudo docker container prune -f | |
sudo docker image prune -f |