From 67b9aae961b74b3af6038439a54ca8b4d2b39255 Mon Sep 17 00:00:00 2001 From: F4ilji Date: Fri, 3 Jul 2026 09:57:34 +0500 Subject: [PATCH] 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 --- .gitignore | 1 + Jenkinsfile | 26 ++++++++++++++++++- composer.json | 1 - composer.lock | 70 +-------------------------------------------------- 4 files changed, 27 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index 0250440..30c9301 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .env deploy.sh +.deploy_prev_sha /node_modules /vendor /sourcefiles diff --git a/Jenkinsfile b/Jenkinsfile index b286744..ce8a65e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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' } } } diff --git a/composer.json b/composer.json index bf4259c..46a12a0 100755 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index effe85a..d55962c 100755 --- a/composer.lock +++ b/composer.lock @@ -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",