Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyun0713 authored Jul 17, 2024
2 parents 1b9cc7f + 96681ed commit 2bf6049
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/deploy_production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Deploy Production Server to AWS EC2 using Docker

on:
push:
branches:
- main

env:
DOCKER_IMAGE_NAME: ${{ secrets.PRODUCTION_DOCKER_IMAGE_NAME }}
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_SSH_USER: ec2-user
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
CONTAINER_NAME_BLUE: ${{ secrets.CONTAINER_NAME_BLUE }}
CONTAINER_NAME_GREEN: ${{ secrets.CONTAINER_NAME_GREEN }}
CURRENT_CONTAINER: ${{ secrets.CURRENT_CONTAINER }}
NEW_CONTAINER: ${{ secrets.NEW_CONTAINER }}
CURRENT_PORT: ${{ secrets.CURRENT_PORT }}
NEW_PORT: ${{ secrets.NEW_PORT }}


jobs:
build-and-push-docker:

runs-on: ubuntu-latest

steps:
- 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 application.yml
run: echo "${{ secrets.PRODUCTION_APPLICATION }}" > ./src/main/resources/application.yml

- 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: 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


deploy-to-ec2:

needs: build-and-push-docker
runs-on: ubuntu-latest

steps:
- name: Determine Active Environment
id: determine-env
run: |
if [ "${{ secrets.CURRENT_CONTAINER }}" == "${{ secrets.CONTAINER_NAME_BLUE }}" ]; then
echo "NEW_CONTAINER=${{ secrets.CONTAINER_NAME_GREEN }}" >> $GITHUB_ENV
echo "NEW_PORT=8088" >> $GITHUB_ENV
else
echo "NEW_CONTAINER=${{ secrets.CONTAINER_NAME_BLUE }}" >> $GITHUB_ENV
echo "NEW_PORT=8087" >> $GITHUB_ENV
fi
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_SSH_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
CURRENT_CONTAINER=${{ secrets.CURRENT_CONTAINER }}
NEW_CONTAINER=${{ env.NEW_CONTAINER }}
CURRENT_PORT=${{ secrets.CURRENT_PORT }}
NEW_PORT=${{ env.NEW_PORT }}
if [ $(sudo docker ps -q -f name=$NEW_CONTAINER) ]; then
sudo docker stop $NEW_CONTAINER
sudo docker rm $NEW_CONTAINER
fi
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name $NEW_CONTAINER -d -p $NEW_PORT:8087 -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}
echo "CURRENT_CONTAINER=$NEW_CONTAINER" >> $GITHUB_ENV
echo "CURRENT_PORT=$NEW_PORT" >> $GITHUB_ENV

0 comments on commit 2bf6049

Please sign in to comment.