Skip to content

Test Deploy

Test Deploy #8

Workflow file for this run

name: Deploy to EC2
on:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
#JDK 설정 21
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
# 빌드 시간단축을 위한 그래들 캐싱
# - path: 캐시의 저장과 복원에 사용되는 runner 내 파일 경로
# - key: 캐시를 저장, 복원에 사용되는 키. 여러 값들을 조합해서 512자 제한으로 생성할 수 있다.
# - restore-keys: 내가 설정한 key로 cache miss가 발생할때 사용할 수 있는 후보군 키들
- name: Gradle Caching
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# gradlew 파일에 실행 권한을 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Gradle을 사용해 프로젝트를 빌드
- name: Build with Gradle
run: ./gradlew build -x test
# 빌드된 JAR 파일을 GitHub Actions의 아티팩트로 업로드
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: campushelper
path: build/libs/campushelper-0.0.1-SNAPSHOT.jar
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Download build artifact
uses: actions/download-artifact@v3
with:
name: campushelper
path: build/libs/
- name: Deploy to EC2
run: |
echo "${{ secrets.EC2_SSH_KEY }}" > private_key.pem
chmod 600 private_key.pem
scp -i private_key.pem -o StrictHostKeyChecking=no build/libs/campushelper-0.0.1-SNAPSHOT.jar ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USERNAME }}/campushelper.jar
ssh -i private_key.pem -o StrictHostKeyChecking=no ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }} "pgrep java | xargs kill -9; nohup java -jar /home/${{ secrets.EC2_USERNAME }}/campushelper.jar > app.log 2>&1 &"
rm -f private_key.pem