Last active
March 15, 2018 05:55
-
-
Save mrTimofey/363181f44997685e2ad25e6023523680 to your computer and use it in GitHub Desktop.
Simple Bitbucket webhook autodeploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# prevent simultaneous builds | |
while [ -f __build-lock ]; do | |
echo "Other build in progress, sleeping for 10 seconds" | |
sleep 10s | |
done | |
# just empty lock file to prevent multiple builds at the same time | |
touch __build-lock | |
# lay down Laravel app | |
./artisan down --message="Updating, please wait..." | |
git pull | |
composer install --no-dev | |
# migrate for Laravel | |
./artisan migrate --force | |
# build frontend | |
npm i --production | |
npm run build | |
# raise up the app | |
rm __build-lock | |
# for Laravel | |
./artisan up |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen 80; | |
server_name hdprog.ru; | |
root /var/www/project/public; | |
include fastcgi-php.conf; | |
# deploy web hook | |
location = /bitbucket-deploy-web-hook.php { | |
# Bitbucket ports | |
allow 104.192.136.0/21; | |
allow 34.198.203.127; | |
allow 34.198.178.64; | |
allow 34.198.32.85; | |
deny all; | |
# for Laraver projects | |
access_log /var/www/project/storage/logs/bitbucket-deploy.log; | |
error_log /var/www/project/storage/logs/bitbucket-deploy.log; | |
try_files $uri /index.php =404; | |
fastcgi_split_path_info ^(.+\.php)(/.+)\$; | |
# php socket, change if needed | |
fastcgi_pass unix:/run/php/php7.2-fpm.sock; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 20 minutes | |
set_time_limit(1200); | |
header('Content-Type: text/plain'); | |
$data = json_decode(file_get_contents('php://input'), true); | |
if (!$data) die('No request body'); | |
// get all pushed repository changes | |
$changes = $data['push']['changes'] ?? null; | |
if (empty($changes)) die('No changes'); | |
// get current branch | |
$branch = trim(shell_exec('cd ../ && git branch | grep \* | cut -d \' \' -f2')); | |
$changed = false; | |
echo 'Checking changes on branch ', $branch, PHP_EOL; | |
foreach ((array)$changes as $change) { | |
$change = $change['new'] ?? null; | |
/** @noinspection NotOptimalIfConditionsInspection */ | |
if ($change && isset($change['type'], $change['name']) && $change['type'] === 'branch') { | |
echo 'Branch ' . $change['name'] . ' has been changed' . PHP_EOL; | |
if ($change['name'] === $branch) { | |
$changed = true; | |
break; | |
} | |
} | |
} | |
if (!$changed) die('Nothing changed'); | |
echo 'Building started'; | |
// send response and continue with building | |
fastcgi_finish_request(); | |
// cd to the project root (suppose cwd is a project/public or project/public_html, whatever) | |
shell_exec('cd ../ && ./build.sh'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment