jenkins流水线-k8s-java后端pod

// 项目 --please change
def app_env = "demo"
def app_pro = "jbb"
def app_port = "8083"
def app_name = "guarantee-backstage" //与git里面对应
def git_address = "git地址"
def git_branch = "master"

// 公共 --no change
def k8s_namespace = "demo"
def registry = "h.zlr.cn"
def reg_project = "demoproject"

//      --no change
def dep_name = "${app_env}-${app_pro}-${app_name}"
def image_name = "${registry}/${reg_project}/${dep_name}:v1"

// 认证 --no change
def pull_secret = "registry-pull-secret"
def git_auth = "xxxxx"
def k8s_auth = "xxxxx"


podTemplate(label: 'jnlp-slave', cloud: 'kubernetes', containers: [
  containerTemplate(
    name: 'jnlp', 
    image: 'h.zlr.cn/ops/jenkins-slave:v1', 
    alwaysPullImage: true 
  ),
  ],
  volumes: [
    hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
    hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'),
    hostPathVolume(mountPath: '/etc/hosts', hostPath: '/etc/hosts'),
  ],
  imagePullSecrets: ["${pull_secret}"],
  ) 
{
  node("jnlp-slave"){
      stage('Git Checkout'){
          checkout([$class: 'GitSCM', branches: [[name: "*/${git_branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
      }

      stage('Maven 构建'){
            sh 'mvn clean package -e -U -DskipTests=true'
      }
      stage('生成dockerfile'){
            sh """cat > Dockerfile << EOF
FROM h.zlr.cn/ops/java-app:v1
MAINTAINER linuxsamu
COPY ${app_name}-assemble/target/*.jar /data/
RUN rm -rf /data/*sources.jar && mv /data/*.jar /data/app.jar
WORKDIR /data
ENTRYPOINT ["java", "-jar","-Dspring.profiles.active=${app_env}" , "app.jar"]
EOF
          """
      }
      stage('构建并且上传镜像'){
          sh """
          docker login '${registry}' -u admin -p admin 
          docker build -t '${image_name}' .
          docker push '${image_name}'
          """
        }
      stage('生成k8s yaml文件'){
            sh """cat > deploy.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ${dep_name}
  namespace: ${k8s_namespace}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ${dep_name}
  template:
    metadata:
      labels:
        app: ${dep_name}
    spec:
      containers:
      - name: ${dep_name}
        image: ${image_name}
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: ${app_port}
        resources:
          limits:
            cpu: "1"
            memory: 512Mi
          requests:
            cpu: "0.5"
            memory: 256Mi
      imagePullSecrets:
      - name: ${pull_secret}
---
apiVersion: v1
kind: Service
metadata:
  name: ${dep_name}
  namespace: ${k8s_namespace}
spec:
  type: NodePort
  ports:
  - port: ${app_port}
    targetPort: ${app_port}
    protocol: TCP
  selector:
    app: ${dep_name}
EOF
          """
      }     

      stage('部署到k8s容器云'){
          kubernetesDeploy configs: 'deploy.yaml', kubeConfig: [path: ''], kubeconfigId: "${k8s_auth}", secretName: '', ssh: [sshCredentialsId: '*', sshServer: ''], textCredentials: [certificateAuthorityData: '', clientCertificateData: '', clientKeyData: '', serverUrl: 'https://']
      }
    }
}