Add build, scan hooks and registry publish stages for #24
Scrap-Web/pipeline/head There was a failure building this commit
Scrap-Web/pipeline/head There was a failure building this commit
This commit is contained in:
Vendored
+121
-16
@@ -1,40 +1,145 @@
|
|||||||
// CI pipeline for Scrap-Web (SSW 590, Continuous Integration with Jenkins).
|
// Scrap-Web pipeline: Chapter 4 CI checks + Chapter 6 secure delivery.
|
||||||
// Runs for every pull request through the Multibranch Pipeline job and reports
|
//
|
||||||
// its result to Gitea as a commit status. Uses only the Python 3 standard
|
// Trust boundary:
|
||||||
// library, so no packages are downloaded during the build.
|
// * Every build (branches and pull requests) runs the Chapter 4 checks on the
|
||||||
|
// controller's built-in node. No credentials, no Docker socket.
|
||||||
|
// * Delivery stages run only for trusted branches, on the 'delivery' agent,
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
def TRUSTED_BRANCH = 'main|feature/issue-24-delivery'
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
// Builds run on the Jenkins controller executor (see the IT manual for the
|
agent none
|
||||||
// documented risk). python3 must be installed in the Jenkins container.
|
|
||||||
agent any
|
|
||||||
|
|
||||||
options {
|
options {
|
||||||
skipDefaultCheckout()
|
skipDefaultCheckout()
|
||||||
timeout(time: 10, unit: 'MINUTES')
|
disableConcurrentBuilds() // no competing releases
|
||||||
|
timeout(time: 30, unit: 'MINUTES')
|
||||||
|
buildDiscarder(logRotator(numToKeepStr: '50'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
environment {
|
||||||
|
REGISTRY = 'git.ssw590-08.me'
|
||||||
|
IMAGE = 'git.ssw590-08.me/ssw-590-08/pricing'
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
// ---------------- Chapter 4 CI (all builds, controller) ----------------
|
||||||
|
stage('CI checks') {
|
||||||
|
agent { label 'built-in' }
|
||||||
|
steps {
|
||||||
|
checkout scm
|
||||||
|
sh 'git rev-parse HEAD'
|
||||||
|
sh 'python3 --version'
|
||||||
|
sh 'python3 -m py_compile pricing.py tests/test_pricing.py'
|
||||||
|
sh 'python3 -m unittest discover -s tests -v'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------- Chapter 6 delivery (trusted branches, agent) ---------
|
||||||
|
stage('Delivery') {
|
||||||
|
when { beforeAgent true; branch pattern: TRUSTED_BRANCH, comparator: 'REGEXP' }
|
||||||
|
agent { label 'delivery' }
|
||||||
stages {
|
stages {
|
||||||
stage('Checkout') {
|
stage('Checkout') {
|
||||||
steps {
|
steps {
|
||||||
checkout scm
|
checkout scm
|
||||||
// Record exactly which commit is being tested, for traceability.
|
script {
|
||||||
sh 'git rev-parse HEAD'
|
env.APP_COMMIT = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
|
||||||
sh 'python3 --version'
|
env.IMAGE_TAG = "${env.IMAGE}:${env.APP_COMMIT}"
|
||||||
|
}
|
||||||
|
echo "Source commit: ${env.APP_COMMIT}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('Syntax check') {
|
|
||||||
|
stage('Unit tests') {
|
||||||
steps {
|
steps {
|
||||||
sh 'python3 -m py_compile pricing.py tests/test_pricing.py'
|
dir('pricing') {
|
||||||
|
sh '''
|
||||||
|
python3 -m venv .venv
|
||||||
|
.venv/bin/pip install -q --require-hashes -r requirements.txt
|
||||||
|
.venv/bin/python -m py_compile app.py test_app.py
|
||||||
|
.venv/bin/python -m unittest -v
|
||||||
|
'''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('Test') {
|
}
|
||||||
|
|
||||||
|
// 6.4.5 (kholmes): dependency + secret checks on the source.
|
||||||
|
stage('Security scan: source') {
|
||||||
steps {
|
steps {
|
||||||
sh 'python3 -m unittest discover -s tests -v'
|
sh '''
|
||||||
|
if [ -x security/scan.sh ]; then
|
||||||
|
security/scan.sh source
|
||||||
|
else
|
||||||
|
echo "security/scan.sh not added yet (6.4.5)"
|
||||||
|
fi
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Build image') {
|
||||||
|
steps {
|
||||||
|
dir('pricing') {
|
||||||
|
sh 'docker build --build-arg APP_COMMIT="$APP_COMMIT" -t "$IMAGE_TAG" .'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6.4.5 (kholmes): image vulnerability scan before publishing.
|
||||||
|
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
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Publish') {
|
||||||
|
steps {
|
||||||
|
withCredentials([usernamePassword(credentialsId: 'gitea-registry-push',
|
||||||
|
usernameVariable: 'REG_USER',
|
||||||
|
passwordVariable: 'REG_TOKEN')]) {
|
||||||
|
// Push, then delete the local copy and pull it back from the
|
||||||
|
// registry, so the recorded digest is the registry's, not a local ID.
|
||||||
|
sh '''
|
||||||
|
printf '%s' "$REG_TOKEN" | docker login "$REGISTRY" -u "$REG_USER" --password-stdin
|
||||||
|
docker push "$IMAGE_TAG"
|
||||||
|
docker image rm "$IMAGE_TAG"
|
||||||
|
docker pull "$IMAGE_TAG"
|
||||||
|
docker logout "$REGISTRY"
|
||||||
|
docker image inspect "$IMAGE_TAG" --format '{{index .RepoDigests 0}}' > image-digest.txt
|
||||||
|
echo "Published: $(cat image-digest.txt)"
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
script { env.IMAGE_DIGEST = readFile('image-digest.txt').trim() }
|
||||||
|
writeFile file: 'build-record.txt', text: """source_commit: ${env.APP_COMMIT}
|
||||||
|
image_tag: ${env.IMAGE_TAG}
|
||||||
|
image_digest: ${env.IMAGE_DIGEST}
|
||||||
|
jenkins_build: ${env.BUILD_URL}
|
||||||
|
"""
|
||||||
|
archiveArtifacts artifacts: 'image-digest.txt, build-record.txt', fingerprint: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6.4.6 (samahk13): Ansible staging deploy + smoke tests - added next.
|
||||||
|
|
||||||
|
// 6.4.7 (kholmes): approval + production deploy.
|
||||||
|
stage('Approve and deploy production') {
|
||||||
|
steps { echo 'Not implemented yet (6.4.7).' }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
success { echo 'All checks 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