이건 쿠버네티스 용
pipeline {
agent any
environment {
PATH = "$HOME/bin:$PATH"
}
triggers {
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref'],
[key: 'clone_url', value: '$.repository.git_http_url'],
[key: 'changed_files', value: '$.commits[*].[\\'modified\\',\\'added\\',\\'removed\\'][*]']
],
token : 'secret',
causeString: 'Triggered on $ref',
printContributedVariables: true,
printPostContent: true,
regexpFilterText: '$ref',
regexpFilterExpression: '^(refs/heads/develop)'
)
}
stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'develop']],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[url: '###',
credentialsId: '###']]
])
}
}
stage('secret.yml download') {
steps {
withCredentials([file(credentialsId: '###', variable: 'application')]) {
script {
sh 'chmod -R 755 backend/community/src/main/resources/'
sh 'cp $application backend/community/src/main/resources/application-local.yml'
}
}
withCredentials([file(credentialsId: 'user-private', variable: 'userPrivate')]) {
script {
sh 'chmod -R 755 backend/user/src/main/resources/'
sh 'cp $userPrivate backend/user/src/main/resources/application-private.yml'
}
}
withCredentials([file(credentialsId: 'bank-private', variable: 'bankPrivate')]) {
script {
sh 'chmod -R 755 backend/bank/src/main/resources/'
sh 'cp $bankPrivate backend/bank/src/main/resources/application-private.yml'
}
}
withCredentials([file(credentialsId: 'gateway-private', variable: 'gatewayPrivate')]) {
script {
sh 'chmod -R 755 backend/gateway/src/main/resources/'
sh 'cp $gatewayPrivate backend/gateway/src/main/resources/application-private.yml'
}
}
}
}
stage('Build and Deploy Backend Services') {
when {
expression {
def files = parseChangedFiles(changed_files)
return files.any { it.trim().startsWith('backend/') }
}
}
steps {
script {
def dockerRegistry = '###'
def dockerRegistryUrl = '<https://index.docker.io/v1/>'
def dockerCredentialsId = '###'
def services = ['user', 'community', 'bank', 'gateway']
def files = parseChangedFiles(changed_files)
services.each { service ->
if (files.any { it.trim().startsWith("backend/${service}") }) {
dir("backend/${service}") {
sh 'chmod +x ./gradlew'
sh './gradlew build'
withCredentials([usernamePassword(credentialsId: '###', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh "docker build -t ${dockerRegistry}:${service}-latest ."
// sh "docker login -u ${DOCKER_USERNAME} -p "
sh "docker login -u ${DOCKER_USERNAME} --password-stdin << EOF\\n${DOCKER_PASSWORD}\\nEOF"
sh "docker push ${dockerRegistry}:${service}-latest"
}
// docker.withRegistry(dockerRegistryUrl, dockerCredentialsId) {
// def app = docker.build("${dockerRegistry}:${service}-latest")
// app.push()
// // sh "docker build -t ${dockerRegistry}:${service}-latest ."
// // sh "docker push ${dockerRegistry}:${service}-latest"
// }
sh "kubectl apply -f k8s/deployment.yaml"
sh "kubectl apply -f k8s/service.yaml"
sh "kubectl rollout restart deploy ${service}"
}
}
}
}
}
}
stage('Build and Deploy Frontend') {
when {
expression {
def files = parseChangedFiles(changed_files)
return files.any { it.trim().startsWith('frontend/') }
}
}
steps {
script {
def dockerRegistry = '###'
def dockerRegistryUrl = '<https://index.docker.io/v1/>'
def dockerCredentialsId = '###'
dir('frontend') {
sh 'npm install'
sh 'npm run build'
withCredentials([usernamePassword(credentialsId: '###', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh "docker build -t ${dockerRegistry}:frontend-latest ."
// sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
sh "docker login -u ${DOCKER_USERNAME} --password-stdin << EOF\\n${DOCKER_PASSWORD}\\nEOF"
sh "docker push ${dockerRegistry}:frontend-latest"
}
// docker.withRegistry(dockerRegistryUrl, dockerCredentialsId) {
// // def app = docker.build("${dockerRegistry}:frontend-latest")
// // app.push()
// sh "docker build -t ${dockerRegistry}:frontend-latest ."
// sh "docker push ${dockerRegistry}:frontend-latest"
// }
sh "kubectl apply -f k8s/deployment.yaml"
sh "kubectl apply -f k8s/service.yaml"
sh "kubectl rollout restart deploy frontend"
}
}
}
}
}
post {
success {
script {
def customBuildUrl = env.BUILD_URL.replaceAll(":8080", "/jenkins")
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (color: 'good',
message: "빌드 성공: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\\n(<${customBuildUrl}|Details>)",
endpoint: '<https://meeting.ssafy.com/hooks/x5xdofue13fe7esrqiqjoi3heo>',
channel: 'A505-alarm'
)
}
}
failure {
script {
def customBuildUrl = env.BUILD_URL.replaceAll(":8080", "/jenkins")
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (color: 'danger',
message: "빌드 실패: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\\n(<${customBuildUrl}|Details>)",
endpoint: '<https://meeting.ssafy.com/hooks/x5xdofue13fe7esrqiqjoi3heo>',
channel: 'A505-alarm'
)
}
}
}
}
@NonCPS
def parseChangedFiles(String changedFiles) {
def jsonSlurper = new groovy.json.JsonSlurper()
def files = jsonSlurper.parseText(changed_files)
return files
}