def ServiceName = "demo"
def ProjectDir = "demo"
def DockerfileDir = "project/demo"
def gitCommit
def gitBranch
def shortGitCommit
node {
def DOCKER_HUB_ADDR='registry.test.inside'
stage('Git Clone and setup') {
dir("docker") {
git credentialsId: 'Testgitlab', url: 'http://git.test.work/ops/cicd.git', branch: 'test'
}
dir("source") {
def myRepo = checkout([$class: 'GitSCM',
branches: [[name: '*/test']],
userRemoteConfigs: [[credentialsId: '70cxxx7-2xce-4xef-9xdf-a5ab6xxx198b', url: 'git@git.test.work:project/demo.git']]
])
gitCommit = myRepo.GIT_COMMIT
gitBranch = myRepo.GIT_BRANCH
shortGitCommit = "${gitCommit[0..5]}"
}
}
stage('Build') {
try {
sh """
mvn -s /etc/maven/settings.xml -f source/pom.xml clean install package -U -X -Ptest -Dmaven.test.skip=true
"""
}
catch (exc) {
println "Failed to test - ${currentBuild.fullDisplayName}"
throw(exc)
}
}
stage('Sonarqube') {
sh """
mvn -s /etc/maven/settings.xml -f source/pom.xml sonar:sonar -Dsonar.organization="test" -Dsonar.projectKey=${ServiceName} \
-Dsonar.projectName=${ServiceName} -Dsonar.host.url=http://sonar.test.work:9000 -Dsonar.login=xxxxxe2c63f
"""
}
stage('Publish Docker') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'dockerhub',
passwordVariable: 'DOCKER_HUB_PASSWORD',
usernameVariable: 'DOCKER_HUB_USER']]) {
sh """
docker login ${DOCKER_HUB_ADDR} -u ${DOCKER_HUB_USER} -p ${DOCKER_HUB_PASSWORD}
docker build \
--build-arg app=${ServiceName} \
-t ${DOCKER_HUB_ADDR}/project/${ServiceName}:${shortGitCommit} \
-f docker/${DockerfileDir}/Dockerfile \
source/${ProjectDir}/target
docker push ${DOCKER_HUB_ADDR}/project/${ServiceName}:${shortGitCommit}
"""
}
}
stage('Deploy to test') {
sh """
helm init \
--client-only \
--stable-repo-url \
http://10.20.10.106:8080
helm repo update
if helm ls --output=yaml |grep "Chart: ${ServiceName}-0";then
helm \
upgrade \
${ServiceName} \
--set image.tag=${shortGitCommit},image.repository=${DOCKER_HUB_ADDR}/project/${ServiceName} \
--recreate-pods \
--description "Upgrade -> ${shortGitCommit}" \
stable/${ServiceName}
else
helm \
install \
--namespace=default \
--name=${ServiceName} \
--set image.tag=${shortGitCommit} \
--description "Install -> ${shortGitCommit}" \
stable/${ServiceName}
fi
"""
}
}