From c939bb24d891d669eafe295eef2b7e920e83c482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Mon, 27 Feb 2023 20:26:44 +0100 Subject: [PATCH] ci: add basic jenkinsfile for building Docker images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the first and most basic version of a Jenkinsfile we can use. Since we can't just hardcode things like docker name, tag, or credentials for pushing to a Docker registry it needs to be a parameter. Signed-off-by: Jakub SokoĊ‚owski --- Jenkinsfile | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 00000000..01819634 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,70 @@ +pipeline { + agent { label 'linux' } + + options { + timestamps() + timeout(time: 20, unit: 'MINUTES') + disableConcurrentBuilds() + buildDiscarder(logRotator( + numToKeepStr: '10', + daysToKeepStr: '30', + )) + } + + parameters { + string( + name: 'COMPONENT', + description: 'Name of component to build.', + defaultValue: params.COMPONENT ?: 'backend' + ) + string( + name: 'DOCKER_TAG', + description: 'Name of Docker tag to push. Chose wisely.', + defaultValue: params.DOCKER_TAG ?: 'latest', + ) + string( + name: 'DOCKER_NAME', + description: 'Name of Docker image to push.', + defaultValue: params.DOCKER_NAME ?: 'ghcr.io/sartography/spiffworkflow-backend', + ) + string( + name: 'DOCKER_CRED_ID', + description: 'ID of Jenkins credential for Docker registry.', + defaultValue: params.DOCKER_CRED_ID ?: 'MISSING' + ) + booleanParam( + name: 'PUBLISH', + description: 'Publish built Docker images.', + defaultValue: params.PUBLISH ?: false + ) + } + + stages { + stage('Build') { + steps { script { + dir("spiffworkflow-${params.COMPONENT}") { + image = docker.build( + "${params.DOCKER_NAME}:${env.GIT_COMMIT.take(8)}", + "--label=commit='${env.GIT_COMMIT.take(8)}' ." + ) + } + } } + } + + stage('Push') { + when { expression { + params.PUBLISH && params.DOCKER_CRED_ID != '' + } } + steps { script { + withDockerRegistry([credentialsId: params.DOCKER_CRED_ID, url: ""]) { + image.push() + image.push(env.DOCKER_TAG) + } + } } + } + } // stages + post { + always { sh 'docker image prune -f' } + cleanup { cleanWs() } + } // post +} // pipeline