-
Notifications
You must be signed in to change notification settings - Fork 1
173 lines (158 loc) · 6.34 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# reusable workflow for build app
# Inspiration: https://philo.dev/how-to-use-github-actions-build-matrix-to-deploy-artifacts-to-multiple-servers/
name: Deployment
# With bran
on:
push:
branches:
- main
paths-ignore:
- '*.md'
jobs:
build-release:
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: pnpm
uses: pnpm/action-setup@v4
with:
version: 7
- name: Use Node.js 21.x
uses: actions/setup-node@v4
with:
node-version: '21'
cache: pnpm
- name: Install dependencies
run: pnpm install --no-frozen-lockfile
- name: Build
run: pnpm run build
- name: Create deployment artifact
run: tar -czf osp_${{github.sha}}.tar.gz --exclude=".git" --exclude="node_modules" *
- name: Store artifact for distribution
uses: actions/upload-artifact@v4
with:
name: app-build
path: osp_${{github.sha}}.tar.gz
deploy-on-staging:
runs-on: self-hosted
needs: build-release
environment: staging
steps:
- name: Get artifact from previous step
uses: actions/download-artifact@v4
with:
name: app-build
path: osp_${{github.sha}}.tar.gz
- name: Upload to staging server
uses: appleboy/scp-action@master
with:
source: osp_${{github.sha}}.tar.gz
target: /var/www/${{secrets.SERVER}}/html/artifacts
host: ${{secrets.SERVER}}
port: 22
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
- name: Extract archive and create directories
uses: appleboy/ssh-action@master
with:
host: ${{secrets.SERVER}}
port: 22
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
script: |
mkdir -p /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
tar -xzf /var/www/${{secrets.SERVER}}/html/artifacts/osp_${{github.sha}}.tar.gz/osp_${{github.sha}}.tar.gz -C /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
rm -rf /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}/storage
- name: Run deploy hooks
uses: appleboy/ssh-action@master
env:
RELEASE_PATH: /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
ACTIVE_RELEASE_PATH: /var/www/${{secrets.SERVER}}/html/current
STORAGE_PATH: /var/www/${{secrets.SERVER}}/html/storage
BASE_PATH: /var/www/${{secrets.SERVER}}/html
with:
host: ${{secrets.SERVER}}
port: 22
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
envs: RELEASE_PATH,ACTIVE_RELEASE_PATH,STORAGE_PATH,BASE_PATH
script_stop: true
script: |
cd /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
ln -s -f $BASE_PATH/.env $RELEASE_PATH
ln -s -f $STORAGE_PATH $RELEASE_PATH
composer install --optimize-autoloader --no-interaction
php artisan optimize:clear
php artisan config:clear
php artisan view:clear
php artisan config:cache
php artisan migrate --force
php artisan db:seed --class=RoleAndPermissionSeeder --force
sudo systemctl reload php8.3-fpm.service
ln -s -n -f $RELEASE_PATH $ACTIVE_RELEASE_PATH
sudo chown -h osp.www-data $ACTIVE_RELEASE_PATH
sudo chown -R osp.www-data $ACTIVE_RELEASE_PATH/
sudo chown -R osp.www-data $STORAGE_PATH
sudo chmod -R 771 $STORAGE_PATH
deploy-on-production:
runs-on: self-hosted
needs: deploy-on-staging
environment: production
steps:
- name: Get artifact from previous step
uses: actions/download-artifact@v4
with:
name: app-build
path: osp_${{github.sha}}.tar.gz
- name: Upload to staging server
uses: appleboy/scp-action@master
with:
source: osp_${{github.sha}}.tar.gz
target: /var/www/${{secrets.SERVER}}/html/artifacts
host: ${{secrets.SERVER}}
port: 22
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
- name: Extract archive and create directories
uses: appleboy/ssh-action@master
with:
host: ${{secrets.SERVER}}
port: 22
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
script: |
mkdir -p /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
tar -xzf /var/www/${{secrets.SERVER}}/html/artifacts/osp_${{github.sha}}.tar.gz/osp_${{github.sha}}.tar.gz -C /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
rm -rf /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}/storage
- name: Run deploy hooks
uses: appleboy/ssh-action@master
env:
RELEASE_PATH: /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
ACTIVE_RELEASE_PATH: /var/www/${{secrets.SERVER}}/html/current
STORAGE_PATH: /var/www/${{secrets.SERVER}}/html/storage
BASE_PATH: /var/www/${{secrets.SERVER}}/html
with:
host: ${{secrets.SERVER}}
port: 22
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
envs: RELEASE_PATH,ACTIVE_RELEASE_PATH,STORAGE_PATH,BASE_PATH
script_stop: true
script: |
cd /var/www/${{secrets.SERVER}}/html/releases/${{github.sha}}
ln -s -f $BASE_PATH/.env $RELEASE_PATH
ln -s -f $STORAGE_PATH $RELEASE_PATH
composer install --optimize-autoloader --no-interaction
php artisan optimize:clear
php artisan config:clear
php artisan view:clear
php artisan config:cache
php artisan migrate --force
php artisan db:seed --class=RoleAndPermissionSeeder --force
sudo systemctl reload php8.3-fpm.service
ln -s -n -f $RELEASE_PATH $ACTIVE_RELEASE_PATH
sudo chown -h osp.www-data $ACTIVE_RELEASE_PATH
sudo chown -R osp.www-data $ACTIVE_RELEASE_PATH/
sudo chown -R osp.www-data $STORAGE_PATH
sudo chmod -R 771 $STORAGE_PATH