diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 553bbc6..e3d9c79 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -22,52 +22,52 @@ 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/create-envfile@v2.0 + 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/scp-action@v0.1.4 + with: + host: ${{ secrets.EC2_HOST }} + username: ubuntu + key: ${{ secrets.EC2_KEY }} + source: docker-compose.yml,.env + target: /tmp - name: Deploy Docker to EC2 uses: appleboy/ssh-action@v1.0.0 - 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_stop: true script: | - cat <<- EOF > docker-compose.yml - version: "3.8" - - services: - api: - image: $DOCKER_IMAGE - container_name: $DOCKER_CONTAINER - restart: always - ports: - - $PORT:$PORT - EOF - - sudo docker compose pull - sudo docker compose up -d - sudo docker container prune -f - sudo docker image prune -f - rm -f docker-compose.yml + cd /tmp + export $(cat .env | xargs) + docker compose pull + docker compose up -d + docker container prune -f + docker image prune -f + rm -f docker-compose.yml .env diff --git a/.vscode/settings.json b/.vscode/settings.json index 689a2ad..769962a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,6 +16,7 @@ "cSpell.words": [ "allenvs", "appleboy", + "envkey", "lockb", "unixtime" ] diff --git a/Dockerfile b/Dockerfile index ac64752..1e2514f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..19d087c --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/scripts/wait-for-db.sh b/scripts/wait-for-db.sh new file mode 100644 index 0000000..bb2c56d --- /dev/null +++ b/scripts/wait-for-db.sh @@ -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