欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jenkins 从 git到k8s

程序员文章站 2022-07-14 09:27:57
...

 

pipeline {
    agent any
    environment {
      
       GIT_URL = "https://github.com/LiuYuDamon/eureka.git"
      GIT_CRED = "XXXXXXX-31f8-4cee-a4ed-XXXXXXXXXX"
      DOCKER_REPO="registry.cn-shanghai.aliyuncs.com/yuanbing/sba-gateway"
      DOCKER_REG="https://registry.cn-shanghai.aliyuncs.com"
      DOCKER_REG_KEY = "XXXXXXXX-6135-41d1-902c-XXXXXXXXXXX"
      dockerImage = ''
      
    }
    stages {
    
       stage('CheckOut Code'){
           steps{
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: GIT_URL]]])
               }
              }
        stage('Maven Build'){
           steps{
               sh 'mvn clean install -DskipTests'
           }

        }
        
        stage('Building image') {
         steps{
           script {
              docker.withRegistry( DOCKER_REG, DOCKER_REG_KEY ) {dockerImage = docker.build DOCKER_REPO + ":$BUILD_NUMBER"
              }
           }
         }
       }
       stage('Push Image') {
      steps{
        script {
         docker.withRegistry( DOCKER_REG, DOCKER_REG_KEY ) {
                  dockerImage.push()
                }
              }
            }
      }
      
      stage('Deploy Image to K8s') {
      steps{
        script {
           sh "sed -i 's/{version}/" + BUILD_NUMBER + "/g' deployment.yaml"
             sh 'kubectl apply -f deployment.yaml'
            }
      }
      }
      
      
      stage('Remove Unused docker image') {
      steps{
        sh "docker rmi $DOCKER_REPO:$BUILD_NUMBER"
      }
    }
   }
  

}