Skip to content

Commit d2dfab4

Browse files
author
Michal Jacko
committed
Add assets for deployment
1 parent 8f0ba68 commit d2dfab4

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

src/Envoy.blade.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
@servers(['web' => $user.'@'.$host,'localhost' => '127.0.0.1'])
2+
3+
@setup
4+
// Sanity checks
5+
if (empty($host)) {
6+
exit('ERROR: $host var empty or not defined');
7+
}
8+
if (empty($user)) {
9+
exit('ERROR: $user var empty or not defined');
10+
}
11+
if (empty($path)) {
12+
exit('ERROR: $path var empty or not defined');
13+
}
14+
if (empty($build)) {
15+
exit('ERROR: $build var empty or not defined');
16+
}
17+
if (empty($commit)) {
18+
exit('ERROR: $commit var empty or not defined');
19+
}
20+
21+
if (file_exists($path) || is_writable($path)) {
22+
exit("ERROR: cannot access $path");
23+
}
24+
25+
// Ensure given $path is a potential web directory (/home/* or /var/www/*)
26+
if (!(preg_match("/(\/home\/|\/var\/www\/)/i", $path) === 1)) {
27+
exit('ERROR: $path provided doesn\'t look like a web directory path?');
28+
}
29+
30+
$current_release_dir = $path . '/current';
31+
$releases_dir = $path . '/releases';
32+
$new_release_dir = $releases_dir . '/' . $build . '_' . $commit;
33+
34+
$remote = $user . '@' . $host . ':' . $new_release_dir;
35+
36+
// Command or path to invoke PHP
37+
$php = empty($php) ? 'php' : $php;
38+
@endsetup
39+
40+
@story('deploy')
41+
rsync
42+
manifest_file
43+
setup_symlinks
44+
verify_install
45+
activate_release
46+
optimise
47+
migrate
48+
cleanup
49+
@endstory
50+
51+
@task('debug', ['on' => 'localhost'])
52+
ls -la {{ $dir }}
53+
@endtask
54+
55+
@task('rsync', ['on' => 'localhost'])
56+
echo "* Deploying code from {{ $dir }} to {{ $remote }} *"
57+
# https://explainshell.com/explain?cmd=rsync+-zrSlh+--exclude-from%3Ddeployment-exclude-list.txt+.%2F.+%7B%7B+%24remote+%7D%7D
58+
rsync -zrSlh --stats --exclude-from=deployment-exclude-list.txt {{ $dir }}/ {{ $remote }}
59+
@endtask
60+
61+
@task('manifest_file', ['on' => 'web'])
62+
echo "* Writing deploy manifest file *"
63+
echo -e "{\"build\":\""{{ $build }}"\", \"commit\":\""{{ $commit }}"\", \"branch\":\""{{ $branch }}"\"}" > {{ $new_release_dir }}/deploy-manifest.json
64+
@endtask
65+
66+
@task('setup_symlinks', ['on' => 'web'])
67+
echo "* Linking .env file to new release dir ({{ $path }}/.env -> {{ $new_release_dir }}/.env) *"
68+
ln -nfs {{ $path }}/.env {{ $new_release_dir }}/.env
69+
70+
if [ -f {{ $new_release_dir }}/storage ]; then
71+
echo "* Moving existing storage dir *"
72+
mv {{ $new_release_dir }}/storage {{ $new_release_dir }}/storage.orig 2>/dev/null
73+
fi
74+
75+
echo "* Linking storage directory to new release dir ({{ $path }}/storage -> {{ $new_release_dir }}/storage) *"
76+
ln -nfs {{ $path }}/storage {{ $new_release_dir }}/storage
77+
@endtask
78+
79+
@task('verify_install', ['on' => 'web'])
80+
echo "* Verifying install ({{ $new_release_dir }}) *"
81+
cd {{ $new_release_dir }}
82+
{{ $php }} artisan --version
83+
@endtask
84+
85+
@task('activate_release', ['on' => 'web'])
86+
echo "* Activating new release ({{ $new_release_dir }} -> {{ $current_release_dir }}) *"
87+
ln -nfs {{ $new_release_dir }} {{ $current_release_dir }}
88+
@endtask
89+
90+
@task('migrate', ['on' => 'web'])
91+
echo '* Running migrations *'
92+
cd {{ $new_release_dir }}
93+
{{ $php }} artisan migrate --force
94+
@endtask
95+
96+
@task('optimise', ['on' => 'web'])
97+
echo '* Clearing cache and optimising *'
98+
cd {{ $new_release_dir }}
99+
100+
{{ $php }} artisan cache:clear
101+
{{ $php }} artisan config:clear
102+
{{ $php }} artisan route:clear
103+
{{ $php }} artisan view:clear
104+
105+
# https://laravel.com/docs/5.5/deployment#optimization
106+
{{ $php }} artisan config:cache
107+
# Only use when no closure used in routes
108+
#{{ $php }} artisan route:cache
109+
110+
#echo '* Reloading php-fpm *'
111+
#sudo -S service php7.1-fpm reload
112+
113+
#echo '* Gracefully terminating Laravel Horizon *'
114+
#{{ $php }} artisan horizon:terminate
115+
#sudo supervisorctl stop horizon # workaround
116+
#sudo supervisorctl start horizon # workaround
117+
@endtask
118+
119+
@task('cleanup', ['on' => 'web'])
120+
echo "* Executing cleanup command in {{ $releases_dir }} *"
121+
ls -dt {{ $releases_dir }}/*/ | tail -n +4 | xargs rm -rf
122+
@endtask

src/LaravelBitbucketDeployServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class LaravelBitbucketDeployServiceProvider extends ServiceProvider
2020
*/
2121
public function boot()
2222
{
23+
$this->publishes([
24+
__DIR__ . '/deploy-manifest.json' => base_path('deploy-manifest.json'),
25+
//__DIR__ . '/config' => config_path('laravel-bitbucket-deploy'),
26+
]);
2327
}
2428

2529

src/bitbucket-pipelines.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
image: php:7.2-fpm
2+
3+
definitions:
4+
steps:
5+
- step: &php-build
6+
name: Build PHP
7+
caches:
8+
- composer
9+
script:
10+
- apt-get update && apt-get install -qy unzip git curl libmcrypt-dev
11+
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
12+
- composer --version
13+
- composer install --optimize-autoloader --no-interaction --prefer-dist --ignore-platform-reqs
14+
artifacts:
15+
- vendor/**
16+
- step: &php-test
17+
name: Test PHP
18+
script:
19+
- cp .env.example .env
20+
- php artisan key:generate
21+
- ./vendor/bin/phpunit --log-junit ./test-reports/junit.xml
22+
artifacts:
23+
- test-reports/**
24+
- step: &npm-build
25+
name: Build Assets
26+
image: node:8.10.0
27+
caches:
28+
- node
29+
script:
30+
- npm install
31+
- yarn
32+
- npm run production
33+
artifacts:
34+
- public/**
35+
- step: &deploy-production
36+
name: Deploy (Production)
37+
deployment: production
38+
caches:
39+
- composer
40+
script:
41+
- apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
42+
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
43+
- composer --version
44+
- composer global require "laravel/envoy"
45+
- ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR
46+
- step: &deploy-staging
47+
name: Deploy (Staging)
48+
deployment: staging
49+
caches:
50+
- composer
51+
script:
52+
- apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
53+
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
54+
- composer --version
55+
- composer global require "laravel/envoy"
56+
- ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR
57+
58+
pipelines:
59+
default:
60+
- step: *php-build
61+
- step: *php-test
62+
- step: *npm-build
63+
branches:
64+
master:
65+
- step: *php-build
66+
- step: *php-test
67+
- step: *npm-build
68+
- step: *deploy-production
69+
develop:
70+
- step: *php-build
71+
- step: *php-test
72+
- step: *npm-build
73+
- step: *deploy-staging
74+
feature/*:
75+
- step: *php-build
76+
- step: *php-test
77+
- step: *npm-build

src/deploy-manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"build": null,
3+
"commit": null,
4+
"branch": null
5+
}

src/deployment-exclude-list.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/bootstrap/cache/*.php
2+
/.*
3+
/node_modules
4+
/storage
5+
/vendor/composer/composer/tests/*
6+
bitbucket-pipelines.yml
7+
deployment-exclude-list.txt
8+
Envoy.blade.php
9+
package-lock.json
10+
package.json
11+
README.md
12+
webpack.mix.js
13+
yarn.lock

0 commit comments

Comments
 (0)