Skip to content

Commit

Permalink
ci: MySQL 이미지 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyongp committed Oct 23, 2023
1 parent b749328 commit 9077be2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 32 deletions.
58 changes: 28 additions & 30 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,50 @@ jobs:
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
username: ${{ secrets.DK_USERNAME }}
password: ${{ secrets.DK_TOKEN }}

- name: Create .env file
run: |
cat <<- EOF > .env
TZ=${{ secrets.TZ }}
PORT=${{ secrets.PORT }}
NODE_ENV=${{ secrets.NODE_ENV }}
JWT_SECRET=${{ secrets.JWT_SECRET }}
DATABASE_URL=${{ secrets.DATABASE_URL }}
EOF
- 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.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}:latest
tags: ${{ secrets.DK_USERNAME }}/${{ secrets.DK_IMAGE }}:latest

- name: Send docker-compose.yml, .env to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
source: docker-compose.yml,.env
target: /home/ubuntu/

- name: Deploy Docker to EC2
uses: appleboy/[email protected]
env:
DOCKER_IMAGE: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}:latest
DOCKER_CONTAINER: ${{ secrets.DOCKER_IMAGE }}
PORT: ${{ secrets.PORT }}
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_KEY }}
envs: DOCKER_IMAGE,DOCKER_CONTAINER,PORT
script: |
cat <<- EOF > docker-compose.yml
version: "3.8"
services:
api:
image: $DOCKER_IMAGE
container_name: $DOCKER_CONTAINER
restart: always
ports:
- $PORT:$PORT
EOF
export $(cat .env | xargs)
sudo docker compose pull
sudo docker compose up -d
sudo docker container prune -f
sudo docker image prune -f
rm -f docker-compose.yml
rm -f docker-compose.yml .env
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cSpell.words": [
"allenvs",
"appleboy",
"envkey",
"lockb",
"unixtime"
]
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from --platform=linux/amd64 oven/bun

arg NODE_VERSION=18
run apt-get update && apt-get install -y curl
run apt-get update && apt-get install -y curl default-mysql-client
run curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
run bash n $NODE_VERSION
run rm n
Expand All @@ -11,10 +11,11 @@ workdir /home/bun/app

copy package.json bun.lockb tsconfig.json .env .
copy prisma ./prisma
copy scripts/*.sh ./
copy src ./src

run chmod +x ./*.sh .env
run bun install --production
run bunx prisma generate
run bun migrate

cmd ["bun", "start:prod"]
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3.8"

services:
api:
image: ${DK_USERNAME}/${DK_IMAGE}:latest
container_name: ${DK_IMAGE}
restart: always
ports:
- ${PORT}:${PORT}
depends_on:
- db
networks:
- api-network
command: ["/home/bun/app/wait-for-db.sh"]

db:
image: mysql:8.1.0
container_name: ${DK_IMAGE}-db
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_PASSWORD: ${DB_PASSWORD}
TZ: ${TZ}
volumes:
- mysql-data:/var/lib/mysql
ports:
- ${DB_PORT}:3306
networks:
- api-network
expose:
- ${DB_PORT}

volumes:
mysql-data:

networks:
api-network:
driver: bridge
11 changes: 11 additions & 0 deletions scripts/wait-for-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
. ./.env

while
mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASSWORD}" -e "show databases;" | grep -q "${DB_NAME}"
[ $? -ne 0 ]
do
echo "Waiting for database(${DB_PORT}) connection..."
sleep 2
done

bun migrate

0 comments on commit 9077be2

Please sign in to comment.