Merge pull request #3 from Central-MakeUs/spec/v1 #6
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: Deploy Java Application to EC2 | |
on: | |
push: | |
branches: | |
- dev # Trigger on pushes to the main branch | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Set up JDK | |
uses: actions/setup-java@v3 | |
with: | |
java-version: 17 | |
distribution: temurin | |
java-package: jdk | |
check-latest: false | |
server-id: github | |
server-username: ${{ github.actor }} | |
server-password: ${{ secrets.GITHUB_TOKEN }} | |
overwrite-settings: true | |
- name: Build JAR | |
run: | | |
./gradlew clean build | |
- name: Prepare SSH Key | |
env: | |
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | |
run: | | |
echo "$EC2_SSH_KEY" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
- name: Transfer JAR to EC2 | |
env: | |
EC2_USER: ${{ secrets.EC2_USER }} | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
run: | | |
scp -i ec2_key.pem -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. | |
- 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 ec2_key.pem -o StrictHostKeyChecking=no \ | |
$EC2_USER@$EC2_HOST << EOF | |
pkill -f api-0.0.1-SNAPSHOT.jar || true | |
nohup java -jar /home/$EC2_USER/api-0.0.1-SNAPSHOT.jar > app.log 2>&1 & | |
EOF | |