|NO.Z.00379|——————————|CloudNative|——|KuberNetes&CI/CD.V17|——|Jenkins.v05|Jenkinsfile流水线模板.v02|

一、生成的pipeline:Jenkinsfile流水线模板
### --- 通过git查看生成的pipeline:Jenkinsfile文件

pipeline {
  agent {
    kubernetes {
      cloud 'kubernetes-default'
      slaveConnectTimeout 1200
      yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
    - args: [\'$(JENKINS_SECRET)\', \'$(JENKINS_NAME)\']
      image: 'registry.cn-beijing.aliyuncs.com/citools/jnlp:alpine'
      name: jnlp
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "volume-2"
          readOnly: false
        - mountPath: "/etc/hosts"
          name: "volume-hosts"
          readOnly: false        
    - command:
        - "cat"
      env:
        - name: "LANGUAGE"
          value: "en_US:en"
        - name: "LC_ALL"
          value: "en_US.UTF-8"
        - name: "LANG"
          value: "en_US.UTF-8"
      image: "registry.cn-beijing.aliyuncs.com/citools/maven:3.5.3"
      imagePullPolicy: "IfNotPresent"
      name: "build"
      tty: true
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "volume-2"
          readOnly: false
        - mountPath: "/root/.m2/"
          name: "volume-maven-repo"
          readOnly: false
        - mountPath: "/etc/hosts"
          name: "volume-hosts"
          readOnly: false
    - command:
        - "cat"
      env:
        - name: "LANGUAGE"
          value: "en_US:en"
        - name: "LC_ALL"
          value: "en_US.UTF-8"
        - name: "LANG"
          value: "en_US.UTF-8"
      image: "registry.cn-beijing.aliyuncs.com/citools/kubectl:self-1.17"
      imagePullPolicy: "IfNotPresent"
      name: "kubectl"
      tty: true
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "volume-2"
          readOnly: false
        - mountPath: "/var/run/docker.sock"
          name: "volume-docker"
          readOnly: false
        - mountPath: "/mnt/.kube/"
          name: "volume-kubeconfig"
          readOnly: false
        - mountPath: "/etc/hosts"
          name: "volume-hosts"
          readOnly: false
    - command:
        - "cat"
      env:
        - name: "LANGUAGE"
          value: "en_US:en"
        - name: "LC_ALL"
          value: "en_US.UTF-8"
        - name: "LANG"
          value: "en_US.UTF-8"
      image: "registry.cn-beijing.aliyuncs.com/citools/docker:19.03.9-git"
      imagePullPolicy: "IfNotPresent"
      name: "docker"
      tty: true
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "volume-2"
          readOnly: false
        - mountPath: "/var/run/docker.sock"
          name: "volume-docker"
          readOnly: false
        - mountPath: "/etc/hosts"
          name: "volume-hosts"
          readOnly: false
  restartPolicy: "Never"
  nodeSelector:
    build: "true"
  securityContext: {}
  volumes:
    - hostPath:
        path: "/var/run/docker.sock"
      name: "volume-docker"
    - hostPath:
        path: "/usr/share/zoneinfo/Asia/Shanghai"
      name: "volume-2"
    - hostPath:
        path: "/etc/hosts"
      name: "volume-hosts"
    - name: "volume-maven-repo"
      hostPath:
        path: "/opt/m2"
    - name: "volume-kubeconfig"
      secret:
        secretName: "multi-kube-config"
''' 
}
}

  stages {
    stage('pulling Code') {
      parallel {
        stage('pulling Code') {
          when {
            expression {
              env.gitlabBranch == null
            }
          }
          steps {
            git(branch: "${BRANCH}", credentialsId: '7a1ce79d-acbe-443c-b500-0971c05b9cdf', url: "${REPO_URL}")
          }
        }

        stage('pulling Code by trigger') {
          when {
            expression {
              env.gitlabBranch != null
            }
          }
          steps {
            git(url: "${REPO_URL}", branch: env.gitlabBranch, credentialsId: '7a1ce79d-acbe-443c-b500-0971c05b9cdf')
          }
        }

      }
    }

    stage('initConfiguration') {
      steps {
        script {
          CommitID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
          CommitMessage = sh(returnStdout: true, script: "git log -1 --pretty=format:'%h : %an  %s'").trim()
          def curDate = sh(script: "date '+%Y%m%d-%H%M%S'", returnStdout: true).trim()
          TAG = curDate[0..14] + "-" + CommitID + "-" + BRANCH
        }

      }
    }

    stage('Building') {
      parallel {
        stage('Building') {
          steps {
            container(name: 'build') {
            sh """
            echo "Building Project..."
            ${BUILD_COMMAND}
          """
            }

          }
        }

        stage('Scan Code') {
          steps {
            sh 'echo "Scan Code"'
          }
        }

      }
    }

    stage('Build image') {
      steps {
                withCredentials([usernamePassword(credentialsId: 'REGISTRY_USER', passwordVariable: 'Password', usernameVariable: 'Username')]) {
        container(name: 'docker') {
          sh """
          docker build -t ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} .
          docker login -u ${Username} -p ${Password} ${HARBOR_ADDRESS}
          docker push ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG}
          """
        }
        }

      }
    }

    stage('Deploy') {
    when {
            expression {
              DEPLOY != "false"
            }
          }
    
      steps {
      container(name: 'kubectl') {
        sh """
        cat ${KUBECONFIG_PATH} > /tmp/1.yaml
  /usr/local/bin/kubectl config use-context ${CLUSTER} --kubeconfig=/tmp/1.yaml
  export KUBECONFIG=/tmp/1.yaml
  /usr/local/bin/kubectl set image ${DEPLOY_TYPE} -l ${DEPLOY_LABEL} ${CONTAINER_NAME}=${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} -n ${NAMESPACE}
"""
        }

      }
    }

  }
  environment {
    CommitID = ''
    CommitMessage = ''
    TAG = ''
  }
}
二、pipeline:Jenkinsfile注释说明
### --- 创建pod模板

pipeline {
  agent {
    kubernetes {                                                            //agent声明使用kubernetes
      cloud 'kubernetes-default'                                            //指定使用哪个k8s进行编译操作,执行的时候会在哪里配置k8s集群
      slaveConnectTimeout 1200                                              //slave的超时时间,按需更改即可
      yaml '''                                                              //yaml指定它的pod文件
      image: 'registry.cn-beijing.aliyuncs.com/citools/jnlp:alpine'         //镜像地址为阿里云仓库
      name: jnlp                                                            //jnlp和master进行通讯的
      image: "registry.cn-beijing.aliyuncs.com/citools/maven:3.5.3"         //启动一个maven镜像,是用来进行编译的,在编译的时候会使用maveinstall进行使用的
      name: "build"                                                         //镜像的名称是build
        - mountPath: "/root/.m2/"                                           //持久化,挂载到.2,maven插件默认是挂载到/root/.m2的目录下,存放放编译的时候插件的位置
          name: "volume-maven-repo"                                         //volume名称
      image: "registry.cn-beijing.aliyuncs.com/citools/kubectl:1.17.4"      //kubectl,编译的时候会使用到set命令,可以使用kubectl执行set命令即可
          name: "volume-kubeconfig"                                         //kubectl的kubeconfig文件挂载进去,可以使用单个kubeconfig文件去管理多个集群,创建多集群的kubeconfig
      image: "registry.cn-beijing.aliyuncs.com/citools/docker:19.03.9-git"  //在docker中build镜像,在push到镜像仓库,需要使用docker客户端,在docker中push镜像时,需要使用demo进程才可以push镜像
        - mountPath: "/var/run/docker.sock"                                 //使用了宿主机的sock文件挂载到容器上,直接push镜像
  volumes:                                                                  //创建的一些volume
        path: "/var/run/docker.sock"                                        //sock文件,使用sock文件进行编译操作,构建操作和pod操作
        path: "/usr/share/zoneinfo/Asia/Shanghai"                           //挂载了一个时间
    - name: "volume-maven-repo"                                             //挂载了maven依赖包
      emptyDir: {}                                                          //直接使用的是空目录,若是有后端存储可以更改为后端存储,或者挂载nfs也是可以的
        secretName: "multi-kube-config"                                     //挂载了一个secret,实验时可以创建一个secret,挂载到pod中,就可以直接使用了
### --- 通过pipeline生成的流水线的框架
  stages {
    stage('pulling Code') {                                                 //拉取代码
      parallel {                                                            //parallel是并行拉取代码,执行的时候只需要执行一个,使用when参数来控制
          when {                                                            //设置一个when,符合条件的情况下才会执行
        stage('pulling Code by trigger') {                                  //手动触发
    stage('initConfiguration') {                                            //生成镜像的tag
    stage('Building') {                                                     //镜像构建
      parallel {                                                            //和下面的stage是并行的

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(71)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示