// Jenkinsfile.test // @version 180903:2 // @author zhangxuhong // def repoName = "gq-hello" def nginxRepoName = "${repoName}-nginx" def label = "worker-${UUID.randomUUID().toString()}" def registryHost = "harbor02.juejin.id/test/" def deployLocation = "test.kube01.lobj.juejin.id" podTemplate( label: label, containers: [ containerTemplate( name: 'git', image: 'harbor02.juejin.id/infrastructure/git-1.8.3.1-centos:0.0.3', command: 'cat', ttyEnabled: true ), containerTemplate( name: 'dind', image: 'harbor02.juejin.id/infrastructure/docker-18.06.0-ce-dind:0.0.2', command: 'cat', ttyEnabled: true ), containerTemplate( name: 'kubectl', image: 'harbor02.juejin.id/infrastructure/kubectl-1.11.1-centos-with-cert-cluster01.lobj:latest', command: 'cat', ttyEnabled: true ), ], volumes: [ hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'), ], cloud: deployLocation ){ node(label) { // start deploy phrase stage("[1/4] check out"){ container('git'){ checkout scm sh "git rev-parse --short HEAD > commit-id" tag = readFile('commit-id').replace("\n", "").replace("\r", "") imageName = "${registryHost}${repoName}:${tag}" nginxImageName = "${registryHost}${nginxRepoName}:${tag}" env.IMAGE_NAME = imageName env.NGINX_IMAGE_NAME = nginxImageName } } stage("[2/4] Build"){ container('dind'){ sh "docker version" sh "docker build -f gq-hello.dockerfile -t ${imageName} ./" sh "docker build -f gq-hello-nginx.dockerfile -t ${nginxImageName} ./" } } stage("[3/4] Push"){ container('dind'){ sh "/usr/local/bin/docker-login.sh" sh "docker push ${imageName}" sh "docker push ${nginxImageName}" } } stage("[4/4] Deploy"){ container('kubectl'){ // deploy nginx sh "sed -i 's#__IMAGE__#$NGINX_IMAGE_NAME#g' ./config/kubernetes/gq-hello-nginx-deployment.yaml" sh "cat ./config/kubernetes/gq-hello-nginx-deployment.yaml" sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-nginx-service.yaml" sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-nginx-ingress.yaml" sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-nginx-deployment.yaml" // deploy repo sh "sed -i 's#__IMAGE__#$IMAGE_NAME#g' ./config/kubernetes/gq-hello-deployment.yaml" sh "cat ./config/kubernetes/gq-hello-deployment.yaml" sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-service.yaml" sh "kubectl --kubeconfig=/root/.kube/config apply -f ./config/kubernetes/gq-hello-deployment.yaml" } } } }