From 90c5d9a3b125b0cf172b42aed4e3daa5a4b36a26 Mon Sep 17 00:00:00 2001 From: Samah Keshiro Date: Fri, 13 Nov 2026 21:54:31 -0500 Subject: [PATCH] Updating Jenkinsfile --- Jenkinsfile | 154 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 134 insertions(+), 20 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 7456a35..2df89e4 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,16 +7,44 @@ // which holds the Docker socket and the registry/deploy credentials. // TODO before submission: remove the feature branch from TRUSTED_BRANCH so // only reviewed code on main can deliver. +// +// Two kinds of run: +// * Normal run (DEPLOY_RECORD empty): test, scan, build once, publish, deploy +// staging, prove idempotence, human approval, promote the SAME digest. +// * Redeploy run (DEPLOY_RECORD = a committed release record, e.g. +// releases/v1-production.yml): no build; redeploys exactly that record with +// this commit's Ansible files. Used for rollback and failed-deploy recovery. +// Production redeploys need the same approval. def TRUSTED_BRANCH = 'main|feature/issue-24-delivery' +// Jenkins user IDs allowed to approve production. Anonymous users cannot. +def APPROVERS = 'samahk,kaylah7' + +def deployRecord(String record) { + withCredentials([sshUserPrivateKey(credentialsId: 'deploy-ssh', keyFileVariable: 'DEPLOY_KEY')]) { + withEnv(["RECORD=${record}"]) { + sh ''' + export PATH=/opt/ansible-venv/bin:$PATH + ansible-playbook -i ansible/inventory.ini ansible/deploy.yml --syntax-check + ansible-playbook -i ansible/inventory.ini ansible/deploy.yml \ + --private-key "$DEPLOY_KEY" -e @"$RECORD" + ''' + } + } +} pipeline { agent none + parameters { + string(name: 'DEPLOY_RECORD', defaultValue: '', + description: 'Leave EMPTY for a normal release. For rollback/recovery, the path of a committed release record, e.g. releases/v1-production.yml') + } + options { skipDefaultCheckout() disableConcurrentBuilds() // no competing releases - timeout(time: 30, unit: 'MINUTES') + timeout(time: 60, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '50')) } @@ -40,7 +68,11 @@ pipeline { // ---------------- Chapter 6 delivery (trusted branches, agent) --------- stage('Delivery') { - when { beforeAgent true; branch pattern: TRUSTED_BRANCH, comparator: 'REGEXP' } + when { + beforeAgent true + branch pattern: TRUSTED_BRANCH, comparator: 'REGEXP' + expression { (params.DEPLOY_RECORD ?: '').trim() == '' } + } agent { label 'delivery' } stages { stage('Checkout') { @@ -121,27 +153,20 @@ jenkins_build: ${env.BUILD_URL} steps { script { // Expected behaviour comes from the source, not from config. - def version = sh(script: "sed -n 's/^VERSION = \"\\(.*\\)\"/\\1/p' pricing/app.py", returnStdout: true).trim() - def unit = sh(script: "sed -n 's/^UNIT_PRICE = //p' pricing/app.py", returnStdout: true).trim() - env.APP_VERSION = version - writeFile file: "release-${version}-staging.yml", text: """target_env: staging + env.APP_VERSION = sh(script: "sed -n 's/^VERSION = \"\\(.*\\)\"/\\1/p' pricing/app.py", returnStdout: true).trim() + def unit = sh(script: "sed -n 's/^UNIT_PRICE = //p' pricing/app.py", returnStdout: true).trim() + env.EXPECTED_TOTAL = "${3 * (unit as Integer)}" + writeFile file: "release-${env.APP_VERSION}-staging.yml", text: """target_env: staging release_image: ${env.IMAGE_DIGEST} -expected_version: ${version} +expected_version: ${env.APP_VERSION} expected_commit: ${env.APP_COMMIT} -expected_total: ${3 * (unit as Integer)} +expected_total: ${env.EXPECTED_TOTAL} public_url: https://staging.ssw590-08.me proxy_network: gitea-net """ } archiveArtifacts artifacts: "release-${env.APP_VERSION}-staging.yml", fingerprint: true - withCredentials([sshUserPrivateKey(credentialsId: 'deploy-ssh', keyFileVariable: 'DEPLOY_KEY')]) { - sh ''' - export PATH=/opt/ansible-venv/bin:$PATH - ansible-playbook -i ansible/inventory.ini ansible/deploy.yml --syntax-check - ansible-playbook -i ansible/inventory.ini ansible/deploy.yml \ - --private-key "$DEPLOY_KEY" -e @release-$APP_VERSION-staging.yml - ''' - } + deployRecord("release-${env.APP_VERSION}-staging.yml") } } @@ -159,10 +184,99 @@ proxy_network: gitea-net } } + // 6.4.7: restricted human approval, then promote the SAME digest. + stage('Approve production') { + steps { + script { + def scan = fileExists('security-reports/image-summary.txt') ? + readFile('security-reports/image-summary.txt').trim() : 'see security-reports' + // Only APPROVERS can approve; Abort or the timeout stops the run. + timeout(time: 30, unit: 'MINUTES') { + env.APPROVER = input( + message: """Promote ${env.APP_VERSION} to production (https://app.ssw590-08.me)? +Source commit: ${env.APP_COMMIT} +Image digest: ${env.IMAGE_DIGEST} +Security scans: source and image gates PASSED (fixable HIGH/CRITICAL block). +${scan} +Staging: deployed, HTTPS health/version/commit/total ${env.EXPECTED_TOTAL} passed, rerun changed=0.""", + ok: 'Approve and deploy to production', + submitter: APPROVERS, + submitterParameter: 'APPROVER') + } + env.APPROVED_AT = sh(script: 'date -Is', returnStdout: true).trim() + echo "APPROVED by ${env.APPROVER} at ${env.APPROVED_AT}" + } + } + } - // 6.4.7 (kholmes): approval + production deploy. - stage('Approve and deploy production') { - steps { echo 'Not implemented yet (6.4.7).' } + stage('Deploy production') { + steps { + // Same digest and commit as staging; only environment and URL change. No rebuild. + writeFile file: "release-${env.APP_VERSION}-production.yml", text: """target_env: production +release_image: ${env.IMAGE_DIGEST} +expected_version: ${env.APP_VERSION} +expected_commit: ${env.APP_COMMIT} +expected_total: ${env.EXPECTED_TOTAL} +public_url: https://app.ssw590-08.me +proxy_network: gitea-net +approved_by: ${env.APPROVER} +approved_at: ${env.APPROVED_AT} +""" + archiveArtifacts artifacts: "release-${env.APP_VERSION}-production.yml", fingerprint: true + deployRecord("release-${env.APP_VERSION}-production.yml") + } + } + } + } + + // ---------------- Rollback / recovery: redeploy a committed record ----- + stage('Redeploy release record') { + when { + beforeAgent true + branch pattern: TRUSTED_BRANCH, comparator: 'REGEXP' + expression { (params.DEPLOY_RECORD ?: '').trim() != '' } + } + agent { label 'delivery' } + stages { + stage('Load record') { + steps { + checkout scm + script { + env.RECORD = params.DEPLOY_RECORD.trim() + if (!(env.RECORD ==~ /releases\/[A-Za-z0-9._-]+\.yml/) || !fileExists(env.RECORD)) { + error "No committed release record at ${env.RECORD}" + } + env.TARGET_ENV = sh(script: 'sed -n "s/^target_env: //p" "$RECORD"', returnStdout: true).trim() + env.CONFIG_COMMIT = sh(script: 'git rev-parse HEAD', returnStdout: true).trim() + } + echo "Redeploying ${env.RECORD} to ${env.TARGET_ENV} with configuration commit ${env.CONFIG_COMMIT}" + sh 'cat "$RECORD"' + archiveArtifacts artifacts: "${env.RECORD}", fingerprint: true + } + } + + stage('Approve redeploy') { + when { environment name: 'TARGET_ENV', value: 'production' } + steps { + script { + timeout(time: 30, unit: 'MINUTES') { + env.APPROVER = input( + message: "Redeploy ${env.RECORD} to PRODUCTION (configuration commit ${env.CONFIG_COMMIT})?", + ok: 'Approve redeploy', + submitter: APPROVERS, + submitterParameter: 'APPROVER') + } + echo "APPROVED by ${env.APPROVER} at ${sh(script: 'date -Is', returnStdout: true).trim()}" + } + } + } + + stage('Redeploy') { + steps { + sh 'echo "Redeploy start: $(date -Is)"' + deployRecord(env.RECORD) + sh 'echo "Redeploy verified: $(date -Is)"' + } } } } @@ -172,4 +286,4 @@ proxy_network: gitea-net success { echo 'All stages passed.' } failure { echo 'A stage failed: see the stage view for which one.' } } -} \ No newline at end of file +}