๐ Docs: README.md ์ถ๊ฐ (#14) #33
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
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 # ๊ตฌ๋ฒ์ ์ ๋์ปค ์ด๋ฏธ์ง ์ ๊ฑฐ |