Create deploy.yml #13
Workflow file for this run
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 Spring Boot Application to EC2 | |
on: | |
push: | |
branches: [be/dev2-setting] | |
pull_request: | |
branches: [be/dev2-setting] | |
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' | |
distribution: 'adopt' # 오류나면 Correto로 변경하기 | |
- 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 clean build -x test | |
working-directory: /be/issue-tracker/ | |
- 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: ap-northeast-2 # 원하는 리전으로 수정 | |
- 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 |