Update deploy.yml #3
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
yamlCopy code | ||
name: Deploy Spring Boot Application to EC2 | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
build_and_deploy: | ||
name: Build and Deploy | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v2 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v2 | ||
with: | ||
java-version: '17' | ||
- name: Cache Gradle packages | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.gradle/caches | ||
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
gradle-${{ runner.os }} | ||
- name: Build with Gradle | ||
run: ./gradlew build -x test | ||
- name: Archive JAR | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: spring-boot-app | ||
path: build/libs/*.jar | ||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: us-east-1 # 원하는 리전으로 수정 | ||
- name: Set up SSH Key | ||
uses: webfactory/[email protected] | ||
with: | ||
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
- name: Transfer JAR to EC2 (Root 계정 사용 시) | ||
run: | | ||
scp -o StrictHostKeyChecking=no root@${{ secrets.EC2_HOST }}:/root/spring-boot-app.jar | ||
- name: Deploy on EC2 (Root 계정 사용 시) | ||
run: | | ||
ssh -o StrictHostKeyChecking=no root@${{ secrets.EC2_HOST }} << 'EOF' | ||
# Stop existing application | ||
pkill -f 'java.*spring-boot-app.jar' | ||
# Start new application | ||
export DB_USERNAME=${{ secrets.DB_USERNAME }} | ||
export DB_PASSWORD=${{ secrets.DB_PASSWORD }} | ||
nohup java -jar /root/spring-boot-app.jar > /dev/null 2>&1 & | ||
EOF |