使用k8s插件Kubernetes Continuous Deploy
  • https://plugins.jenkins.io/kubernetes-cd/#documentation
  • pipeline添加kubernetesDeploy(enableConfigSubstitution: false, kubeconfigId: ‘kubeconfig’, configs: ‘deploy/*.yaml’)
pipeline {
   agent { label "baseline-front" }

   environment {
       GITLAB_ID = '8cb89727-c404-44e1-b3d7-e82dde2df432'
       GITLAB_URL = 'http://xxx.xxx.xxx/devops1/baseline_front.git'
       REGISTRY = 'https://core.harbor.haier'
       DOCKERHUB_NAMESPACE = 'core.harbor.haier/baseline'
       APP_NAME = 'baseline_frontend'
       VERSION = 'latest'
       BRANCH_NAME = 'dev'
   }

   stages {
       stage('拉取代码') {
           steps {
               echo '**************拉取代码****************'
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "$GITLAB_ID", url: "$GITLAB_URL"]]])
           }
       }

       stage('编译构建') {
           steps {
               container('front') {
                   script {
                       echo '***************编译构建****************'
                       sh 'npm ci'
                       sh 'CI=false npm run build'
                   }
               }
           }
       }

       stage('构建Docker镜像') {
           steps {
               container('front') {
                   script {
                       echo '*************构建Docker镜像**************'
                       sh 'docker login -u admin -p Harbor12345 $REGISTRY'
                       sh 'docker build -f deploy/Dockerfile -t $APP_NAME:$VERSION .'
                       sh 'docker tag $APP_NAME:$VERSION $DOCKERHUB_NAMESPACE/$APP_NAME:$VERSION'
                       sh 'docker push $DOCKERHUB_NAMESPACE/$APP_NAME:$VERSION'
                   }
               }
           }
       }

       stage('部署') {
           steps {
               echo '*************部署***************'
               kubernetesDeploy(enableConfigSubstitution: false, kubeconfigId: 'deploy_k8s', configs: 'deploy/*.yaml')
           }
       }


   }
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐