You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins共享库无法在K8s Pod/容器内执行的问题排查

Jenkins共享库方法无法在K8s Pod指定容器内执行的问题

我编写的Jenkins共享库包含文件夹创建、稀疏检出等功能,在Docker代理环境下测试运行正常,但在K8s多容器环境中运行流水线时,共享库的方法并未在指定的container-1内执行。

流水线代码

@Library('devopsdb-global-lib') _

import devopsdb.utilities.Utilities
def obj_Utilities = new Utilities(this)                        

pipeline {
    agent {
        kubernetes {
            yaml """
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            some-label: "pod-template-${env.BUILD_ID}"
        spec:
          containers:
          - name: container-1
            image: registry.devops-db.internal:5000/img-jenkins-devopsdb:2.0
            env:
            - name: CONTAINER_NAME
              value: "container-1"
            volumeMounts:
            - name: shared-volume
              mountPath: /mnt
            command:
            - cat
            tty: true
          - name: container-2
            image: ubuntu:22.04
            env:
            - name: CONTAINER_NAME
              value: "container-2"
            volumeMounts:
            - name: shared-volume
              mountPath: /mnt
            command:
            - cat
            tty: true
          volumes:
          - name: shared-volume
            emptyDir: {}            
        """
        }
    }
    options { timestamps ()
        skipDefaultCheckout(true)
    }
    stages {
        stage('Environment') {
            steps {
                container('container-1') {
                    script {
                        sh 'echo $CONTAINER_NAME > /mnt/shared_volume.txt'
                        def str_folder = "/mnt/pipelines/resources"
                        def str_folderCheckout = "/pip"
                        obj_Utilities.CreateFolders(str_folder)
                        sh 'sleep 10m'
                        obj_Utilities.SparseCheckout('git@gitlab.devops-db.internal:infrastructure/pipelines/resources.git',
                                'master',
                                str_folderCheckout,
                                'usr-service-jenkins',
                                str_folder)
                        sh 'sleep 1s'
                        sh "PIP_CONFIG_FILE=${str_folder}${str_folderCheckout}/pip-devops.conf pip install devopsdb"
                    }
                }
            }
        }
    } 
}

容器检查结果

进入container-1检查,发现sh命令创建的/mnt/shared_volume.txt文件存在,但共享库CreateFolders方法应创建的/mnt/pipelines/resources文件夹并未生成:

kubectl get pods --namespace jenkins-devopsdb
NAME                                                              READY   STATUS    RESTARTS     AGE
infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d   3/3     Running   0            61s

kubectl get pods infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d --namespace jenkins-devopsdb -o="custom-columns=NAME:.metadata.name,INIT-CONTAINERS:.spec.initContainers[*].name,CONTAINERS:.spec.containers[*].name"
NAME                                                          INIT-CONTAINERS   CONTAINERS
infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d   <none>            container-1,container-2,jnlp

kubectl exec -i -t infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d --container container-1 --namespace jenkins-devopsdb -- /bin/bash
root@infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d:/pipeline# ls -lah /mnt/
total 12K
drwxrwxrwx 2 root root 4.0K Jun 26 10:24 .
drwxr-xr-x 1 root root 4.0K Jun 26 10:24 ..
-rw-r--r-- 1 root root   12 Jun 26 10:24 shared_volume.txt

后续错误日志

继续执行流水线会触发权限拒绝错误,导致稀疏检出失败:

11:39:36  ERROR: Checkout failed
11:39:36  Also:   hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from 172.21.5.156/172.21.5.156:26427
11:39:36  at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1787)
11:39:36  at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
11:39:36  at hudson.remoting.Channel.call(Channel.java:1003)
11:39:36  at hudson.FilePath.act(FilePath.java:1226)
11:39:36  at hudson.FilePath.act(FilePath.java:1215)
11:39:36  at hudson.FilePath.mkdirs(FilePath.java:1406)
11:39:36  at hudson.plugins.git.GitSCM.createClient(GitSCM.java:845)
11:39:36  at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1294)
11:39:36  at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:136)
11:39:36  at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:101)
11:39:36  at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:88)
11:39:36  at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
11:39:36  at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
11:39:36  at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
11:39:36  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
11:39:36  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
11:39:36  at java.base/java.lang.Thread.run(Thread.java:840)
11:39:36  java.nio.file.AccessDeniedException: /mnt/pipelines

核心问题

问题并非文件夹创建本身(sh命令可正常创建文件),而是共享库方法未在指定的container-1内运行,导致无法完成稀疏检出等后续操作。

内容的提问来源于stack exchange,提问作者devops-db

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:57:05