k8s中jenkins流水线配置记录
stage表示流水线步骤,在其中编写具体流程,大致为拉代码、制镜像推镜像、发布
构建参数可以在流水线外设置(k8s界面)

pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  stages {
    stage('check scm') {
      agent none
      steps {
        git(url: 'http://gitUrl.git', credentialsId: 'gitlab', branch: 'dev', changelog: true, poll: false)
      }
    }

    stage('build and push') {
      agent none
      steps {
        container('maven') {
          sh '''cd your-project

mvn clean package --settings configuration/settings.xml  -Dmaven.test.skip=true'''
          sh 'docker build -f your-project/Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER .'
          withCredentials([usernamePassword(credentialsId : 'harbor' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
            sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
          }

          sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER'
        }

      }
    }

    stage('artifacts') {
      agent none
      steps {
        archiveArtifacts 'your-project/target/*.jar'
      }
    }

    stage('Deploy') {
      agent none
      steps {
        container('maven') {
          withCredentials([kubeconfigContent(credentialsId : 'kubeconfig' ,variable : 'KUBECONFIG_CONTENT' ,)]) {
            sh '''mkdir ~/.kube

echo "$KUBECONFIG_CONTENT" > ~/.kube/config

envsubst < your-project/deploy/dev-ol/devops-sample-svc.yaml | kubectl apply -f -

envsubst < your-project/deploy/dev-ol/devops-sample.yaml | kubectl apply -f -'''
          }

        }

      }
    }

  }
}