Skip to content

๐Ÿ”€ dev branch์™€ sync ๋งž์ถค (#9) #17

๐Ÿ”€ dev branch์™€ sync ๋งž์ถค (#9)

๐Ÿ”€ dev branch์™€ sync ๋งž์ถค (#9) #17

Workflow file for this run

name: Build and Push Docker Image and Deploy
# main, dev ๋ธŒ๋žœ์น˜์— push or PR ์ด ์˜ค๋ฉด ์‹คํ–‰
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
jobs:
# ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ, ํ‘ธ์‹œ
build_and_push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# DB ์„ธํŒ… ์ •๋ณด ์ž…๋ ฅ
- name: Set up application.yml
run: |
echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.yml
echo "${{ secrets.APPLICATION_TEST }}" > ./src/main/resources/application-test.yml
# SSL ์ ์šฉ
- name: Create SSH Key File
run: echo "${{ secrets.PRIVATE_KEY }}" > /tmp/private_key.pem
- name: Set Permissions for SSH Key
run: chmod 600 /tmp/private_key.pem
- name: Copy keystore.p12 from EC2
run: scp -o StrictHostKeyChecking=no -i /tmp/private_key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USER }}/keystore.p12 ./src/main/resources/keystore.p12
# ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ์šฉ ํ™˜๊ฒฝ ์„ธํŒ… ๋ฐ ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ
- name: set up test DB and docker build
run: |
docker compose -f docker-compose-auth-test-db.yml up -d # ๋„์ปค ์ปดํฌ์ฆˆํŒŒ์ผ๋กœ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ ์„ธํŒ…
DOCKER_BUILDKIT=0 docker build --network testNet -t ${{ secrets.DOCKER_IMAGE_NAME }}:latest . # ๋„์ปค ๋นŒ๋“œ (๋นŒ๋“œ ๊ณผ์ •์—์„œ ๋„คํŠธ์›Œํฌ ์‚ฌ์šฉ์„ ์œ„ํ•ด ๋นŒ๋“œํ‚ท 0)
docker compose -f docker-compose-auth-test-db.yml down # ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ ์ œ๊ฑฐ (๋„คํŠธ์›Œํฌ๊นŒ์ง€ ์‚ญ์ œ๋จ)
# ๋„์ปค ๋กœ๊ทธ์ธ
- name: docker Login
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# ๋„์ปค ์ด๋ฏธ์ง€ push
- name: push docker images
run: |
docker push ${{ secrets.DOCKER_IMAGE_NAME }}:latest
# ๋„์ปค ์ด๋ฏธ์ง€ EC2 ์ธ์Šคํ„ด์Šค์— ๋ฐฐํฌ
deploy_to_ec2:
needs: build_and_push
runs-on: ubuntu-24.04
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }} # EC2 IP ์ฃผ์†Œ
username: ${{ secrets.EC2_USER }} # EC2 ์‚ฌ์šฉ์ž
key: ${{ secrets.PRIVATE_KEY }} # pem ํ‚ค
# ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€
script: |
CONTAINER_ID=$(sudo docker ps -aq --filter "name=kaboo-auth")
if [ ! -z "$CONTAINER_ID" ]; then
sudo docker stop $CONTAINER_ID || true
sudo docker rm -f $CONTAINER_ID || true
fi
# ์ตœ์‹  ๋„์ปค ์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰
sudo docker pull ${{ secrets.DOCKER_IMAGE_NAME }}:latest # ๋„์ปค ์ตœ์‹  ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
# ๋„์ปค ์ด๋ฏธ์ง€ ์‹คํ–‰ (host.docker.internal ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก)
docker run --name kaboo-auth -d --add-host host.docker.internal:host-gateway -p 8081:8081 ${{ secrets.DOCKER_IMAGE_NAME }}:latest
sudo docker image prune -f # ๊ตฌ๋ฒ„์ „์˜ ๋„์ปค ์ด๋ฏธ์ง€ ์ œ๊ฑฐ