Update gradle.yml #60
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: Deploy JAR to EC2 using SCP Action | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_KEY }} | |
source: "build/libs/spotserver-0.0.1-SNAPSHOT.jar" | |
target: "/home/${{ secrets.EC2_USER }}/" | |
- name: Execute commands on EC2 | |
run: | | |
ssh -i ~/.ssh/id_rsa -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 |