-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to: #5
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 | ||
# 도커 이미지 빌드용 환경 세팅 및 도커 이미지 빌드 | ||
- 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-chat-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 # 구버전의 도커 이미지 제거 |