1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| void setBuildStatus(String message, String state, String context) { step([ $class: "GitHubCommitStatusSetter", reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/Zerohertz/docker"], contextSource: [$class: "ManuallyEnteredCommitContextSource", context: context], errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]], statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ] ]); }
pipeline { agent { kubernetes { yaml """ apiVersion: v1 kind: Pod metadata: labels: jenkins/agent-type: kaniko spec: containers: - name: jnlp image: jenkins/inbound-agent:latest resources: requests: memory: "512Mi" cpu: "500m" limits: memory: "1024Mi" cpu: "1000m" - name: ubuntu image: ubuntu:latest command: - sleep args: - "infinity" resources: requests: memory: "512Mi" cpu: "500m" limits: memory: "1024Mi" cpu: "1000m" - name: kaniko image: gcr.io/kaniko-project/executor:debug command: - /busybox/cat tty: true resources: requests: memory: "2048Mi" cpu: "2000m" limits: memory: "4096Mi" cpu: "4000m" volumeMounts: - name: docker-config mountPath: /kaniko/.docker/ volumes: - name: docker-config secret: secretName: docker-config """ } } environment { DOCKERHUB_USERNAME = "zerohertzkr" CHANGE_PATTERNS = "airflow-*" DEFAULT_TAG = "v1.0.0" } stages { stage("Detect Changes") { steps { script { try { setBuildStatus("Detect...", "PENDING", "$STAGE_NAME") def patterns = env.CHANGE_PATTERNS.split(",") def regex = patterns.collect { it.trim() }.join("|") changedDirs = sh(script: "git diff --name-only HEAD^ HEAD | grep -E '${regex}' | xargs -r -n 1 dirname | uniq", returnStdout: true).trim().split("\n") slackSend(color: "good", message: ":+1: <${env.BUILD_URL}|[${env.JOB_NAME}: ${STAGE_NAME}]> SUCCESS\nBRANCH NAME: ${env.BRANCH_NAME}\nChange Dirs: ${changedDirs}") setBuildStatus("Success", "SUCCESS", "$STAGE_NAME") } catch (Exception e) { def STAGE_ERROR_MESSAGE = e.getMessage().split("\n")[0] setBuildStatus(STAGE_ERROR_MESSAGE, "FAILURE", "$STAGE_NAME") slackSend(color: "danger", message: ":-1: <${env.BUILD_URL}|[${env.JOB_NAME}: ${STAGE_NAME}]> FAIL\nBRANCH NAME: ${env.BRANCH_NAME}\nError Message: ${STAGE_ERROR_MESSAGE}") throw e } } } } stage("Kaniko") { when { expression { return changedDirs != [""] } } steps { script { container("ubuntu") { sh """ apt-get update apt-get install -y curl jq """ } for (dir in changedDirs) { def imageName = dir.replaceAll("/", "-") def newTag = "" container("ubuntu") { def apiResponse = sh(script: """ curl -s "https://hub.docker.com/v2/repositories/${DOCKERHUB_USERNAME}/${imageName}/tags/?page_size=100" """, returnStdout: true).trim() echo "Docker Hub API Response: ${apiResponse}" if (apiResponse.contains("httperror 404")) { newTag = env.DEFAULT_TAG } else { def currentTag = sh(script: "echo '${apiResponse}' | jq -r '.results[].name' | sort -V | grep v | tail -n 1", returnStdout: true).trim() echo "Current Tag: ${currentTag}" def version = currentTag.replaceAll("[^0-9.]", "") def (major, minor, patch) = version.tokenize(".").collect { it.toInteger() } newTag = "v${major}.${minor}.${patch + 1}" } echo "New Tag: ${newTag}" } container("kaniko") { script { try { setBuildStatus("Build...", "PENDING", "$STAGE_NAME - ${DOCKERHUB_USERNAME}/${imageName}:${newTag}") sh "/kaniko/executor --context ${dir} --dockerfile ${dir}/Dockerfile --destination ${DOCKERHUB_USERNAME}/${imageName}:latest --cleanup && mkdir -p /workspace" sh "/kaniko/executor --context ${dir} --dockerfile ${dir}/Dockerfile --destination ${DOCKERHUB_USERNAME}/${imageName}:${newTag} --cleanup && mkdir -p /workspace" setBuildStatus("Success", "SUCCESS", "$STAGE_NAME - ${DOCKERHUB_USERNAME}/${imageName}:${newTag}") slackSend(color: "good", message: ":+1: <${env.BUILD_URL}|[${env.JOB_NAME}: ${STAGE_NAME}]> SUCCESS\nBRANCH NAME: ${env.BRANCH_NAME}\nIMAGE: <https://hub.docker.com/repository/docker/zerohertzkr/${imageName}/general|${DOCKERHUB_USERNAME}/${imageName}:${newTag}>") } catch (Exception e) { def STAGE_ERROR_MESSAGE = e.getMessage().split("\n")[0] setBuildStatus(STAGE_ERROR_MESSAGE, "FAILURE", "$STAGE_NAME - ${DOCKERHUB_USERNAME}/${imageName}:${newTag}") slackSend(color: "danger", message: ":-1: <${env.BUILD_URL}|[${env.JOB_NAME}: ${STAGE_NAME}]> FAIL\nBRANCH NAME: ${env.BRANCH_NAME}\nIMAGE: ${DOCKERHUB_USERNAME}/${imageName}:${newTag}\nError Message: ${STAGE_ERROR_MESSAGE}") } } } } } } } } }
|