Jenkins Pipeline
1) Gitlab 접속 Checkout
2) Gradle 파일 가져와서 버전 체크(0.0.1)
3) 해당버전 Docker Repository 있는지 체크
4) 없으면 빌드 후 Docker Repository에 PUSH
5) ArgoCd가 Auto Sync 하고 있는 Helm 차트에 Values.yaml 파일을 업데이트
def version = ""
pipeline {
agent any
tools {
gradle "Gradle 7.4.1"
jdk("JDK 11.0.2")
}
stages{
stage("Check Build"){
steps{
script{
def functionList = ["function1", "function2", "function3", "function4", "function5", "function6"]
def cnt = 0
def GIT_VARS = git branch: 'dev',
credentialsId: 'jenkins_build',
// 실제는 gitlab 서버
url: 'https://github.com/Namppo/namppo.git'
for ( function in functionList ) {
//Gradle 에 입력한 버전정보 가져오기
version = sh(returnStdout: true, script: "cat ${function}/build.gradle | grep -o 'version [^,]*' | cut -d " + "\"'\"" + " -f 2" ).trim()
cnt = cnt + 1
echo "***************************************************************"
echo "$cnt : ${function}"
echo "Gradle버전 : ${version}"
echo "***************************************************************"
// Docker Repository(Nexus)에 해당 버전의 매니페스트 파일이 존재 하는지 검사
// 해당 파일에 이미지가 존재하면 미 배포
def isExists = sh(returnStatus : true, script: "curl -v http://myDockerRegistry.com:8081/repository/docker-registry/v2/${function}/manifests/${version} --stderr - | grep size" )
if ( isExists == 1 ) {
echo "##########빌드 & 배포 시작##########"
// 1. Gradle Build
sh "gradle bootJar -x test -b ${function}/build.gradle"
// 2. Docker build
sh "docker build -t ${function}:${version} --build-arg APP_VERSION=${version} -f ${function}/Dockerfile ."
sh "docker image tag ${function}:${version} myDockerRegistry:5500/${function}:${version}"
sh "docker push myDockerRegistry:5500/${function}:${version}"
// 3. Argocd Helm 차트 values.yaml Deployment 가져오기
GIT_VARS = git branch: 'master',
credentialsId: 'jenkins_build',
url: 'https://github.com/Namppo/argocd.git'
// 4. Git Deployment Update
sh "yq -i '.container.${function}.tag = \"" + "${version}" + "\"" + "' /values.yaml"
// 5. Git Push
withCredentials([gitUsernamePassword(credentialsId: 'jenkins_build', gitToolName: 'Default')]) {
sh "git config user.name 'jenkins'"
sh "git config user.email 'jenkins@test.com'"
sh "git add 'values.yaml'"
sh "git commit -m 'Build : ${BUILD_TIMESTAMP} and update values.yaml'"
sh "git push -u origin master"
}
} else {
// 이미지가 이미 존재(배포 안함)
echo "##########해당 버전 이미지가 이미 존재##########"
}
}
}
}
}
}
}