Skip to content

Commit e24b34e

Browse files
authored
Update deploy.yml
1 parent 49ce1f3 commit e24b34e

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

.github/workflows/deploy.yml

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
1-
name: Deploy to EC2
1+
name: Build & Deploy Backend (GHCR → EC2)
22

33
on:
44
push:
55
branches: [ main ]
66

7+
permissions:
8+
contents: read
9+
packages: write
10+
711
jobs:
8-
deploy:
9-
name: Deploy to EC2 Server
12+
build-push-deploy:
1013
runs-on: ubuntu-latest
11-
1214
steps:
13-
- name: Checkout code
14-
uses: actions/checkout@v4
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-java@v4
17+
with:
18+
distribution: temurin
19+
java-version: 17
20+
- uses: actions/cache@v4
21+
with:
22+
path: |
23+
~/.gradle/caches
24+
~/.gradle/wrapper
25+
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*','**/gradle-wrapper.properties') }}
26+
- run: ./gradlew clean bootJar --no-daemon
27+
28+
- uses: docker/setup-buildx-action@v3
29+
- uses: docker/login-action@v3
30+
with:
31+
registry: ghcr.io
32+
username: ${{ github.actor }}
33+
password: ${{ secrets.GITHUB_TOKEN }}
1534

16-
- name: Set up SSH key
35+
- uses: docker/build-push-action@v6
36+
with:
37+
context: .
38+
push: true
39+
tags: |
40+
ghcr.io/dmu-debugvisual/debugvisual-backend:latest
41+
ghcr.io/dmu-debugvisual/debugvisual-backend:${{ github.sha }}
42+
43+
- name: Deploy on EC2 (compose pull/up)
44+
env:
45+
HOST: ${{ secrets.EC2_HOST }}
46+
USER: ${{ secrets.EC2_USER }}
1747
run: |
1848
mkdir -p ~/.ssh
1949
echo "${{ secrets.EC2_KEY }}" > ~/.ssh/id_rsa
50+
sed -i 's/\r$//' ~/.ssh/id_rsa
2051
chmod 600 ~/.ssh/id_rsa
21-
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
22-
23-
- name: SSH and deploy
24-
run: |
25-
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "\
26-
set -euo pipefail; \
27-
cd ~; \
28-
if [ ! -f ./deploy.sh ]; then echo '❌ deploy.sh not found in ~'; exit 1; fi; \
29-
sed -i 's/\r$//' ./deploy.sh || true; \
30-
chmod +x ./deploy.sh; \
31-
./deploy.sh"
52+
ssh-keyscan -H "$HOST" >> ~/.ssh/known_hosts
53+
ssh "$USER@$HOST" "\
54+
docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} && \
55+
cd ~/apps/debugvisual && \
56+
docker compose pull && docker compose up -d && \
57+
docker image prune -af || true"

0 commit comments

Comments
 (0)