76 lines
1.7 KiB
Groovy
76 lines
1.7 KiB
Groovy
|
|
pipeline {
|
|
agent any
|
|
|
|
stages {
|
|
stage('Cloning configuration repository'){
|
|
steps {
|
|
dir('ops-jenkins-config'){
|
|
git branch: 'main',
|
|
url: 'ssh://git@git.gansejunge.com:1322/devops-project/ops-jenkins-config.git',
|
|
credentialsId: 'jenkins-ssh'
|
|
}
|
|
}
|
|
}
|
|
stage('Load configuration files') {
|
|
steps {
|
|
script {
|
|
def parts = env.JOB_NAME.split('-')
|
|
env.service = parts[0..-2].join('-')
|
|
env.stage = parts[-1]
|
|
|
|
def configFile = "ops-jenkins-config/values/${env.service}/${env.stage}.env"
|
|
def props = loadEnvFile(configFile)
|
|
props.each { k, v ->
|
|
env."${k}" = v
|
|
}
|
|
env.gitRepository = "ssh://git@git.gansejunge.com:1322/devops-project/${env.service}.git"
|
|
}
|
|
}
|
|
|
|
stage('Clean Workspace') {
|
|
steps { deleteDir() }
|
|
}
|
|
|
|
stage('Checkout Code') {
|
|
steps {
|
|
git branch: "${env.gitBranch}",
|
|
url: "${env.gitRepository}",
|
|
credentialsId: 'jenkins-ssh'
|
|
}
|
|
}
|
|
|
|
stage('Get Commit ID') {
|
|
steps {
|
|
script {
|
|
env.gitCommitID = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build Docker Image') {
|
|
steps {
|
|
script {
|
|
def imageTag = "${env.service}:${gitCommitID}-${env.BUILD_NUMBER}"
|
|
def buildArgs = "--build-arg ARTIFACT_NAME=${env.service} --build-arg EXPOSED_PORT=${env.serviceExposedPort}"
|
|
sh "docker build -t ${imageTag} ${buildArgs} ."
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
def loadEnvFile(path) {
|
|
def map = [:]
|
|
def content = readFile(path)
|
|
content.split('\n').each { line ->
|
|
line = line.trim()
|
|
if (!line || line.startsWith('#')) return
|
|
def parts = line.split('=', 2)
|
|
if (parts.size() == 2) {
|
|
map[parts[0].trim()] = parts[1].trim()
|
|
}
|
|
}
|
|
return map
|
|
}
|
|
|