Skip to content

fixed error

fixed error #14

name: Build Test and Deploy
on:
pull_request:
types: [opened, synchronize, closed]
branches: ["main"]
paths:
- "src/**"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: "17"
distribution: "temurin"
cache: maven
- name: Build with Maven
if: github.event_name == 'pull_request' && github.event.action != 'closed'
run: mvn -B package --file pom.xml
- name: Test with Maven
if: github.event_name == 'pull_request' && github.event.action != 'closed'
run: mvn test
- name: Upload JAR as Artifact
if: github.event_name == 'pull_request' && github.event.action != 'closed'
uses: actions/upload-artifact@v2
with:
name: springrolls
path: ${{ secrets.JAR_FILE }}
deploy:
runs-on: ubuntu-latest
needs: build_test
if: github.event_name == 'pull_request' && github.event.action == 'closed'
steps:
- name: Set up SSH Key
run: |
echo "${{ secrets.DEV_KEY_PAIR }}" >> SpringKeyPair.pem
chmod 400 SpringKeyPair.pem
- name: Download JAR Artifact
uses: actions/download-artifact@v2
with:
name: springrolls
path: ${{ secrets.JAR_FILE }}
- name: Deploy to EC2
if: github.event.pull_request.merged == true
run: |
echo "Copying jar to EC2 Instance..."
scp -i "SpringKeyPair.pem" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${{ secrets.JAR_FILE }} ${{ secrets.DEV_INSTANCE_IP }}:${{ secrets.TARGET_DIR }}
echo "Jar Copied successfully!!!"
echo "Attempting logging to machine..."
# SSH into the EC2 instance and restart the application
ssh -i "SpringKeyPair.pem" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${{ secrets.DEV_INSTANCE_IP }} <<EOF
echo "Switching to application directory"
cd springrolls
echo "Stopping the existing application"
pkill -f "java -jar springrolls-0.0.1-SNAPSHOT.jar"
echo "Start the new version of the application"
nohup java -jar springrolls-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &
echo "Application Started!!!"
exit
EOF