pipeline { agent any tools { //工具必须预先在jenkins中预配置 maven 'mvn' jdk 'jdk' } stages { stage('Env') { steps { sh 'printenv' } } stage('git') { steps{ checkout([$class: 'GitSCM', branches: [[name: '*/develop']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '18980d0a-1ae4-443b17dc687b1', url: 'https://CFAI_the_best/codeflag-basis.git']]]) } } stage('mvn install') { steps { sh 'cd /var/lib/jenkins/workspace/k8s-test-basis/src/main/resources && mv application.test.properties application.properties' sh 'mvn clean install' } } stage('docker') { steps { sh 'cd /var/lib/jenkins/workspace/k8s-test-basis/target ' sh ''' REPOSITORY=172.18.0.218/codeflagtest/codeflag-basis:${BUILD_ID} pwd echo ' FROM 172.18.0.218/codeflagtest/tomcat RUN rm -rf /usr/local/tomcat/webapps/ROOT RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone ADD target/basic-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war ' > Dockerfile docker build -t $REPOSITORY . docker login 172.18.0.218 -u admin -p Harbor123456 docker push $REPOSITORY ''' } } stage('deploy') { steps { sh ' REPOSITORY=172.18.0.218/codeflagtest/codeflag-basis:${BUILD_ID}' sh 'kubectl get pod' sh ''' cat << EOF > basis.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: basis spec: replicas: 2 selector: matchLabels: app: basis template: metadata: labels: app: basis spec: containers: - name: basis image: 172.18.0.218/codeflagtest/codeflag-basis:${BUILD_ID} imagePullPolicy: Always ports: - containerPort: 8080 volumeMounts: - name: pvc-nas mountPath: "/home/www/test" volumes: - name: pvc-nas persistentVolumeClaim: claimName: pvc-nas imagePullSecrets: - name: registry-secret --- apiVersion: v1 kind: Service metadata: name: basis spec: ports: - port: 80 targetPort: 8080 name: basis selector: app: basis type: LoadBalancer EOF kubectl apply -f basis.yaml ''' } } } }