jenkins 动态slave配置4之pods模板

在这里插入图片描述
在这里插入图片描述

  • 开始添加一个maven模版

在这里插入图片描述

  • 再加一个docker容器模版

在这里插入图片描述

  • 最后对应jenkinsfile

在这里插入图片描述

pipeline {
     environment {
    name="miniprogram"
    image="xxx.jbjb.com/libary/zhihui-hr:${name}$BUILD_ID"
        }   
    agent { 
     kubernetes {
       inheritFrom 'maven-test'
     	 }
   			 }   
   			 
    stages {
      stage('Get Code') {
        steps {  
  checkout([$class: 'GitSCM', branches: [[name: '*/sit']], extensions: [], userRemoteConfigs: [[credentialsId: 'vip', url: 'https://gitlab.jbjb.com/wanghui/miniprogram.git']]])
         }
           }
           
        stage('Test-maven36....') {
            steps {       
                container('maven363'){
                     sh 'mvn -v'
                }
            }
        }   
        
      stage('Test-docker....') {
            steps {       
                container('docker'){
                     sh 'docker info'
                    //script{
                     //dockerlmage =  docker.build name + ":" + ${BUILD_ID}       
            }
               }
                   }
                       }
                          }

在这里插入图片描述

是maven项目记得配置存储哦

在这里插入图片描述

  • 以上Jenkins/CI配置完成


  • 以下CI/CD 配置

  • 配置jenkins可以远程操作K8S集群

  • 一.配置凭据
1.方式获取k8s密钥
cat .kube/config 
2.导出方式或者密钥
kubectl config view --raw > k8s-config-admin 
  • 添加一个k8s凭据

在这里插入图片描述

添加一个kubectl容器

在这里插入图片描述

  • jenkinsfile片段 namespac是指定哪一个命名空间启动pods

      stage('Deploy....') {
        steps {       
        container('kubectl'){
        withKubeConfig([credentialsId:'kj8s',namespace:'default']) {
            sh "sed -i 's#{{image}}#${image}#g' k8s.yaml"
            sh 'cat k8s.yaml'
            sh 'kubectl apply -f k8s.yaml'
                    }
      					}
    						}
    						}
  • KubeConfig更多配置参考链接

https://www.jenkins.io/doc/pipeline/steps/kubernetes-cli/
https://github.com/jenkinsci/kubernetes-cli-plugin/blob/master/README.md
完整pipeline
pipeline {
    
     environment {

    name="miniprogramnx"
    image="www.jbjb.com/libary/jr-prod-01:${name}$BUILD_ID"
        }
        
    agent { 
     kubernetes {
       inheritFrom 'maven-test'
      }
    }   

    stages {
      stage('Get Code') {
        steps {  
  checkout([$class: 'GitSCM', branches: [[name: '*/sit']], extensions: [], userRemoteConfigs: [[credentialsId: 'vip', url: 'https://git.jbjb.com/wanghui/miniprogram.git']]])
         }
           }

        stage('Node-build') {
            steps {       
                container('node'){
                    sh '''
                    npm install cnpm -g --registry=https://registry.npm.taobao.org
                    npm config set registry https://registry.npm.taobao.org
                    npm config get registry
                    cnpm install
                    cnpm run build:sit
                    '''     
                    
                }
            }
        }
      stage('Docker-build') {
            steps {       
                container('docker'){
                #docker登录凭据配置不再讲了
                    withCredentials([usernamePassword(credentialsId: 'dockeropa', passwordVariable: 'Password', usernameVariable: 'Username')]) {
                    sh ''' 
                    docker login -u$Username -p$Password ccr.ccs.tencentyun.com
                    docker build -t ${name}:${BUILD_ID} .
                    docker tag  ${name}:${BUILD_ID} ${image}
                    docker push ${image}
                    echo ${image}
                    docker rmi ${image}
                    docker rmi ${name}:${BUILD_ID}
                    echo ${name}:${BUILD_ID} 
                     '''
                    //script{
                     //dockerlmage =  docker.build name + ":" + ${BUILD_ID}
                }
            }
        }
      }
      
      stage('Deploy....') {
        steps {       
        container('kubectl'){
        withKubeConfig([credentialsId:'kj8s',namespace:'default']) {
            sh "sed -i 's#{{image}}#${image}#g' k8s.yaml"
            sh 'cat k8s.yaml'
            sh 'kubectl apply -f k8s.yaml'
        }
        
      }
        
    }
    
        }

            }
                }
  • k8s模版

cat <<END>k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: {{image}}
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: 2Gi
          requests:
            cpu: 100m
            memory: 0.5Gi

---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    #nodePort: 30019
  selector:
    app: nginx
END
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值