Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions .github/workflows/spring-cd.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
name: CD on PR Merged to develop

on:
pull_request:
types: [closed]
push:
branches: [develop]

jobs:
build:
if: github.event.pull_request.merged == true
name: Build Application
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
Expand All @@ -27,20 +25,27 @@ jobs:
run: ./gradlew build

- name: Upload JAR as artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: app-jar
path: build/libs/*.jar
retention-days: 1

deploy:
needs: build
name: Deploy to EC2
runs-on: ubuntu-latest

needs: build
steps:
- name: Download JAR artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: app-jar
path: ./artifacts

- name: Add EC2 to known hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts

- name: Set up SSH Agent
uses: webfactory/ssh-agent@v0.9.0
Expand All @@ -49,11 +54,12 @@ jobs:

- name: Copy JAR to EC2
run: |
scp -o StrictHostKeyChecking=no app-jar/*.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/ubuntu/app/app.jar
scp ./artifacts/*.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/ubuntu/app/app.jar

- name: Restart App with Docker Compose
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
cd /home/ubuntu/app
docker compose down
docker compose up -d --force-recreate
EOF