使用k8s部署jenkins并制作从Pod执行任务,如何将pod中生成的文件或安装包放入jenkins pvc

ifmq2ha2  于 2023-02-03  发布在  Jenkins
关注(0)|答案(1)|浏览(179)

我部署了jenkins,并使用slave pod来运行它,我使用openebs的本地pv模式,将jenkins和volume部署在同一个节点上,我使用volume模式来传输pod生成的数据,它共享在主机的jenkins卷中,我的任务是下载代码并添加一些安装包到其中,但是下载包需要很长时间,我希望Slave Pod不会每次部署都被下载。

#!/usr/bin/env groovy
// groovy公共变量
def PROJECT = "CI-code"
def WORKDIR_PATH = "/opt/status"
def DOWNLOAD_KUBE_DOWNLOAD_URL = "xxx/kube-1.19.0-v2.2.0-amd64.tar.gz"
def PVC_PATH = "/var/openebs/local/pvc-8e8f9830-9bdc-494d-ac45-19310cbda035/cloudybase"

pipeline {
   agent {
   kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  name: jenkins-slave
  namespace: devops-tools
spec:
  containers:
  - name: jnlp
    image: "xxx/google_containers/jenkins-slave-jdk11-wget:latest"
    imagePullPolicy: Always
    securityContext:
      privileged: true
      runAsUser: 0
    volumeMounts:
      - name: docker-cmd
        mountPath: /usr/bin/docker
      - name: docker-sock
        mountPath: /var/run/docker.sock
      - name: code
        mountPath: /home/jenkins/agent/workspace/${PROJECT}
  volumes:
    - name: docker-cmd
      hostPath:
        path: /usr/bin/docker
    - name: docker-sock
      hostPath:
        path: /var/run/docker.sock
    - name: code
      hostPath:
        path: ${PVC_PATH}
"""
    }
  }

   stages {
      stage('拉取代码') {
         steps {
            git branch: 'release-2.2.0', credentialsId: 'e17ba069-aa8b-4bfd-9c9e-3f3956914f09', url: 'xxx/deployworker.git'
         }
      }

   stage('下载依赖包到组件包对应目录') {
      steps {
            sh """
               logging() {
                 echo -e "\033[32m $(/bin/date)\033[0m" - $@
                }
             
               main () {
                 logging Check ${DOWNLOAD_KUBE_DOWNLOAD_URL} installation package if download ...
                    if [ ! -f "${PVC_PATH}/kube_status_code" ];then
                     download_kube
                    fi
                }

                download_kube () {
                DOWNLOAD_KUBE_NAME=$(echo ${DOWNLOAD_KUBE_DOWNLOAD_URL} | /bin/sed 's|.*/||')
                
                cd / && { /bin/curl -O ${DOWNLOAD_KUBE_DOWNLOAD_URL} ; cd -; }
                if [ "$?" -ne "0" ]; then
                  echo "Failed"
                  exit 1
                fi
 
                echo 'true' > ${W_PATH}/kube_status_code
                mkdir -p /home/jenkins/agent/workspace/deploywork/Middleware-choreography/kubeQ/kubeQ/
                tar xf /\${DOWNLOAD_KUBE_NAME} -C /home/jenkins/agent/workspace/deploywork/Middleware-choreography/kubeQ/kubeQ/

                if [ "$?" -ne "0" ]; then
                  echo "Failed"
                  exit 1
                fi
                }
            """
         }
      }
8nuwlpux

8nuwlpux1#

Hi zccharts从上面的解释中我可以看出你每次都在尝试从头开始创建你的容器,这很耗时。这个问题可以通过创建你的基本容器映像来解决,你可以在你的管道中使用它来部署你的应用程序。

相关问题