From 577b13e9ed048b81710bba95b81d1b41f3f90ab2 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Tue, 6 Feb 2024 20:12:46 +0100 Subject: [PATCH] Jenkins build file --- Jenkinsfile | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..b3c0108 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,79 @@ +pipeline { + agent { + kubernetes { + defaultContainer 'kaniko' + yaml """ +apiVersion: v1 +kind: Pod +metadata: + name: kaniko +spec: + containers: + - name: kaniko + image: gcr.io/kaniko-project/executor:v1.5.1-debug + imagePullPolicy: Always + command: + - /busybox/cat + tty: true + volumeMounts: + - name: jenkins-docker-cfg + mountPath: /kaniko/.docker + volumes: + - name: jenkins-docker-cfg + projected: + sources: + - secret: + name: docker-credentials + items: + - key: data + path: config.json +""" + } + } + environment { + IMAGE_PUSH_DESTINATION="ghcr.io/ruakij/routingtabletowg" + } + stages { + stage('Build with Kaniko') { + steps { + checkout scm + // Define a variable that stores the output of the git describe --tags --long command + def gitTag = sh (returnStdout: true, script: 'git describe --tags --long').trim() + // Define a variable that stores the output of the git rev-parse HEAD command + def commitHash = sh (returnStdout: true, script: 'git rev-parse HEAD').trim() + // Define a variable that stores the output of the git rev-parse --abbrev-ref HEAD command + def branchName = sh (returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim() + // Define a variable that stores the image tag based on the git tag and branch name + def imageTag = '' + // Check if the git tag exists + if (gitTag) { + // Split the git tag by - and . + def parts = gitTag.split(/[-.]/) + // Loop through the parts and append them to the image tag with a comma + for (int i = 0; i < parts.size(); i++) { + imageTag += parts[i] + if (i < parts.size() - 1) { + imageTag += ',' + } + } + } else { + // Use the commit hash as the image tag + imageTag = commitHash + } + // Check if the branch name is main or master + if (branchName == 'main' || branchName == 'master') { + // Append the latest tag to the image tag with a comma + imageTag += ',latest' + } + container(name: 'kaniko', shell: '/busybox/sh') { + withEnv(['PATH+EXTRA=/busybox']) { + // Use the image tag variable as part of the image name when you build and push the image with kaniko + sh '''#!/busybox/sh + /kaniko/executor --context `pwd` --destination $IMAGE_PUSH_DESTINATION --force --build-arg=IMAGE_TAG=$imageTag + ''' + } + } + } + } + } +}