Update prod-deploy-container.yml #6
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 EC2 on develop | |
on: | |
push: | |
branches: [ "develop" ] | |
pull_request: | |
branches: [ "develop" ] | |
jobs: | |
build-and-push-image: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
# 소스 코드 체크아웃 | |
- uses: actions/checkout@v4 | |
# JDK 설정 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# Gradle 설정 | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v4 | |
with: | |
gradle-version: '7.3' | |
# Gradle Wrapper 실행 권한 추가 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
# Gradle 빌드 | |
- name: Build with Gradle Wrapper | |
run: ./gradlew build | |
# Docker 이미지 빌드 | |
- name: Build Docker image | |
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} . | |
# Docker Hub에 로그인 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# Docker 이미지 푸시 | |
- name: Push Docker image to Docker Hub | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
deploy-to-ec2: | |
needs: build-and-push-image | |
runs-on: ubuntu-latest | |
steps: | |
# SSH 비밀 키 설정 | |
- name: Install SSH Key | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
# EC2에 접속하여 Docker Compose 실행 | |
- name: Deploy on EC2 | |
env: | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
EC2_USER: ${{ secrets.EC2_USER }} | |
run: | | |
ssh -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << EOF | |
# Docker Compose로 컨테이너 중지 및 업데이트 | |
cd /path/to/your/docker-compose/directory # docker-compose.yml 파일 위치로 이동 | |
docker-compose down # 기존 컨테이너 중지 | |
docker-compose pull # 최신 이미지 가져오기 | |
docker-compose up -d # 컨테이너 다시 실행 | |
EOF | |
dependency-submission: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# Generates and submits a dependency graph, enabling Dependabot Alerts for all project dependencies. | |
# See: https://github.com/gradle/actions/blob/main/dependency-submission/README.md | |
- name: Generate and submit dependency graph | |
uses: gradle/actions/dependency-submission@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 |