Skip to content

Merge pull request #1 from Central-MakeUs/spec/v1 #1

Merge pull request #1 from Central-MakeUs/spec/v1

Merge pull request #1 from Central-MakeUs/spec/v1 #1

Workflow file for this run

name: Deploy Java Application to EC2
on:
push:
branches:
- dev # Trigger on pushes to the main branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# Step 1: Check out the repository
- name: Checkout Code
uses: actions/checkout@v3
# Step 2: Set up Java environment
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '17' # Adjust based on your project requirements
- name: Test Project
run: |
./gradlew test
# Step 3: Build the application
- name: Build JAR
run: |
./gradlew clean build
# Step 4: Transfer the JAR to the EC2 instance
- name: Transfer JAR to EC2
env:
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
EC2_USER: ${{ secrets.EC2_USER }}
EC2_HOST: ${{ secrets.EC2_HOST }}
run: |
scp -i <(echo "$EC2_SSH_KEY") -o StrictHostKeyChecking=no \
build/libs/api-0.0.1-SNAPSHOT.jar $EC2_USER@$EC2_HOST:/home/$EC2_USER/
# TODO: The name of jar file might change. Need other way to handle this.
# Step 5: Deploy the application on the EC2 instance
- name: Deploy Application on EC2
env:
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
EC2_USER: ${{ secrets.EC2_USER }}
EC2_HOST: ${{ secrets.EC2_HOST }}
run: |
ssh -i <(echo "$EC2_SSH_KEY") -o StrictHostKeyChecking=no \
$EC2_USER@$EC2_HOST << EOF
pkill -f api-0.0.1-SNAPSHOT.jar || true
nohup java -jar /home/$EC2_USER/your-application.jar > app.log 2>&1 &
EOF