Pipeline 소개

 


Create New Item

Click pipeline

 

Discard Old builds

 


Build Triggers

Bild periodically

Minute(0-59) Hour(0-23) DayOfMonth(1-31) Month(1-12) DayOfWeek(0-6, Sunday to Saturday)

example: corn('00 11 * * 1-5') - 11:00 AM every Monday to Friday

# Monday to Friday, every 13:00, 22:00 triggers { cron('00 13,22 * * 1-5') }

 


Use Credential

GIT_CREDENTIAL = "a71612d3-0209-4503-9b2f-a9ea321d2426" SLACK_TOKEN = "slacktoken"

 

 


Slack Notification

 

 


Parameter

 

 

 


ssh publisher

 

 


Pipeline Script

properties([ parameters([ /* To write Custom branch for developer when you select custom option on ascendant parameter ascendant parameter : GIT_BRANCH Default value: custom * Must be write created branch on your git ex) JIRANAME_ISSUEID(ICE4-120) */ [$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HTML', name: 'GIT_BRANCH_CUSTOM', referencedParameters: 'GIT_BRANCH', description: '', omitValueField: true, script: [ $class: 'GroovyScript', script: [ classpath: [], sandbox: false, script: ''' switch(GIT_BRANCH){ case~/.*custom.*/: return """<input name="value" value="custom" class="setting-input" type="text">""" break default: return "" break } ''' ], fallbackScript: [ classpath: [], sandbox: false, script: "return ''" ], ] ], ]) ]) // To get build user name def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause') pipeline { agent any tools { nodejs "NODE_HOME" } /* Build periodically (Minute Hour DayOfMonth Month DayOfWeek) Minute(0-59) Hour(0-23) DayOfMonth(1-31) Month(1-12) DayOfWeek(0-6, Sunday to Saturday) ('00 11 * * 1-5') - 11:00 AM every Monday to Friday */ triggers { cron('00 13,22 * * 1-5') } /* To set up env to use jenkins script Credential - GIT_CREDENTIAL, SLACK_TOKEN -> set up Http://${JENKINS_HOME}/Credentials GIT_URL SLACK_CHANNEL */ environment { GIT_URL = "http://dcsf-dev08.i-on.net/dxp/backendcore.git" GIT_BRANCH = "develop" GIT_CREDENTIAL = "a71612d3-0209-4503-9b2f-a9ea321d2426" SLACK_TOKEN = "slacktoken" SLACK_CHANNEL = "#jenkins-test" RED = "#F25749" GREEN = "#50BFA0" YELLOW = "#F2C438" BLUE = "#34A6BF" } parameters { // Option for Git checkout use booleanParam(name: 'UES_GIT_CHECKOUT', defaultValue: true) // Option for Build booleanParam(name: 'USE_BUILD', defaultValue: true) // Option for TEST booleanParam(name: 'USE_TEST', defaultValue: true) // Option for DEPLOY booleanParam(name: 'USE_DEPLOY', defaultValue: true) // Option for SWITCH booleanParam(name: 'USE_SWITCH', defaultValue: true) // Back-Up cache booleanParam(name: 'BACKUP_CACHE', defaultValue: true) // Delete Jenkins Workspace booleanParam(name: 'DELETE_JENKINS_WORKSPACE', defaultValue: true) // Delete cache booleanParam(name: 'DELETE_CACHE', defaultValue: false) // Delete project on docker booleanParam(name: 'DELETE_PROJECT', defaultValue: false) // Create project on docker booleanParam(name: 'CREATE_PROJECT', defaultValue: false) // Docker PORT choice(name: 'DOCKER_PORT', choices: [ '8080' ]) // string(name: 'DOCKER_PORT', defaultValue: '8080') choice(name: 'NGINX_PORT', choices: [ '8080' ]) // string(name: 'NGINX_PORT', defaultValue: '8080') // SSH SERVER INFORMAION ex) dev, stg, prd, test choice(name: 'SERVER_LEVEL', choices: [ 'dev' ]) // Select Git branch choice(name: 'GIT_BRANCH', choices: [ "develop", "project/EMMP", "custom"]) } stages { // Whole Flow Check stage('Flow Check') { steps { script { echo "Flow Check \nUES_GIT_CHECKOUT = ${UES_GIT_CHECKOUT} \nUSE_BUILD = ${USE_BUILD} \nUSE_TEST = ${USE_TEST} \nUSE_DEPLOY = ${USE_DEPLOY} \nUSE_SWITCH = ${USE_SWITCH} \nBACKUP_CACHE = ${BACKUP_CACHE} \nDELETE_CACHE = ${DELETE_CACHE}\nDELETE_PROJECT = ${DELETE_PROJECT}\nCREATE_PROJECT = ${CREATE_PROJECT}" // send slack message to notify start build process } } } // Parameter Check except of credential stage('Parameter Check') { steps { script { echo "SERVER_LEVEL = ${SERVER_LEVEL} \nSelected GIT URL = ${GIT_URL} \nBranch = ${GIT_BRANCH} ${GIT_BRANCH_CUSTOM} \n" } } } /* Git Check out UES_GIT_CHECKOUT = Use, Must be selected to check out git source Git Url : GIT_URL Git Branch : GIT_BRANCH or GIT_BRANCH_CUSTOM Auth : GIT_CREDENTIAL */ stage('GIT Check Out') { steps { script { if(UES_GIT_CHECKOUT.equals("true")){ echo "Git Check out Start" if(GIT_BRANCH != "custom"){ git branch: "${GIT_BRANCH}", credentialsId: "${GIT_CREDENTIAL}", url: "${GIT_URL}" echo "Git Check out Finish: ${GIT_BRANCH}" } else if(GIT_BRANCH.equals("custom")){ git branch: "${GIT_BRANCH_CUSTOM}", credentialsId: "${GIT_CREDENTIAL}", url: "${GIT_URL}" echo "Git Check out Finish: ${GIT_BRANCH_CUSTOM}" } } else { echo "Git Check out SKIP" } // echo "author = ${author}" // echo "last commit message = ${message}" slackSend ( message: "Build Start ${currentBuild.fullDisplayName} (<${BUILD_URL}|Open>)\nServer : ${SERVER_LEVEL}\nGit Branch: ${GIT_BRANCH} ${GIT_BRANCH_CUSTOM} \nBuild Start By `${specificCause.userName[0]}`", username: "${JOB_NAME}", channel: "${SLACK_CHANNEL}", color: "${GREEN}", tokenCredentialId: "${SLACK_TOKEN}", ) } } } stage('Build') { steps { script{ if(USE_BUILD.equals("true")){ echo "Gradle Clean and Build start" withGradle { sh './gradlew clean build -x test --refresh-dependencies --stacktrace' } echo "Gradle Clean and Build end" }else{ echo "Build/Launch skip" } } } } stage('Backup/Clean') { stages{ stage('Backup Cache') { steps { script{ if(BACKUP_CACHE.equals("true")){ echo "Backup Cache start" sshPublisher( publishers: [ sshPublisherDesc( configName: "${SERVER_LEVEL}", transfers: [ sshTransfer( remoteDirectory: "${JOB_BASE_NAME}", execCommand: '''pwd ~/${JOB_BASE_NAME}/shell/ice4-aws-all.sh backup ${JOB_BASE_NAME} ${SERVER_LEVEL} ''' , excludes: '', cleanRemote: false, execTimeout: 1280000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false, )], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false )]) echo "Cache backup finish" } else { echo "Cache backup skip" } } } } stage('Delete Cache'){ steps { script{ if(DELETE_CACHE.equals("true")){ echo "Cache Delete start" sshPublisher( publishers: [ sshPublisherDesc( configName: "${SERVER_LEVEL}", transfers: [ sshTransfer( remoteDirectory: "${JOB_BASE_NAME}", execCommand: '''pwd ~/${JOB_BASE_NAME}/shell/ice4-aws-all.sh delete ${JOB_BASE_NAME} ${SERVER_LEVEL} ''', excludes: '', cleanRemote: false, execTimeout: 1280000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false, )], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false )]) echo "Cache Delete Finish" }else { echo "Cache Delete skip" } } } } /* Delete All Project Files DELETE_CACHE = Use, Must be selected to delete All Project Files Process: Docker stop Remove Folder Make Folder Give Authrisation as a 775 configName: select SSH server which server run project. ${SERVER_LEVEL} parameter use to access. (ex. dev, stg, prd) remoteDirectory: link directory ${JOB_BASE_NAME} parameter use to link directory execCommand: to use Shell command line excludes: To excludes file when send file into SSH server */ stage('Delete Project'){ steps { script{ if(DELETE_PROJECT.equals("true")){ echo "Delete Project Start" retry(3) { sshPublisher( publishers: [ sshPublisherDesc( configName: "${SERVER_LEVEL}", transfers: [ sshTransfer( remoteDirectory: "${JOB_BASE_NAME}", execCommand: '''pwd docker stop ${JOB_BASE_NAME} docker rm -f ${JOB_BASE_NAME} docker rmi -f ${JOB_BASE_NAME} sudo rm -rf ~/${JOB_BASE_NAME} ''', excludes: '', cleanRemote: false, execTimeout: 1280000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, remoteDirectorySDF: false, patternSeparator: '[, ]+', )], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false )]) } echo "Delete Project Finish" }else { echo "Delete Project Stop" } } } } } } stage('Deploy') { stages{ stage('Create Docker Image') { steps{ script { echo "Create docker image start" if(USE_DEPLOY.equals("true") && CREATE_PROJECT.equals("true")){ sshPublisher( publishers: [ sshPublisherDesc( configName: "${SERVER_LEVEL}", transfers: [ sshTransfer( remoteDirectory: "${JOB_BASE_NAME}", execCommand: '''pwd chmod 775 -R ~/${JOB_BASE_NAME} mkdir ~/${JOB_BASE_NAME}/resource && chmod 775 -R ~/${JOB_BASE_NAME}/resource docker image build -f ~/${JOB_BASE_NAME}/Dockerfile -t ${JOB_BASE_NAME} ./${JOB_BASE_NAME} --build-arg SERVER_LEVEL=${SERVER_LEVEL} --build-arg JENKINS_WORKSPACE=${JOB_BASE_NAME} --build-arg DOCKER_PORT=${DOCKER_PORT} docker container run --restart="always" --privileged -d -p 8443:8443 -p 5701:5701 -p ${NGINX_PORT}:${DOCKER_PORT} -v ~/${JOB_BASE_NAME}:/root/core --name ${JOB_BASE_NAME} ${JOB_BASE_NAME}:latest ~/${JOB_BASE_NAME}/shell/ice4-aws-all.sh cacheLoad ${JOB_BASE_NAME} ${SERVER_LEVEL} initload ''', excludes: '', cleanRemote: false, execTimeout: 2560000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false, sourceFiles: '**/*.war, shell/backendCore-aws-all.sh, shell/ice4-aws-all.sh, Dockerfile' )], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false )]) echo "create docker image finish" }else { echo "create docker image Skip" } } } } stage('Restart Docker Container') { steps { retry(5) { script { if(USE_DEPLOY.equals("true") && CREATE_PROJECT != "true" && DELETE_CACHE !="true"){ echo "Restart Docker Container start" sshPublisher( publishers: [ sshPublisherDesc( configName: "${SERVER_LEVEL}", transfers: [ sshTransfer( remoteDirectory: "${JOB_BASE_NAME}", execCommand: '''pwd docker restart ${JOB_BASE_NAME} ~/${JOB_BASE_NAME}/shell/ice4-aws-all.sh cacheLoad ${JOB_BASE_NAME} ${SERVER_LEVEL} initload ''', excludes: '', cleanRemote: false, execTimeout: 2560000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false, sourceFiles: '**/*.war,shell/backendCore-aws-all.sh, shell/ice4-aws-all.sh' )], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false )]) echo "Restart Docker Container finish" }else{ echo "Restart Docker Container skip" } } } } } stage('Restart Docker Container - Delete Cache') { steps { retry(5) { script { if(USE_DEPLOY.equals("true") && CREATE_PROJECT != "true" && DELETE_CACHE.equals("true")){ echo "Restart Docker Container start" sshPublisher( publishers: [ sshPublisherDesc( configName: "${SERVER_LEVEL}", transfers: [ sshTransfer( remoteDirectory: "${JOB_BASE_NAME}", execCommand: '''pwd docker restart ${JOB_BASE_NAME} ~/${JOB_BASE_NAME}/shell/ice4-aws-all.sh cacheLoad ${JOB_BASE_NAME} ${SERVER_LEVEL} initload ''', excludes: '', cleanRemote: false, execTimeout: 1280000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false, sourceFiles: '**/*.war,shell/backendCore-aws-all.sh, shell/ice4-aws-all.sh' )], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false )]) echo "Restart Docker Container finish" }else{ echo "Restart Docker Container skip" } } } } } } } stage('Test - Postman') { steps { script { echo "Test start" // if(USE_TEST.equals("true")){ // To stop error when test failed // try{ // sh "newman run /var/lib/jenkins/workspace/${env.JOB_NAME}/postman/new/QA-Postman-Test.postman_collection.json -e /var/lib/jenkins/workspace/${env.JOB_NAME}/postman/new/Dev-Env.postman_environment.json" //sh "newman run /var/lib/jenkins/workspace/${JOB_NAME}/postman/core/admTest.json -e /var/lib/jenkins/workspace/${env.JOB_NAME}/postman/new/Dev-Env.postman_environment.json" // } catch (Exception error){ // slackSend ( // message: "${SERVER_LEVEL} TEST Failed (<${BUILD_URL}|Open>) : \n${error}. ", // username: "${JOB_NAME}", // channel: "${SLACK_CHANNEL}", // color:"${RED}", // tokenCredentialId: "${SLACK_TOKEN}", // ) // echo "${error}" // break // } // echo "Test end" // }else{ // echo "Test skip" // } } } } } post('Post') { always { echo "One way or another, I have finished. Result: ${currentBuild.result}" script { if(DELETE_JENKINS_WORKSPACE.equals("true")){ echo "Delete Jenkins Workspace" deleteDir() } } } success { // send slack message to notify start build process slackSend ( message: "Deploy Success ${JOB_BASE_NAME}\nServer : ${SERVER_LEVEL} (<${BUILD_URL}|Open>)", username: "${JOB_NAME}", channel: "${SLACK_CHANNEL}", color: "${GREEN}", tokenCredentialId: "${SLACK_TOKEN}", ) echo 'I succeeeded!' } failure { slackSend ( message: "Deploy Failure ${JOB_BASE_NAME}\nServer : ${SERVER_LEVEL} (<${BUILD_URL}|Open>)", username: "${JOB_NAME}", channel: "${SLACK_CHANNEL}", color:"${RED}", tokenCredentialId: "${SLACK_TOKEN}", ) echo 'I failed :(' } unstable { slackSend ( message: "Deploy Unstable ${JOB_BASE_NAME}\nServer : ${SERVER_LEVEL} (<${BUILD_URL}|Open>)", username: "${JOB_NAME}", channel: "${SLACK_CHANNEL}", color:"${YELLOW}", tokenCredentialId: "${SLACK_TOKEN}", ) echo 'I am unstable :/' } changed { echo 'Things were different before...' } } }