docker-dmarc-report/Jenkinsfile

126 lines
3.5 KiB
Plaintext
Raw Normal View History

2021-12-12 12:03:39 +01:00
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import java.text.SimpleDateFormat
DOCKER_IMAGE_NAME = ''
DOCKER_IMAGE = ''
DOCKER_ARGS = '--network=services_default'
DOCKER_REGISTRY = 'registry.n-os.org:5000'
properties([
2022-08-09 21:16:23 +02:00
disableConcurrentBuilds(),
parameters([
2021-12-12 12:03:39 +01:00
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Do you want to run the build with tests?')
])
])
2021-12-12 12:03:39 +01:00
node {
try {
pipeline()
}
catch(e) {
2021-12-10 22:48:00 +01:00
setBuildStatus(e.toString().take(140), 'FAILURE')
throw e
}
finally {
cleanup()
}
}
2021-12-10 22:08:06 +01:00
2021-12-12 12:03:39 +01:00
/*
******************************************************************
standard functions
these functions below implement the standard docker image pipeline
******************************************************************
*/
def pipeline() {
2021-12-12 12:03:39 +01:00
stage('checkout git') {
checkout scm
2021-12-10 22:48:00 +01:00
setBuildStatus('In progress...', 'PENDING')
}
2021-12-12 12:03:39 +01:00
// https://docs.cloudbees.com/docs/admin-resources/latest/plugins/docker-workflow
stage('build image') {
DOCKER_IMAGE_NAME = "${DOCKER_REGISTRY}/${getDockerImage()}:${getDockerTag()}"
DOCKER_IMAGE = docker.build(DOCKER_IMAGE_NAME, "--no-cache ${DOCKER_ARGS} .")
}
2021-12-12 12:03:39 +01:00
stage('run tests') {
if (fileExists('./test/run.sh') && !params.SKIP_TESTS) {
DOCKER_IMAGE.inside("${DOCKER_ARGS} --entrypoint=") {
sh 'bash /usr/build/test/run.sh'
}
}
else {
Utils.markStageSkippedForConditional('run tests')
}
}
stage('push image') {
2021-12-12 12:03:39 +01:00
if (BRANCH_NAME == 'master') {
DOCKER_IMAGE.push()
}
else {
Utils.markStageSkippedForConditional('push image')
}
}
2021-12-12 12:03:39 +01:00
stage('delete image') {
if (BRANCH_NAME == 'master') {
Utils.markStageSkippedForConditional('delete image')
}
else {
deleteDockerImage(DOCKER_IMAGE_NAME)
}
setBuildStatus('Success', 'SUCCESS')
}
}
void deleteDockerImage(image) {
sh(script: "docker rmi -f ${image}")
}
2021-12-12 12:03:39 +01:00
void cleanup() {
stage('schedule cleanup') {
2021-12-12 12:03:39 +01:00
build job: '/maintenance/starter', wait: false
}
}
String getDockerImage() {
return sh(script: "echo '${JOB_NAME}' | awk -F/ '{print \$(NF-1)}' | sed 's%docker-%%'", returnStdout: true).trim()
}
String getDockerTag() {
def shortHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
def date = new Date()
def sdf = new SimpleDateFormat("yyyyMMddHHmmss")
// semver in TAG_ID file or reference to ARG in Dockerfile
if (!fileExists('./TAG_ID')) {
return "${sdf.format(date)}.${shortHash}.b${BUILD_ID}"
}
def tagId = sh(script: 'cat ./TAG_ID', returnStdout: true).trim()
if (tagId ==~ /^[A-Z_]+$/) {
return sh(script: "awk -F= '/ARG ${tagId}=/{print \$2}' Dockerfile", returnStdout: true).trim()
}
else {
return tagId
}
}
void setBuildStatus(message, state) {
def repoUrl = sh(script: 'git config --get remote.origin.url', returnStdout: true).trim()
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoUrl],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}