Add semantic versioning, LaTeX manual stage and versioned ZIP
Scrap-Web/pipeline/pr-main This commit looks good
Scrap-Web/pipeline/head There was a failure building this commit

This commit is contained in:
2026-11-29 20:24:54 -05:00
parent dae5b47373
commit 927df037b0
Vendored
+56 -5
View File
@@ -8,6 +8,9 @@
// 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.
@@ -16,9 +19,12 @@
// 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')]) {
@@ -81,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}"
}
}
@@ -99,6 +109,26 @@ pipeline {
}
}
// 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 '''
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') {
@@ -109,7 +139,7 @@ pipeline {
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" .'
}
}
}
@@ -130,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
@@ -138,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}
@@ -147,6 +180,22 @@ 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') {
@@ -161,6 +210,7 @@ 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://staging.ssw590-08.me
proxy_network: gitea-net
"""
@@ -193,7 +243,7 @@ proxy_network: gitea-net
// 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)?
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).
@@ -217,6 +267,7 @@ 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}