Skip to content

Build Test and Deploy #1

Build Test and Deploy

Build Test and Deploy #1

name: Build and Test with Maven
on:
pull_request:
types: [opened, synchronize]
branches: ["main"]
paths:
- "springrolls/src/**"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build:
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: Setup SSH Key
run: |
echo "${{ secrets.DEV_KEY_PAIR }}" >> SpringKeyPair.pem
chmod 400 SpringKeyPair.pem
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Test with Maven
run: mvn test
- name: Deploy to EC2
run: |
scp -i "SpringKeyPair.pem" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${{ secrets.JAR_FILE }} ${{ secrets.DEV_INSTANCE_IP }}:${{ secrets.TARGET_DIR }}
# 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
cd springrolls
# Stop the existing application (if necessary)
pkill -f "java -jar springrolls-0.0.1-SNAPSHOT.jar"
# Start the new version of the application
java -jar springrolls-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &
exit
EOF