1. 程式人生 > 其它 >|NO.Z.00379|——————————|CloudNative|——|KuberNetes&CI/CD.V17|-----------------------------------------------------------|Jenkins.v05|Jenkinsfile流水線模板.v02|

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



[CloudNative:KuberNetes&CI/CD.V17]                                                                    [Applications.KuberNetes] [圖形化建立Jenkinsfile|k8s動態JenkinsSlave|建立Jenkinsfile|pipeline模板|建立Job|推送專案包到gitlab|]








一、生成的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是並行的








===============================END===============================


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



來自為知筆記(Wiz)