refs #4685 Jenkinsfile improved for branch deploy
gitea/mycdc/pipeline/pr-master There was a failure building this commit Details

This commit is contained in:
Juan Ferrer 2025-02-26 17:06:54 +01:00
parent 6e8ea95e53
commit 7a8a7fe4d1
1 changed files with 76 additions and 12 deletions

96
Jenkinsfile vendored
View File

@ -1,33 +1,81 @@
#!/usr/bin/env groovy #!/usr/bin/env groovy
def PROTECTED_BRANCH
def IS_LATEST
node {
stage('Setup') {
PROTECTED_BRANCH = [
'dev',
'test',
'master',
'main',
'beta'
].contains(env.BRANCH_NAME)
IS_LATEST = ['master', 'main'].contains(env.BRANCH_NAME)
// https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
echo "NODE_NAME: ${env.NODE_NAME}"
echo "WORKSPACE: ${env.WORKSPACE}"
}
}
pipeline { pipeline {
agent any agent any
options { options {
disableConcurrentBuilds() disableConcurrentBuilds()
} }
stages { stages {
stage('Version') {
when {
expression { PROTECTED_BRANCH }
}
steps {
script {
def packageJson = readJSON file: 'package.json'
def version = "${packageJson.version}-build${env.BUILD_ID}"
writeFile(file: 'VERSION.txt', text: version)
echo "VERSION: ${version}"
}
}
}
stage('Build') { stage('Build') {
when {branch 'master'} when {
steps { expression { PROTECTED_BRANCH }
script {
def packageJson = readJSON file: 'package.json'
env.VERSION = packageJson.version
} }
sh 'docker-compose build --build-arg BUILD_ID=$BUILD_ID --parallel'
}
}
stage('Push') {
when {branch 'master'}
environment { environment {
CREDENTIALS = credentials('docker-registry') VERSION = readFile 'VERSION.txt'
}
parallel {
stage('Producer') {
steps {
dockerBuild 'mycdc-producer', '.', 'assets/Dockerfile.producer'
}
}
stage('Consumer') {
steps {
dockerBuild 'mycdc-consumer', '.', 'assets/Dockerfile.consumer'
}
}
}
}
stage('Deploy') {
when {
branch 'dev'
branch 'test'
}
environment {
VERSION = readFile 'VERSION.txt'
} }
steps { steps {
script { withKubeConfig([
def packageJson = readJSON file: 'package.json' serverUrl: "$KUBERNETES_API",
env.VERSION = packageJson.version credentialsId: 'kubernetes',
namespace: 'mycdc'
]) {
sh 'kubectl set image deployment/producer-$BRANCH_NAME main=$REGISTRY/mycdc-producer:$VERSION'
sh 'kubectl set image deployment/consumer-$BRANCH_NAME main=$REGISTRY/mycdc-consumer:$VERSION'
} }
sh 'docker login --username $CREDENTIALS_USR --password $CREDENTIALS_PSW $REGISTRY'
sh 'docker-compose push'
} }
} }
} }
@ -38,3 +86,19 @@ pipeline {
} }
} }
} }
def dockerBuild(imageName, context, dockerfile = null) {
if (dockerfile == null)
dockerfile = "${context}/Dockerfile"
def baseImage = "${imageName}:${env.VERSION}"
def image = docker.build(baseImage, "-f ${dockerfile} ${context}")
dockerPush(image)
}
def dockerPush(image) {
docker.withRegistry("https://${env.REGISTRY}", 'docker-registry') {
image.push()
image.push(env.BRANCH_NAME)
if (IS_LATEST) image.push('latest')
}
}