Skip to content

Deploy API Server

Deploy API Server #63

Workflow file for this run

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 network create api-network
sudo docker network create api-network
sudo docker run --network api-network --name ${DK_IMAGE}-db \
-e MYSQL_DATABASE=${DB_DATABASE} -e MYSQL_PASSWORD=${DB_PASSWORD} -e TZ=${TZ} \
-v mysql-data:/var/lib/mysql -p ${DB_PORT}:3306 -d mysql:8.1.0 --default-authentication-plugin=mysql_native_password
sudo docker run --network api-network --name ${DK_IMAGE} \
--link ${DK_IMAGE}-db:db -p ${PORT}:${PORT} -d ${DK_USERNAME}/${DK_IMAGE}:latest /home/bun/app/wait-for-db.sh
sudo docker container prune -f
sudo docker image prune -f