Merge pull request #32 from DigiLabChallengeHackathon/rel/api #25
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
cache: 'gradle' | |
- name: Build without tests | |
run: | | |
set -x # Enable debug mode | |
./gradlew clean assemble || { echo "Gradle build failed"; exit 1; } | |
- name: Check JAR file existence | |
run: | | |
set -x # Enable debug mode | |
JAR_FILE=$(find build/libs -name "bewavoca-backend-0.0.1-SNAPSHOT.jar") | |
if [ -z "$JAR_FILE" ]; then | |
echo "No JAR file found!" | |
exit 1 | |
fi | |
echo "JAR file found: $JAR_FILE" | |
- name: Deploy to Server | |
if: success() | |
env: | |
SSH_KEY: ${{ secrets.SSH_KEY }} | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
DATABASE_URL: ${{ secrets.DATABASE_URL }} | |
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }} | |
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} | |
run: | | |
echo "DATABASE_URL: $DATABASE_URL" | |
echo "DATABASE_USERNAME: $DATABASE_USERNAME" | |
# Start ssh-agent and add the private key | |
eval "$(ssh-agent -s)" | |
echo "$SSH_KEY" | tr -d '\r' > private_key.pem | |
chmod 600 private_key.pem | |
ssh-add private_key.pem | |
# Disable strict host key checking | |
ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST << 'EOF' | |
echo "Stopping existing application if running..." | |
pkill -f 'java -jar' || true | |
echo "Cleaning up old files..." | |
rm -rf ~/bewavoca-backend/build/libs/* | |
EOF | |
# Copy built JAR file to the server | |
scp -r build/libs/bewavoca-backend-0.0.1-SNAPSHOT.jar $SSH_USER@$SSH_HOST:~/bewavoca-backend/build/libs/ | |
# Start the application on the server | |
ssh $SSH_USER@$SSH_HOST << 'EOF' | |
pkill -f 'java -jar' || true | |
cd ~/bewavoca-backend/build/libs | |
nohup java -jar -Dspring.profiles.active=prod bewavoca-backend-0.0.1-SNAPSHOT.jar & | |
EOF | |
- name: Clean up | |
if: success() | |
run: rm -f private_key.pem |