def KUBERNETES_NAME = "kubernetes" def DOCKER_REPOSITORY_CREDENTIAL_ID = "ea3d4aae-783b-460f-9cd0-931c53c021db" def HARBOR_HOST = "http://192.168.75.149:30002" def NAMESPACE_NAME = "library" def REPOSITORY_NAME = "my-web-app" def TAG = "v1.0" pipeline { agent { kubernetes { cloud "${KUBERNETES_NAME}" // 增加超时时间为60分钟 containerTemplate { name 'docker' image 'docker:19.03.15-git' tty true command 'cat' } yaml """ apiVersion: v1 kind: Pod spec: containers: - name: jnlp image: jenkins/inbound-agent:3107.v665000b_51092-15 args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)'] imagePullPolicy: IfNotPresent volumeMounts: - mountPath: "/etc/localtime" name: "volume-2" readOnly: false - name: kubectl image: bitnami/kubectl:1.22.0 imagePullPolicy: IfNotPresent tty: true volumeMounts: - mountPath: "/etc/localtime" name: "volume-2" readOnly: false - mountPath: "/var/run/docker.sock" name: "volume-docker" readOnly: false - mountPath: "/.kube/config" subPath: config name: "kubeconfig" readOnly: false volumes: - name: volume-maven-repo emptyDir: {} - name: volume-2 hostPath: path: "/usr/share/zoneinfo/Asia/Shanghai" - name: kubeconfig secret: secretName: kubeconfig items: - key: config path: config - name: volume-docker hostPath: path: "/var/run/docker.sock" """ } } stages { stage('Check Kubernetes ENV') { steps { container(name: 'kubectl', shell: 'sh') { sh 'kubectl get pod -A' } } } stage('构建镜像及检查kubernetes环境') { parallel { stage('构建镜像') { steps { withCredentials([usernamePassword(credentialsId: '${DOCKER_REPOSITORY_CREDENTIAL_ID}', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) { container(name: 'docker', shell: 'sh') { sh """ docker build -t ${HARBOR_HOST}/${NAMESPACE_NAME}/${REPOSITORY_NAME}:${TAG} . docker login ${HARBOR_HOST} --username=${USERNAME} --password=${PASSWORD} docker push ${HARBOR_HOST}/${NAMESPACE_NAME}/${REPOSITORY_NAME}:${TAG} """ } } } } } } stage('Check Kubernetes ENV (Again)') { steps { container(name: 'kubectl', shell: 'sh') { sh 'kubectl get pod -A' } } } } }