Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 69f1fe05cf | |||
| c5172fca86 | |||
| 90c5d9a3b1 |
Vendored
+134
-20
@@ -7,16 +7,44 @@
|
|||||||
// which holds the Docker socket and the registry/deploy credentials.
|
// which holds the Docker socket and the registry/deploy credentials.
|
||||||
// TODO before submission: remove the feature branch from TRUSTED_BRANCH so
|
// TODO before submission: remove the feature branch from TRUSTED_BRANCH so
|
||||||
// only reviewed code on main can deliver.
|
// 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'
|
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 {
|
pipeline {
|
||||||
agent none
|
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 {
|
options {
|
||||||
skipDefaultCheckout()
|
skipDefaultCheckout()
|
||||||
disableConcurrentBuilds() // no competing releases
|
disableConcurrentBuilds() // no competing releases
|
||||||
timeout(time: 30, unit: 'MINUTES')
|
timeout(time: 60, unit: 'MINUTES')
|
||||||
buildDiscarder(logRotator(numToKeepStr: '50'))
|
buildDiscarder(logRotator(numToKeepStr: '50'))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +68,11 @@ pipeline {
|
|||||||
|
|
||||||
// ---------------- Chapter 6 delivery (trusted branches, agent) ---------
|
// ---------------- Chapter 6 delivery (trusted branches, agent) ---------
|
||||||
stage('Delivery') {
|
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' }
|
agent { label 'delivery' }
|
||||||
stages {
|
stages {
|
||||||
stage('Checkout') {
|
stage('Checkout') {
|
||||||
@@ -121,27 +153,20 @@ jenkins_build: ${env.BUILD_URL}
|
|||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Expected behaviour comes from the source, not from config.
|
// 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()
|
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()
|
def unit = sh(script: "sed -n 's/^UNIT_PRICE = //p' pricing/app.py", returnStdout: true).trim()
|
||||||
env.APP_VERSION = version
|
env.EXPECTED_TOTAL = "${3 * (unit as Integer)}"
|
||||||
writeFile file: "release-${version}-staging.yml", text: """target_env: staging
|
writeFile file: "release-${env.APP_VERSION}-staging.yml", text: """target_env: staging
|
||||||
release_image: ${env.IMAGE_DIGEST}
|
release_image: ${env.IMAGE_DIGEST}
|
||||||
expected_version: ${version}
|
expected_version: ${env.APP_VERSION}
|
||||||
expected_commit: ${env.APP_COMMIT}
|
expected_commit: ${env.APP_COMMIT}
|
||||||
expected_total: ${3 * (unit as Integer)}
|
expected_total: ${env.EXPECTED_TOTAL}
|
||||||
public_url: https://staging.ssw590-08.me
|
public_url: https://staging.ssw590-08.me
|
||||||
proxy_network: gitea-net
|
proxy_network: gitea-net
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
archiveArtifacts artifacts: "release-${env.APP_VERSION}-staging.yml", fingerprint: true
|
archiveArtifacts artifacts: "release-${env.APP_VERSION}-staging.yml", fingerprint: true
|
||||||
withCredentials([sshUserPrivateKey(credentialsId: 'deploy-ssh', keyFileVariable: 'DEPLOY_KEY')]) {
|
deployRecord("release-${env.APP_VERSION}-staging.yml")
|
||||||
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
|
|
||||||
'''
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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('Deploy production') {
|
||||||
stage('Approve and deploy production') {
|
steps {
|
||||||
steps { echo 'Not implemented yet (6.4.7).' }
|
// 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.' }
|
success { echo 'All stages passed.' }
|
||||||
failure { echo 'A stage failed: see the stage view for which one.' }
|
failure { echo 'A stage failed: see the stage view for which one.' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user