Test Deploy #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 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@v4 | |
with: | |
name: campushelper | |
path: build/libs/ | |
- name: Configure SSH | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/key.pem | |
chmod 600 ~/.ssh/key.pem | |
echo -e "Host *\n StrictHostKeyChecking no\n" > ~/.ssh/config | |
- name: Deploy to EC2 | |
run: | | |
scp -i ~/.ssh/key.pem build/libs/campushelper-0.0.1-SNAPSHOT.jar ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USERNAME }}/campushelper.jar | |
ssh -i ~/.ssh/key.pem ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }} ' | |
pid=$(pgrep java) | |
if [ -n "$pid" ]; then | |
kill -15 $pid | |
sleep 5 | |
fi | |
nohup java -jar /home/${{ secrets.EC2_USERNAME }}/campushelper.jar > app.log 2>&1 & | |
' | |