Skip to content

Commit 36d002d

Browse files
authored
feat(deploy): add deployment ci/cd workflow (#112)
* feat(deploy): add deployment ci/cd workflow * fix: frontend only / backend only changes
1 parent 956a5f2 commit 36d002d

1 file changed

Lines changed: 94 additions & 12 deletions

File tree

‎.github/workflows/deploy.yml‎

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
name: Push to production
1+
name: Deploy CMS
22

33
on:
44
push:
5-
branches: [main]
5+
branches:
6+
- main
7+
68
pull_request:
7-
branches: [main]
9+
branches:
10+
- main
811

912
jobs:
1013
test:
1114
name: Run tests
1215
runs-on: ubuntu-latest
1316

1417
steps:
15-
- uses: actions/checkout@v4
18+
- name: Checkout
19+
uses: actions/checkout@v4
1620

17-
- uses: actions/setup-go@v5
21+
- name: Setup Go
22+
uses: actions/setup-go@v5
1823
with:
1924
go-version-file: go.mod
2025
cache: true
@@ -25,21 +30,98 @@ jobs:
2530
- name: Run tests
2631
run: go test ./test/... -v -count=1
2732

28-
deploy:
29-
name: Deploy via SSH
33+
34+
deploy-backend:
35+
name: Deploy backend
3036
needs: test
37+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
3138
runs-on: ubuntu-latest
39+
40+
steps:
41+
- name: Checkout
42+
uses: actions/checkout@v4
43+
with:
44+
fetch-depth: 2
45+
46+
- name: Check backend changes
47+
id: changes
48+
run: |
49+
if git diff --name-only HEAD^ HEAD | grep -qv '^app/'; then
50+
echo "changed=true" >> "$GITHUB_OUTPUT"
51+
else
52+
echo "changed=false" >> "$GITHUB_OUTPUT"
53+
fi
54+
55+
- name: Deploy backend
56+
if: steps.changes.outputs.changed == 'true'
57+
uses: appleboy/ssh-action@v1.0.3
58+
with:
59+
host: ${{ secrets.SSH_HOST }}
60+
username: ${{ secrets.SSH_USERNAME }}
61+
key: ${{ secrets.SSH_PRIVATE_KEY }}
62+
port: 22
63+
script: |
64+
set -e
65+
66+
cd /home/serv-admin/Documents/projects/cms-web
67+
68+
git pull --ff-only origin main
69+
70+
go build -o cms-web.new .
71+
mv cms-web.new cms-web
72+
73+
sudo systemctl restart cms
74+
sudo systemctl is-active --quiet cms
75+
76+
echo "Backend deployed successfully."
77+
78+
79+
deploy-frontend:
80+
name: Deploy frontend
81+
needs: test
3282
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
83+
runs-on: ubuntu-latest
3384

3485
steps:
35-
- name: Deploy to Server via SSH
86+
- name: Checkout
87+
uses: actions/checkout@v4
88+
with:
89+
fetch-depth: 2
90+
91+
- name: Check frontend changes
92+
id: changes
93+
run: |
94+
if git diff --name-only HEAD^ HEAD | grep -q '^app/'; then
95+
echo "changed=true" >> "$GITHUB_OUTPUT"
96+
else
97+
echo "changed=false" >> "$GITHUB_OUTPUT"
98+
fi
99+
100+
- name: Deploy frontend
101+
if: steps.changes.outputs.changed == 'true'
36102
uses: appleboy/ssh-action@v1.0.3
37103
with:
38104
host: ${{ secrets.SSH_HOST }}
39105
username: ${{ secrets.SSH_USERNAME }}
40-
password: ${{ secrets.SSH_PASSWORD }}
106+
key: ${{ secrets.SSH_PRIVATE_KEY }}
41107
port: 22
42108
script: |
43-
cd ~/cms-webb
44-
git pull origin main
45-
sudo docker compose up -d --build
109+
set -e
110+
111+
cd /home/serv-admin/Documents/projects/cms-web
112+
113+
git pull --ff-only origin main
114+
115+
cd app
116+
117+
npm ci
118+
npm run build
119+
120+
sudo rm -rf /var/www/cms/*
121+
sudo cp -a dist/. /var/www/cms/
122+
123+
sudo nginx -t
124+
sudo systemctl reload nginx
125+
126+
echo "Frontend deployed successfully."
127+

0 commit comments

Comments
 (0)