Java CI CD with Gradle #39
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: Java CI CD with Gradle | |
on: | |
workflow_dispatch: # 수동 실행 트리거 | |
# push: | |
# branches: ["main"] | |
# pull_request: | |
# branches: ["main"] | |
jobs: | |
build: | |
runs-on: ubuntu-latest # 워크플로우가 Ubuntu 최신 버전에서 실행 | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 # 리포지토리의 코드를 체크아웃 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Set Gradle Wrapper executable | |
run: chmod +x ./gradlew | |
- name: Execute remote ssh commands | |
uses: appleboy/ssh-action@master # ssh 연결을 위한 오픈 소스 액션 | |
with: | |
host: ${{ secrets.REMOTE_IP }} # 인스턴스 IP | |
username: ${{ secrets.REMOTE_USER }} # 우분투 ID | |
key: ${{ secrets.REMOTE_PRIVATE_KEY }} # EC2 인스턴스 pem key | |
port: ${{ secrets.REMOTE_SSH_PORT }} # 접속 포트 | |
script: | # 실행할 스크립트 | |
cd ./apap-back/cicd | |
git pull origin main | |
touch ./src/main/resources/application.properties # application.properties 파일 생성 | |
echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties | |
cat ./src/main/resources/application.properties | |
- name: Build with Gradle | |
run: ./gradlew build -x test | |
- name: Check Jar file at build/libs | |
run: | | |
ls | |
ls build/libs | |
pwd | |
chmod +x /home/runner/work/APAP-back/APAP-back/build/libs/apap-backend-0.0.1-SNAPSHOT.jar | |
- name: Check JAR file existence in /home/runner/work/APAP-back/APAP-back/build/libs | |
run: ls -l /home/runner/work/APAP-back/APAP-back/build/libs | |
- name: Copy JAR file to EC2 # 빌드한 파일 EC2 에 복사 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.REMOTE_IP }} # 인스턴스 IP | |
username: ${{ secrets.REMOTE_USER }} # 우분투 ID | |
key: ${{ secrets.REMOTE_PRIVATE_KEY }} # EC2 인스턴스 pem key | |
port: ${{ secrets.REMOTE_SSH_PORT }} # 접속 포트 | |
script: | # 실행할 스크립트 | |
cd ./apap-back/ | |
echo "${{ secrets.REMOTE_PRIVATE_KEY }}" > temp.pem | |
scp -i temp.pem /home/runner/work/APAP-back/APAP-back/build/libs/apap-backend-0.0.1-SNAPSHOT.jar ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_IP }}:/apap-back/ | |
rm temp.pem |