feat(ci): add migration rollback and remove duplicate chown in Jenkinsfile

- Save pre-deploy SHA before git pull for automatic rollback
- On migrate failure: git reset + migrate:rollback + reinstall deps + restart
- Remove duplicate chown from Restart Services stage (already in Fix Permissions)
- Clean up .deploy_prev_sha after deploy completes
This commit is contained in:
F4ilji
2026-07-03 09:57:34 +05:00
parent 282d5a7c38
commit 67b9aae961
4 changed files with 27 additions and 71 deletions
+1
View File
@@ -1,5 +1,6 @@
.env
deploy.sh
.deploy_prev_sha
/node_modules
/vendor
/sourcefiles
Vendored
+25 -1
View File
@@ -39,6 +39,7 @@ pipeline {
steps {
dir("${APP_DIR}") {
sh 'git config --global --add safe.directory ${APP_DIR}'
sh 'DEPLOY_PREV_SHA=$(git rev-parse HEAD) && echo $DEPLOY_PREV_SHA > .deploy_prev_sha'
sh 'git reset --hard'
sh 'git pull origin master'
}
@@ -59,6 +60,26 @@ pipeline {
sh 'docker exec ntspi-php php artisan migrate --force'
}
}
post {
failure {
dir("${APP_DIR}") {
sh '''
PREV_SHA=$(cat .deploy_prev_sha 2>/dev/null || echo "")
if [ -n "$PREV_SHA" ]; then
echo "Rolling back to $PREV_SHA"
git reset --hard $PREV_SHA
docker exec ntspi-php php artisan migrate:rollback --force || true
docker exec ntspi-php composer install --no-dev --no-interaction --prefer-dist --no-cache
docker exec ntspi-php php artisan cache:clear
docker exec ntspi-php php artisan config:clear
docker exec ntspi-php php artisan route:clear
docker exec ntspi-php php artisan view:clear
docker restart ntspi-nginx ntspi-php ntspi-php-queue
fi
'''
}
}
}
}
stage('Build Frontend') {
@@ -103,7 +124,6 @@ pipeline {
stage('Restart Services') {
steps {
dir("${APP_DIR}") {
sh 'docker exec -u root ntspi-php chown -R www-data:www-data storage bootstrap/cache'
sh 'docker restart ntspi-nginx ntspi-php ntspi-php-queue'
}
}
@@ -138,11 +158,15 @@ pipeline {
post {
success {
echo "Deploy completed successfully"
dir("${APP_DIR}") {
sh 'rm -f .deploy_prev_sha'
}
}
failure {
echo "Deploy failed. Check build logs in Jenkins."
dir("${APP_DIR}") {
sh 'docker exec ntspi-php php artisan up || true'
sh 'rm -f .deploy_prev_sha'
}
}
}
-1
View File
@@ -43,7 +43,6 @@
"require-dev": {
"fakerphp/faker": "^1.9.1",
"laravel/breeze": "^1.25",
"laravel/pint": "^1.0",
"laravel/sail": "^1.18",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^7.0",
Generated
+1 -69
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6cf6c43ee4ed42c7027f6bc6a208b519",
"content-hash": "6df5b3167c82bcd51f51f438281adf2b",
"packages": [
{
"name": "alxdorosenco/porto-for-laravel",
@@ -11740,74 +11740,6 @@
},
"time": "2024-03-04T14:35:21+00:00"
},
{
"name": "laravel/pint",
"version": "v1.29.0",
"source": {
"type": "git",
"url": "https://github.com/laravel/pint.git",
"reference": "bdec963f53172c5e36330f3a400604c69bf02d39"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/pint/zipball/bdec963f53172c5e36330f3a400604c69bf02d39",
"reference": "bdec963f53172c5e36330f3a400604c69bf02d39",
"shasum": ""
},
"require": {
"ext-json": "*",
"ext-mbstring": "*",
"ext-tokenizer": "*",
"ext-xml": "*",
"php": "^8.2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.94.2",
"illuminate/view": "^12.54.1",
"larastan/larastan": "^3.9.3",
"laravel-zero/framework": "^12.0.5",
"mockery/mockery": "^1.6.12",
"nunomaduro/termwind": "^2.4.0",
"pestphp/pest": "^3.8.6",
"shipfastlabs/agent-detector": "^1.1.0"
},
"bin": [
"builds/pint"
],
"type": "project",
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Seeders\\": "database/seeders/",
"Database\\Factories\\": "database/factories/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nuno Maduro",
"email": "enunomaduro@gmail.com"
}
],
"description": "An opinionated code formatter for PHP.",
"homepage": "https://laravel.com",
"keywords": [
"dev",
"format",
"formatter",
"lint",
"linter",
"php"
],
"support": {
"issues": "https://github.com/laravel/pint/issues",
"source": "https://github.com/laravel/pint"
},
"time": "2026-03-12T15:51:39+00:00"
},
{
"name": "laravel/sail",
"version": "v1.54.0",