Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 24df5d9c5a | |||
| 291c38bfd0 | |||
| 3cc6f3b0d4 | |||
| db8f5fe27b | |||
| c9006bb314 | |||
| 8e2fe554d3 | |||
| 9fdfd00fa6 | |||
| 927df037b0 | |||
| dae5b47373 | |||
| 944ca0f203 | |||
| 1841a7d4ab | |||
| 12226285c6 | |||
| 7b73df19bd | |||
| d6787727f8 | |||
| 69f1fe05cf | |||
| c5172fca86 | |||
| 90c5d9a3b1 | |||
| f14abad27b | |||
| c46f127e8c | |||
| 05d3c58c3a | |||
| 3fcd04010a | |||
| ad37d148ed | |||
| c864ddd0d2 | |||
| 8979d92c00 | |||
| 17750b844b | |||
| fb2bac752a | |||
| 99acac3136 | |||
| 3b79b7e07a | |||
| 5c0ed9fbcd |
@@ -1 +1,2 @@
|
||||
__pycache__/
|
||||
security-reports/
|
||||
@@ -7,16 +7,50 @@
|
||||
// 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.
|
||||
//
|
||||
// Release version: major.minor.changelist = 1.0.<Jenkins build number>,
|
||||
// shown as the build name and used for the image tag, ZIP, and manual PDF.
|
||||
//
|
||||
// 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'
|
||||
def TRUSTED_BRANCH = 'main|feature/issue-24-delivery'
|
||||
// Jenkins user IDs allowed to approve production. Anonymous users cannot.
|
||||
def APPROVERS = 'samahk,kaylah7'
|
||||
// Semantic version: bump MAJOR_MINOR by hand for a feature or breaking release;
|
||||
// the changelist part is the Jenkins build number, so every build is unique.
|
||||
def MAJOR_MINOR = '1.0'
|
||||
|
||||
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 +74,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') {
|
||||
@@ -49,8 +87,12 @@ pipeline {
|
||||
script {
|
||||
env.APP_COMMIT = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||
env.IMAGE_TAG = "${env.IMAGE}:${env.APP_COMMIT}"
|
||||
env.SEMVER = "${MAJOR_MINOR}.${env.BUILD_NUMBER}"
|
||||
env.SEMVER_TAG = "${env.IMAGE}:${env.SEMVER}"
|
||||
currentBuild.displayName = "#${env.BUILD_NUMBER} v${env.SEMVER}"
|
||||
}
|
||||
echo "Source commit: ${env.APP_COMMIT}"
|
||||
echo "Release version: ${env.SEMVER}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,38 +109,45 @@ pipeline {
|
||||
}
|
||||
}
|
||||
|
||||
// 6.4.5 (kholmes): dependency + secret checks on the source.
|
||||
stage('Security scan: source') {
|
||||
// LaTeX: compile the IT manual on the Droplet, in the same TeX Live
|
||||
// image the Gitea Actions workflow uses (manual/ci/Dockerfile).
|
||||
// The agent talks to the host's Docker daemon, so files are copied
|
||||
// into and out of the container instead of bind-mounted.
|
||||
stage('Compile manual (LaTeX)') {
|
||||
steps {
|
||||
sh '''
|
||||
if [ -x security/scan.sh ]; then
|
||||
security/scan.sh source
|
||||
else
|
||||
echo "security/scan.sh not added yet (6.4.5)"
|
||||
fi
|
||||
docker image inspect ssw590/manual-latex:1 >/dev/null 2>&1 || docker build -q -t ssw590/manual-latex:1 manual/ci
|
||||
cid=$(docker create -w /work ssw590/manual-latex:1 latexmk devopsAssignment.tex)
|
||||
docker cp manual/. "$cid":/work
|
||||
if docker start -a "$cid"; then rc=0; else rc=$?; fi
|
||||
docker cp "$cid":/work/devopsAssignment.pdf "manual-$SEMVER.pdf" || true
|
||||
docker rm "$cid" > /dev/null
|
||||
[ "$rc" -eq 0 ] && [ -s "manual-$SEMVER.pdf" ] || { echo "Manual build failed"; exit 1; }
|
||||
echo "Manual compiled: manual-$SEMVER.pdf"
|
||||
'''
|
||||
archiveArtifacts artifacts: "manual-${env.SEMVER}.pdf", fingerprint: true
|
||||
}
|
||||
}
|
||||
|
||||
// 6.4.5: dependency and secret checks on the source. Fails closed:
|
||||
// a missing script, a scanner error, or a finding stops the run.
|
||||
stage('Security scan: source') {
|
||||
steps { sh 'bash security/scan.sh source' }
|
||||
post { always { archiveArtifacts artifacts: 'security-reports/**', allowEmptyArchive: true } }
|
||||
}
|
||||
|
||||
stage('Build image') {
|
||||
steps {
|
||||
dir('pricing') {
|
||||
sh 'docker build --build-arg APP_COMMIT="$APP_COMMIT" -t "$IMAGE_TAG" .'
|
||||
sh 'docker build --build-arg APP_COMMIT="$APP_COMMIT" -t "$IMAGE_TAG" -t "$SEMVER_TAG" .'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6.4.5 (kholmes): image vulnerability scan before publishing.
|
||||
// 6.4.5: image vulnerability scan before publishing. Fails closed.
|
||||
stage('Security scan: image') {
|
||||
steps {
|
||||
sh '''
|
||||
if [ -x security/scan.sh ]; then
|
||||
security/scan.sh image "$IMAGE_TAG"
|
||||
else
|
||||
echo "security/scan.sh not added yet (6.4.5)"
|
||||
fi
|
||||
'''
|
||||
}
|
||||
steps { sh 'bash security/scan.sh image "$IMAGE_TAG"' }
|
||||
post { always { archiveArtifacts artifacts: 'security-reports/**', allowEmptyArchive: true } }
|
||||
}
|
||||
|
||||
stage('Publish') {
|
||||
@@ -111,7 +160,8 @@ pipeline {
|
||||
sh '''
|
||||
printf '%s' "$REG_TOKEN" | docker login "$REGISTRY" -u "$REG_USER" --password-stdin
|
||||
docker push "$IMAGE_TAG"
|
||||
docker image rm "$IMAGE_TAG"
|
||||
docker push "$SEMVER_TAG"
|
||||
docker image rm "$IMAGE_TAG" "$SEMVER_TAG"
|
||||
docker pull "$IMAGE_TAG"
|
||||
docker logout "$REGISTRY"
|
||||
docker image inspect "$IMAGE_TAG" --format '{{index .RepoDigests 0}}' > image-digest.txt
|
||||
@@ -119,7 +169,9 @@ pipeline {
|
||||
'''
|
||||
}
|
||||
script { env.IMAGE_DIGEST = readFile('image-digest.txt').trim() }
|
||||
writeFile file: 'build-record.txt', text: """source_commit: ${env.APP_COMMIT}
|
||||
writeFile file: 'build-record.txt', text: """version: ${env.SEMVER}
|
||||
source_commit: ${env.APP_COMMIT}
|
||||
version_tag: ${env.SEMVER_TAG}
|
||||
image_tag: ${env.IMAGE_TAG}
|
||||
image_digest: ${env.IMAGE_DIGEST}
|
||||
jenkins_build: ${env.BUILD_URL}
|
||||
@@ -128,33 +180,43 @@ jenkins_build: ${env.BUILD_URL}
|
||||
}
|
||||
}
|
||||
|
||||
// Deployment-ready ZIP named with the semantic version.
|
||||
stage('Package') {
|
||||
steps {
|
||||
sh '''
|
||||
rm -rf dist && mkdir -p "dist/pricing-$SEMVER"
|
||||
cp pricing/app.py pricing/requirements.txt pricing/Dockerfile "dist/pricing-$SEMVER/"
|
||||
cp -r ansible "dist/pricing-$SEMVER/ansible"
|
||||
cp build-record.txt "dist/pricing-$SEMVER/"
|
||||
printf '%s\n' "$SEMVER" > "dist/pricing-$SEMVER/VERSION"
|
||||
cd dist && python3 -m zipfile -c "pricing-$SEMVER.zip" "pricing-$SEMVER"
|
||||
python3 -m zipfile -l "pricing-$SEMVER.zip"
|
||||
'''
|
||||
archiveArtifacts artifacts: "dist/pricing-${env.SEMVER}.zip", fingerprint: true
|
||||
}
|
||||
}
|
||||
|
||||
// 6.4.6: deploy the digest just published to staging with Ansible,
|
||||
// then rerun unchanged and require changed=0 (idempotence).
|
||||
stage('Deploy staging') {
|
||||
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()
|
||||
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.APP_VERSION = version
|
||||
writeFile file: "release-${version}-staging.yml", text: """target_env: staging
|
||||
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}
|
||||
release_version: ${env.SEMVER}
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,10 +234,100 @@ 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} (release ${env.SEMVER}) 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}
|
||||
release_version: ${env.SEMVER}
|
||||
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)"'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 146 KiB |
|
After Width: | Height: | Size: 192 KiB |
|
After Width: | Height: | Size: 159 KiB |
|
After Width: | Height: | Size: 194 KiB |
|
After Width: | Height: | Size: 220 KiB |
|
After Width: | Height: | Size: 617 KiB |
|
After Width: | Height: | Size: 169 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 422 KiB |
|
After Width: | Height: | Size: 157 KiB |
|
After Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 174 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 264 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 2.0 MiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 38 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 84 KiB |
|
After Width: | Height: | Size: 131 KiB |
|
After Width: | Height: | Size: 160 KiB |
|
After Width: | Height: | Size: 65 KiB |
|
After Width: | Height: | Size: 432 KiB |
|
After Width: | Height: | Size: 235 KiB |
|
After Width: | Height: | Size: 151 KiB |
|
After Width: | Height: | Size: 188 KiB |
|
After Width: | Height: | Size: 522 KiB |
|
After Width: | Height: | Size: 84 KiB |
|
After Width: | Height: | Size: 134 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 472 KiB |
|
After Width: | Height: | Size: 257 KiB |
|
After Width: | Height: | Size: 66 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 281 KiB |
|
After Width: | Height: | Size: 104 KiB |
|
After Width: | Height: | Size: 172 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 149 KiB |
|
After Width: | Height: | Size: 105 KiB |
|
After Width: | Height: | Size: 142 KiB |
|
After Width: | Height: | Size: 98 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 96 KiB |
|
After Width: | Height: | Size: 138 KiB |
|
After Width: | Height: | Size: 120 KiB |
|
After Width: | Height: | Size: 181 KiB |
|
After Width: | Height: | Size: 216 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 446 KiB |
|
After Width: | Height: | Size: 93 KiB |
|
After Width: | Height: | Size: 155 KiB |
|
After Width: | Height: | Size: 192 KiB |
|
After Width: | Height: | Size: 192 KiB |
|
After Width: | Height: | Size: 189 KiB |
|
After Width: | Height: | Size: 220 KiB |
|
After Width: | Height: | Size: 309 KiB |
|
After Width: | Height: | Size: 435 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 171 KiB |
|
After Width: | Height: | Size: 92 KiB |
|
After Width: | Height: | Size: 58 KiB |
|
After Width: | Height: | Size: 113 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 92 KiB |
|
After Width: | Height: | Size: 234 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 232 KiB |
|
After Width: | Height: | Size: 362 KiB |
|
After Width: | Height: | Size: 95 KiB |
|
After Width: | Height: | Size: 606 KiB |
|
After Width: | Height: | Size: 572 KiB |
|
After Width: | Height: | Size: 158 KiB |
|
After Width: | Height: | Size: 157 KiB |
|
After Width: | Height: | Size: 468 KiB |
|
After Width: | Height: | Size: 266 KiB |
|
After Width: | Height: | Size: 89 KiB |
|
After Width: | Height: | Size: 173 KiB |
|
After Width: | Height: | Size: 38 KiB |
|
After Width: | Height: | Size: 135 KiB |
|
After Width: | Height: | Size: 333 KiB |
|
After Width: | Height: | Size: 56 KiB |
|
After Width: | Height: | Size: 206 KiB |