diff --git a/Jenkinsfile b/Jenkinsfile index a3b8da3..279efed 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,6 +5,8 @@ pipeline { NAMESPACE_NAME = "newrain_wang" REPOSITORY_NAME = "my-web-app" TAG = "v1.0" + REPLICAS = 4 + YAML_PATH = "k8s" } agent { kubernetes { @@ -22,6 +24,22 @@ spec: - mountPath: "/etc/localtime" name: "volume-2" readOnly: false + - name: kubectl + image: bitnami/kubectl:1.22.0 + imagePullPolicy: IfNotPresent + tty: true + command: ["cat"] + 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 - name: docker image: docker:19.03.15-git command: ['cat'] @@ -30,8 +48,6 @@ spec: - mountPath: "/var/run/docker.sock" name: "volume-docker" readOnly: false - - mountPath: "/etc/docker/daemon.json" - name: "volume-docker-daemon" volumes: - name: volume-2 hostPath: @@ -39,9 +55,6 @@ spec: - name: volume-docker hostPath: path: "/var/run/docker.sock" - - name: volume-docker-daemon - hostPath: - path: "/etc/docker/daemon.json" """ } } @@ -51,13 +64,24 @@ spec: withCredentials([usernamePassword(credentialsId: env.DOCKER_REPOSITORY_CREDENTIAL_ID, passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) { container('docker') { sh """ - docker build -t ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.REPOSITORY_NAME}:${env.TAG} . - docker login ${env.HARBOR_HOST} --username ${env.USERNAME} --password ${env.PASSWORD} - docker push ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.REPOSITORY_NAME}:${env.TAG} + docker build -t ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.REPOSITORY_NAME}:${env.TAG} . + docker login ${env.HARBOR_HOST} --username ${env.USERNAME} --password ${env.PASSWORD} + docker push ${env.HARBOR_HOST}/${env.NAMESPACE_NAME}/${env.REPOSITORY_NAME}:${env.TAG} """ } } } } + stage('Check Kubernetes ENV') { + steps { + container(name: 'kubectl', shell: 'echo') { + sh """ + sed -i "s/REPLICAS/${env.REPLICAS}/;s/HARBOR_HOST/${env.HARBOR_HOST}/;s/NAMESPACE_NAME/${env.NAMESPACE_NAME}/;s/REPOSITORY_NAME/${env.REPOSITORY_NAME}/;s/TAG/${env.TAG}/" ${YAML_PATH} + kubectl apply -f ${YAML_PATH} + """ + } + + } + } } }