Merge pull request #47 from Chapter-1/dev #13
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 | |
on: | |
push: | |
branches: [ main ] # main 브랜치에 push될 때 실행 | |
pull_request: | |
types: [closed] # PR이 닫힐 때 | |
branches: [ main ] # main 브랜치로의 PR일 때 | |
jobs: | |
deploy: | |
# PR이 merge되어 닫힌 경우에만 실행 | |
if: github.event.pull_request.merged == true || github.event_name == 'push' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
- name: Build with Gradle | |
run: ./gradlew clean bootJar | |
# 빌드된 JAR 파일 확인 | |
- name: List build directory | |
run: ls -la build/libs | |
# EC2 서버에 SSH 접속하기 위한 키 파일 생성 | |
- name: Create SSH Directory | |
run: mkdir -p ~/.ssh | |
- name: Add SSH Key | |
run: | | |
echo "${{ secrets.AWS_SSH_KEY }}" > ~/.ssh/key.pem | |
chmod 600 ~/.ssh/key.pem | |
# 빌드된 JAR 파일을 EC2로 전송 및 배포 | |
- name: Deploy to EC2 | |
run: | | |
# SSH 연결시 호스트 키 검증 건너뛰기 | |
echo "StrictHostKeyChecking no" >> ~/.ssh/config | |
# JAR 파일 전송 전 디렉토리 생성 | |
ssh -i ~/.ssh/key.pem ${{ secrets.AWS_USERNAME }}@${{ secrets.AWS_HOST }} "mkdir -p ~/blueprint/build/libs" | |
# EC2로 JAR 파일 전송 | |
scp -i ~/.ssh/key.pem \ | |
build/libs/*.jar \ | |
${{ secrets.AWS_USERNAME }}@${{ secrets.AWS_HOST }}:~/blueprint/build/libs/ | |
# 배포 스크립트 실행 | |
ssh -i ~/.ssh/key.pem ${{ secrets.AWS_USERNAME }}@${{ secrets.AWS_HOST }} \ | |
"cd ~/blueprint && ./script/deploy.sh" |