[Setting] CI/CD 구축을 위한 gradle.yml 생성 및 수정 #67
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: Java CI/CD with Gradle | |
on: | |
push: | |
branches: [ "dev" ] | |
pull_request: | |
branches: [ "dev" ] | |
permissions: | |
contents: read | |
jobs: | |
build: | |
name: Build | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Checkout branch | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Run chmod to make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Build with Gradle | |
run: ./gradlew clean build -x test | |
- name: Upload Build artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: build-artifacts | |
path: | | |
build/libs/*.jar | |
deploy: | |
needs: build | |
name: Deploy | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Download Build artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
name: build-artifacts | |
path: build/libs/ | |
- name: Install sshpass | |
run: sudo apt-get update && sudo apt-get install -y sshpass | |
- name: Deploy JAR to EC2 using sshpass | |
run: | | |
sshpass -p ${{ secrets.EC2_PASSWORD }} scp -o StrictHostKeyChecking=no build/libs/spotserver-0.0.1-SNAPSHOT.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USER }}/ | |
- name: Execute commands on EC2 using sshpass | |
run: | | |
sshpass -p ${{ secrets.EC2_PASSWORD }} ssh -T -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' | |
# Set PATH if needed | |
export PATH=/usr/bin:/bin:/sbin:/usr/sbin | |
# Start Redis server | |
if ! systemctl is-active --quiet redis-server; then | |
sudo systemctl start redis-server | |
fi | |
# Kill any process on port 8080 if needed | |
if sudo lsof -i tcp:8080; then | |
sudo fuser -k -n tcp 8080 | |
fi | |
# Backup existing JAR | |
if [ -f /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar ]; then | |
mv /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar.bak | |
fi | |
# Run new application | |
nohup java -jar /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar \ | |
--spring.config.location=/home/${{ secrets.EC2_USER }}/app.env \ | |
> /home/${{ secrets.EC2_USER }}/nohup.out 2>&1 & | |
# Monitor application startup logs | |
tail -n 20 /home/${{ secrets.EC2_USER }}/nohup.out | |
EOF |