Skip to content

Deploy to Staging EC2 Instance #106

Deploy to Staging EC2 Instance

Deploy to Staging EC2 Instance #106

Workflow file for this run

name: Deploy to Staging EC2 Instance
on:
workflow_dispatch:
inputs:
branch:
description: 'Enter github branch name to be deployed'
required: true
default: 'main'
description:
description: 'Enter description about deployment'
required: true
default: 'New feature release'
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.branch }}
- name: Install Java 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
- name: Build with Maven
run: |-
./mvnw clean package -DskipTests
- name: Copy Jar to Server
env:
STAGING_PORT: ${{ secrets.STAGING_PORT }}
STAGING_USERNAME: ${{ secrets.STAGING_USERNAME }}
STAGING_HOST: ${{ secrets.STAGING_HOST }}
STAGING_PRIVATE_KEY: ${{ secrets.STAGING_PRIVATE_KEY }}
run : |-
echo "${{ secrets.STAGING_PRIVATE_KEY }}" >> staging.pem
chmod 600 staging.pem
scp -o StrictHostKeyChecking=no -i staging.pem -P ${{ secrets.STAGING_PORT }} target/$(ls target | grep "\.jar$") ${{ secrets.STAGING_USERNAME }}@${{ secrets.STAGING_HOST }}:/home/ubuntu/salesparrow-api/
- name: Restart server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.STAGING_HOST }}
username: ${{ secrets.STAGING_USERNAME }}
key: ${{ secrets.STAGING_PRIVATE_KEY }}
port: ${{ secrets.STAGING_PORT }}
script: |
cd salesparrow-api
echo "Moving jar file"
echo $(ls | grep "\.jar$" | tail -n 1)
mv $(ls | grep "\.jar$" | tail -n 1) app.jar
chmod 777 app.jar
sudo systemctl stop app.service
sudo systemctl start app.service