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
| def label = "slave-${UUID.randomUUID().toString()}"
def helmLint(String chartDir) { println "校验 chart 模板" sh "helm lint ${chartDir}" }
def helmDeploy(Map args) { if (args.debug) { println "Debug 应用" sh "helm upgrade --dry-run --debug --install ${args.name} ${args.chartDir} -f ${args.valuePath} --set image.tag=${args.imageTag} --namespace ${args.namespace}" } else { println "部署应用" sh "helm upgrade --install ${args.name} ${args.chartDir} -f ${args.valuePath} --set image.tag=${args.imageTag} --namespace ${args.namespace}" echo "应用 ${args.name} 部署成功. 可以使用 helm status ${args.name} 查看应用状态" } }
podTemplate(label: label, containers: [ containerTemplate(name: 'golang', image: 'golang:1.14.2-alpine3.11', command: 'cat', ttyEnabled: true), containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true), containerTemplate(name: 'helm', image: 'cnych/helm', command: 'cat', ttyEnabled: true), containerTemplate(name: 'kubectl', image: 'cnych/kubectl', command: 'cat', ttyEnabled: true) ], serviceAccount: 'jenkins', envVars: [ envVar(key: 'DOCKER_HOST', value: 'tcp://docker-dind:2375') ]) { node(label) { def myRepo = checkout scm def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() def registryUrl = "harbor.k8s.local" def imageEndpoint = "course/devops-demo" def image = "${registryUrl}/${imageEndpoint}:${imageTag}"
stage('单元测试') { echo "测试阶段" } stage('代码编译打包') { try { container('golang') { echo "2.代码编译打包阶段" sh """ export GOPROXY=https://goproxy.cn GOOS=linux GOARCH=amd64 go build -v -o demo-app """ } } catch (exc) { println "构建失败 - ${currentBuild.fullDisplayName}" throw(exc) } } stage('构建 Docker 镜像') { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'docker-auth', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASSWORD']]) { container('docker') { echo "3. 构建 Docker 镜像阶段" sh """ cat /etc/resolv.conf docker login ${registryUrl} -u ${DOCKER_USER} -p ${DOCKER_PASSWORD} docker build -t ${image} . docker push ${image} """ } } } stage('运行 Helm') { withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { container('helm') { sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config" echo "4.开始 Helm 部署" def userInput = input( id: 'userInput', message: '选择一个部署环境', parameters: [ [ $class: 'ChoiceParameterDefinition', choices: "Dev\nQA\nProd", name: 'Env' ] ] ) echo "部署应用到 ${userInput} 环境" if (userInput == "Dev") { } else if (userInput == "QA"){ } else { } helmDeploy( debug : false, name : "devops-demo", chartDir : "./helm", namespace : "kube-ops", valuePath : "./helm/my-values.yaml", imageTag : "${imageTag}" ) } } } stage('运行 Kubectl') { withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { container('kubectl') { sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config" echo "5.查看应用" sh "kubectl get all -n kube-ops -l app=devops-demo" } } } stage('快速回滚?') { withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) { container('helm') { sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config" def userInput = input( id: 'userInput', message: '是否需要快速回滚?', parameters: [ [ $class: 'ChoiceParameterDefinition', choices: "Y\nN", name: '回滚?' ] ] ) if (userInput == "Y") { sh "helm rollback devops-demo --namespace kube-ops" } } } } } }
|