-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,52 +22,54 @@ 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: /tmp | ||
|
||
- 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_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) | ||
ls -al | ||
env | ||
docker compose pull | ||
docker compose up -d | ||
docker container prune -f | ||
docker image prune -f | ||
rm -f docker-compose.yml .env |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"cSpell.words": [ | ||
"allenvs", | ||
"appleboy", | ||
"envkey", | ||
"lockb", | ||
"unixtime" | ||
] | ||
|
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
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
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 |
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
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 |