Add Jenkins pipeline for image publish
This commit is contained in:
111
Jenkinsfile
vendored
Normal file
111
Jenkinsfile
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
environment {
|
||||
GIT_URL = 'https://gitea.mindboost.team/mindboost/education-flagger.git'
|
||||
GIT_BRANCH = 'pipeline/deploy-image'
|
||||
REGISTRY_SCHEME = 'https'
|
||||
REGISTRY_AUTHORITY = 'gitea.mindboost.team'
|
||||
IMAGE_NAME = 'mindboost/education-flagger'
|
||||
REGISTRY_CREDENTIALS_ID = 'REGISTRY_CREDENTIALS_ID'
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('Checkout') {
|
||||
steps {
|
||||
script {
|
||||
checkout([
|
||||
$class: 'GitSCM',
|
||||
branches: [[name: "*/${env.GIT_BRANCH}"]],
|
||||
userRemoteConfigs: [[
|
||||
url: env.GIT_URL,
|
||||
credentialsId: 'b5f383be-8c74-40f9-b7e1-3a9c5856df0e'
|
||||
]]
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Check Repository') {
|
||||
steps {
|
||||
script {
|
||||
sh 'pwd'
|
||||
sh 'ls -la'
|
||||
sh 'git status'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Determine Version') {
|
||||
steps {
|
||||
script {
|
||||
def fullHash = sh(
|
||||
script: 'git rev-parse HEAD',
|
||||
returnStdout: true
|
||||
).trim()
|
||||
env.IMAGE_TAG = "sha256-${fullHash}"
|
||||
echo "Resolved image tag: ${env.IMAGE_TAG}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Check Docker Image with the same tag') {
|
||||
steps {
|
||||
script {
|
||||
def imageExists = sh(
|
||||
script: "docker images -q ${env.IMAGE_NAME}:${env.IMAGE_TAG} || true",
|
||||
returnStdout: true
|
||||
).trim()
|
||||
|
||||
if (imageExists) {
|
||||
echo "Docker Image mit Tag ${env.IMAGE_TAG} existiert bereits. Überspringe Build."
|
||||
currentBuild.result = 'SUCCESS'
|
||||
return
|
||||
} else {
|
||||
echo "Kein vorhandenes Docker Image gefunden. Baue neues Image..."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Build Docker Image') {
|
||||
when {
|
||||
expression { currentBuild.result == null }
|
||||
}
|
||||
steps {
|
||||
script {
|
||||
sh "docker build --rm -t ${env.IMAGE_NAME}:${env.IMAGE_TAG} ."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Push Docker Image') {
|
||||
when {
|
||||
expression { currentBuild.result == null }
|
||||
}
|
||||
steps {
|
||||
script {
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: env.REGISTRY_CREDENTIALS_ID,
|
||||
usernameVariable: 'REGISTRY_USER',
|
||||
passwordVariable: 'REGISTRY_PASS'
|
||||
)]) {
|
||||
def registryEndpoint = "${env.REGISTRY_SCHEME}://${env.REGISTRY_AUTHORITY}"
|
||||
sh "echo '${REGISTRY_PASS}' | docker login ${env.REGISTRY_AUTHORITY} -u '${REGISTRY_USER}' --password-stdin"
|
||||
sh "docker tag ${env.IMAGE_NAME}:${env.IMAGE_TAG} ${env.REGISTRY_AUTHORITY}/${env.IMAGE_NAME}:${env.IMAGE_TAG}"
|
||||
sh "docker push ${env.REGISTRY_AUTHORITY}/${env.IMAGE_NAME}:${env.IMAGE_TAG}"
|
||||
sh "docker logout ${env.REGISTRY_AUTHORITY}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Cleanup Docker Images') {
|
||||
steps {
|
||||
script {
|
||||
sh 'set -eux; docker image prune -f; docker builder prune -f'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user