Skip to content

Commit

Permalink
Fix(#14): port 변경하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyun0713 authored Jul 18, 2024
1 parent c5fb0a3 commit 2ace47f
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions .github/workflows/deploy_production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,53 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Set up application.yml
run: echo "${{ secrets.PRODUCTION_APPLICATION }}" > ./src/main/resources/application.yml
- name: Set up application.yml
run: echo "${{ secrets.PRODUCTION_APPLICATION }}" > ./src/main/resources/application.yml

- name: Build with Gradle
run: ./gradlew build -x test
- name: Build with Gradle
run: ./gradlew build -x test

- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{ env.DOCKER_IMAGE_NAME }}:latest
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{ env.DOCKER_IMAGE_NAME }}:latest

- name: Login to Docker Hub using Access Token
run: echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Login to Docker Hub using Access Token
run: echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin

- name: Push the Docker image
run: docker push ${{ env.DOCKER_IMAGE_NAME }}:latest
- name: Push the Docker image
run: docker push ${{ env.DOCKER_IMAGE_NAME }}:latest

deploy-to-ec2:
needs: build-and-push-docker
runs-on: ubuntu-latest

steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_SSH_USER }}
key: ${{ env.PRIVATE_KEY }}
script: |
if [ $(sudo docker ps -q -f name=${{ env.CONTAINER_NAME_BLUE }}) ]; then
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name ${{ env.CONTAINER_NAME_GREEN }} -d -p ${{ env.GREEN_PORT }}:${{ env.GREEN_PORT }} -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}
sleep 30
sudo docker stop ${{ env.CONTAINER_NAME_BLUE }}
sudo docker rm ${{ env.CONTAINER_NAME_BLUE }}
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_SSH_USER }}
key: ${{ env.PRIVATE_KEY }}
script: |
if [ $(sudo docker ps -q -f name=${{ env.CONTAINER_NAME_BLUE }}) ]; then
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name ${{ env.CONTAINER_NAME_GREEN }} -d -p ${{ env.GREEN_PORT }}:${{ env.GREEN_PORT }} -e SPRING_PROFILES_ACTIVE=green -e SERVER_PORT=${{ env.GREEN_PORT }} -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}:latest
sleep 30
sudo docker stop ${{ env.CONTAINER_NAME_BLUE }}
sudo docker rm ${{ env.CONTAINER_NAME_BLUE }}
else
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name ${{ env.CONTAINER_NAME_BLUE }} -d -p ${{ env.BLUE_PORT }}:${{ env.BLUE_PORT }} -e SPRING_PROFILES_ACTIVE=blue -e SERVER_PORT=${{ env.BLUE_PORT }} -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}:latest
sleep 30
sudo docker stop ${{ env.CONTAINER_NAME_GREEN }}
sudo docker rm ${{ env.CONTAINER_NAME_GREEN }}
fi
sudo systemctl reload nginx
else
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name ${{ env.CONTAINER_NAME_BLUE }} -d -p ${{ env.BLUE_PORT }}:${{ env.BLUE_PORT }} -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}
sleep 30
sudo docker stop ${{ env.CONTAINER_NAME_GREEN }}
sudo docker rm ${{ env.CONTAINER_NAME_GREEN }}
sudo systemctl reload nginx
fi

0 comments on commit 2ace47f

Please sign in to comment.