๐จ [Fix] file ๊ฒฝ๋ก ํ ์คํธ ๋ก๊ทธ ์ถ๊ฐ #32
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: Deploy to AWS EC2 using Docker | |
on: | |
push: | |
branches: | |
- main | |
env: | |
DOCKER_IMAGE_NAME: khyaejin/pnd | |
EC2_HOST: 13.124.4.73 | |
EC2_SSH_USER: ubuntu | |
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
CONTAINER_NAME: pnd-container | |
# ์์๋ก application.properties์์ ๋ฏผ๊ฐํ ์ ๋ณด ๊ด๋ฆฌ | |
# # DB ์ค์ | |
# DB_URL: ${{ secrets.DB_URL }} | |
# DB_USERNAME: ${{ secrets.DB_USERNAME }} | |
# DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
# | |
# # S3 ์ค์ | |
# ACCESS_KEY: ${{ secrets.ACCESS_KEY }} | |
# SECRET_KEY: ${{ secrets.SECRET_KEY }} | |
# GITHUB_REPORT_IMAGE_BUKET_NAME: ${{ secrets.GITHUB_REPORT_IMAGE_BUKET_NAME }} | |
# USER_IMAGE_BUKET_NAME: ${{ secrets.USER_IMAGE_BUKET_NAME }} | |
# REGION: ${{ secrets.REGION }} | |
# | |
# # JWT ์ค์ | |
# JWT_SECRET: ${{ secrets.JWT_SECRET }} | |
# JWT_REDIRECT_URI: ${{ secrets.JWT_REDIRECT_URI }} | |
# ACCESS_TOKEN_EXPIRATION_TIME: ${{ secrets.ACCESS_TOKEN_EXPIRATION_TIME }} | |
# REFRESH_TOKEN_EXPIRATION_TIME: ${{ secrets.REFRESH_TOKEN_EXPIRATION_TIME }} | |
# | |
# # GitHub OAuth2 ์ค์ | |
# OAUTH_GITHUB_CLIENT_ID: ${{ secrets.OAUTH_GITHUB_CLIENT_ID }} | |
# OAUTH_GITHUB_CLIENT_SECRET: ${{ secrets.OAUTH_GITHUB_CLIENT_SECRET }} | |
# OAUTH_GITHUB_SCOPE: ${{ secrets.OAUTH_GITHUB_SCOPE }} | |
# OAUTH_GITHUB_REDIRECT_URI: ${{ secrets.OAUTH_GITHUB_REDIRECT_URI }} | |
# OAUTH_GITHUB_AUTHORIZATION_GRANT_TYPE: ${{ secrets.OAUTH_GITHUB_AUTHORIZATION_GRANT_TYPE }} | |
# OAUTH_GITHUB_TOKEN_URI: ${{ secrets.OAUTH_GITHUB_TOKEN_URI }} | |
# | |
# # OpenAI API ์ค์ | |
# OPEN_AI_KEY: ${{ secrets.OPEN_AI_KEY }} | |
# OPEN_AI_URL: ${{ secrets.OPEN_AI_URL }} | |
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' | |
# SSH ์์ด์ ํธ ์์ ๋ฐ ํ๋ผ์ด๋น ํค ์ถ๊ฐ | |
- name: Start SSH Agent | |
uses: webfactory/ssh-agent@v0.5.3 | |
with: | |
ssh-private-key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
# application-secret.properties ์ค์ | |
- name: Set up application.properties | |
run: | | |
echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties | |
- name: Build with Gradle | |
run: ./gradlew build | |
- 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: Start SSH Agent | |
uses: webfactory/ssh-agent@v0.5.3 | |
with: | |
ssh-private-key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.EC2_HOST }} | |
username: ${{ env.EC2_SSH_USER }} | |
key: ${{ env.PRIVATE_KEY }} | |
script: | | |
# ๊ธฐ์กด ์ปจํ ์ด๋๊ฐ ์๋์ง ํ์ธํ๊ณ ์ญ์ | |
CONTAINER_ID=$(sudo docker ps -a -q --filter "name=${{ env.CONTAINER_NAME }}") | |
if [ ! -z "$CONTAINER_ID" ]; then | |
echo "Stopping and removing existing container $CONTAINER_ID" | |
sudo docker stop $CONTAINER_ID || true | |
sudo docker rm $CONTAINER_ID || true | |
fi | |
# ์ต์ ์ด๋ฏธ์ง๋ก Docker ์ปจํ ์ด๋ ์คํ | |
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }} | |
sudo docker run --name ${{ env.CONTAINER_NAME }} -d -p 80:8080 -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }} |