forked from spatie/freek.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Envoy.blade.php
187 lines (160 loc) · 5.13 KB
/
Envoy.blade.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@setup
$branch = 'main';
$server = "freek.dev";
$server = "138.68.76.137";
$userAndServer = 'forge@'. $server;
$repository = "spatie/freek.dev";
$baseDir = "/home/forge/freek.dev";
$releasesDir = "{$baseDir}/releases";
$persistentDir = "{$baseDir}/persistent";
$currentDir = "{$baseDir}/current";
$newReleaseName = date('Ymd-His');
$newReleaseDir = "{$releasesDir}/{$newReleaseName}";
$user = get_current_user();
function logMessage($message) {
return "echo '\033[32m" .$message. "\033[0m';\n";
}
@endsetup
@servers(['local' => '127.0.0.1', 'remote' => $userAndServer])
@macro('deploy')
startDeployment
cloneRepository
runComposer
runYarn
generateAssets
updateSymlinks
optimizeInstallation
backupDatabase
migrateDatabase
blessNewRelease
cleanOldReleases
finishDeploy
@endmacro
@macro('deploy-code')
deployOnlyCode
@endmacro
@task('startDeployment', ['on' => 'local'])
{{ logMessage("🏃 Starting deployment…") }}
git checkout {{ $branch }}
git pull origin {{ $branch }}
@endtask
@task('cloneRepository', ['on' => 'remote'])
{{ logMessage("🌀 Cloning repository…") }}
[ -d {{ $releasesDir }} ] || mkdir {{ $releasesDir }};
[ -d {{ $persistentDir }} ] || mkdir {{ $persistentDir }};
[ -d {{ $persistentDir }}/uploads ] || mkdir {{ $persistentDir }}/uploads;
[ -d {{ $persistentDir }}/storage ] || mkdir {{ $persistentDir }}/storage;
cd {{ $releasesDir }};
# Create the release dir
mkdir {{ $newReleaseDir }};
# Clone the repo
git clone --depth 1 [email protected]:{{ $repository }} --branch {{ $branch }} {{ $newReleaseName }}
# Configure sparse checkout
cd {{ $newReleaseDir }}
git config core.sparsecheckout true
echo "*" > .git/info/sparse-checkout
echo "!storage" >> .git/info/sparse-checkout
echo "!public/build" >> .git/info/sparse-checkout
git read-tree -mu HEAD
# Mark release
cd {{ $newReleaseDir }}
echo "{{ $newReleaseName }}" > public/release-name.txt
@endtask
@task('runComposer', ['on' => 'remote'])
# Import the environment config
cd {{ $newReleaseDir }};
ln -nfs {{ $baseDir }}/.env .env;
cd {{ $newReleaseDir }};
{{ logMessage("🚚 Running Composer…") }}
ln -nfs {{ $baseDir }}/auth.json auth.json;
composer install --prefer-dist --no-scripts --no-dev -q -o;
php artisan nova:publish
@endtask
@task('runYarn', ['on' => 'remote'])
{{ logMessage("📦 Running Yarn…") }}
cd {{ $newReleaseDir }};
yarn config set ignore-engines true
yarn
@endtask
@task('generateAssets', ['on' => 'remote'])
{{ logMessage("🌅 Generating assets…") }}
cd {{ $newReleaseDir }};
yarn run production
rm -rf node_modules
@endtask
@task('updateSymlinks', ['on' => 'remote'])
{{ logMessage("🔗 Updating symlinks to persistent data…") }}
# Remove the storage directory and replace with persistent data
rm -rf {{ $newReleaseDir }}/storage;
cd {{ $newReleaseDir }};
ln -nfs {{ $baseDir }}/persistent/storage storage;
# Remove the public/uploads directory and replace with persistent data
rm -rf {{ $newReleaseDir }}/public/uploads;
cd {{ $newReleaseDir }};
ln -nfs {{ $baseDir }}/persistent/uploads public/uploads;
# Symlink the og images to the public directory
rm -rf {{ $newReleaseDir }}/public/og-images;
cd {{ $newReleaseDir }};
ln -nfs {{ $baseDir }}/persistent/storage/og-images {{ $newReleaseDir }}/public/og-images;
# Symlink the persistent fonts to the public directory
cd {{ $baseDir }}/persistent/fonts
git pull origin {{ $branch }}
ln -nfs {{ $baseDir }}/persistent/fonts {{ $newReleaseDir }}/public/fonts;
@endtask
@task('optimizeInstallation', ['on' => 'remote'])
{{ logMessage("✨ Optimizing installation…") }}
cd {{ $newReleaseDir }};
php artisan clear-compiled;
@endtask
@task('backupDatabase', ['on' => 'remote'])
{{ logMessage("📀 Backing up database…") }}
cd {{ $newReleaseDir }}
php artisan backup:run
@endtask
@task('migrateDatabase', ['on' => 'remote'])
{{ logMessage("🙈 Migrating database…") }}
cd {{ $newReleaseDir }};
php artisan migrate --force;
@endtask
@task('blessNewRelease', ['on' => 'remote'])
{{ logMessage("🙏 Blessing new release…") }}
ln -nfs {{ $newReleaseDir }} {{ $currentDir }};
cd {{ $newReleaseDir }}
php artisan horizon:terminate
php artisan config:clear
php artisan view:clear
php artisan cache:clear
php artisan config:cache
php artisan route:clear
php artisan route:cache
sudo service php8.1-fpm restart
sudo supervisorctl restart all
php artisan schedule:sync
php artisan health:check
@endtask
@task('cleanOldReleases', ['on' => 'remote'])
{{ logMessage("🚾 Cleaning up old releases…") }}
# Delete all but the 3 most recent.
cd {{ $releasesDir }}
ls -dt {{ $releasesDir }}/* | tail -n +4 | xargs -d "\n" sudo chown -R forge .;
ls -dt {{ $releasesDir }}/* | tail -n +4 | xargs -d "\n" rm -rf;
@endtask
@task('finishDeploy', ['on' => 'local'])
{{ logMessage("🚀 Application deployed!") }}
@endtask
@task('deployOnlyCode',['on' => 'remote'])
{{ logMessage("💻 Deploying code changes…") }}
cd {{ $currentDir }}
git pull origin {{ $branch }}
php artisan config:clear
php artisan view:clear
php artisan cache:clear
php artisan config:cache
php artisan route:clear
php artisan route:cache
php artisan responsecache:clear
php artisan horizon:terminate
sudo service php8.1-fpm restart
php artisan health:check
php artisan schedule:sync
@endtask