You need to enable JavaScript to run this app. Jenkins Git SCM插件在K8S容器节点检出失败求助
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Git SCM插件在K8S容器节点检出失败求助

Jenkins Kubernetes容器节点GitSCM checkout失败问题排查

问题描述

使用Jenkins Kubernetes插件,采用内置Git的自定义镜像,此前在EC2节点可正常工作。切换到K8S容器节点后,GitSCM checkout插件执行失败,但直接运行sh git clone...命令却能正常完成。疑惑问题根源是JENKINS-30600还是权限拒绝?

错误日志

Warning: JENKINS-30600: special launcher org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1@6c6a5bef; decorates RemoteLauncher[hudson.remoting.Channel@124fe7dc:JNLP4-connect connection from ip-10-20-119-149.us-west-2.compute.internal/10.20.119.149:53594] will be ignored (a typical symptom is the Git executable not being run inside a designated container)
15:02:29  Cloning the remote Git repository
15:02:29  ERROR: Error cloning remote repo 'origin'
15:02:29  hudson.plugins.git.GitException: Could not init /home/jenkins/agent/workspace/PR__test_container_main/infra-kit
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:1049)
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:802)
15:02:29    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:158)
15:02:29    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:151)
15:02:29    at hudson.remoting.UserRequest.perform(UserRequest.java:211)
15:02:29    at hudson.remoting.UserRequest.perform(UserRequest.java:54)
15:02:29    at hudson.remoting.Request$2.run(Request.java:377)
15:02:29    at hudson.remoting.InterceptingExecutorService.lambda$wrap$0(InterceptingExecutorService.java:78)
15:02:29    at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
15:02:29    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
15:02:29    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
15:02:29    at hudson.remoting.Engine$1.lambda$newThread$0(Engine.java:125)
15:02:29    at java.base/java.lang.Thread.run(Unknown Source)
15:02:29    Suppressed: hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from ip-10-20-119-149.us-west-2.compute.internal/10.20.119.149:53594
15:02:29        at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1784)
15:02:29        at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
15:02:29        at hudson.remoting.Channel.call(Channel.java:1000)
15:02:29        at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.execute(RemoteGitImpl.java:143)
15:02:29        at jdk.internal.reflect.GeneratedMethodAccessor745.invoke(Unknown Source)
15:02:29        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
15:02:29        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
15:02:29        at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.invoke(RemoteGitImpl.java:129)
15:02:29        at com.sun.proxy.$Proxy108.execute(Unknown Source)
15:02:29        at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1226)
15:02:29        at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1309)
15:02:29        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129)
15:02:29        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97)
15:02:29        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84)
15:02:29        at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
15:02:29        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
15:02:29        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
15:02:29        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
15:02:29        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
15:02:29        at java.base/java.lang.Thread.run(Thread.java:829)
15:02:29  Caused by: hudson.plugins.git.GitException: Command "/usr/bin/git init /home/jenkins/agent/workspace/test_container_main/infra-kit" returned status code 1:
15:02:29  stdout: 
15:02:29  stderr: /home/jenkins/agent/workspace/test_container_main/infra-kit/.git: Permission denied
15:02:29  
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2734)
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2660)
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2656)
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1981)
15:02:29    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:1047)
15:02:29    ... 12 more

问题分析

两个核心问题点:

  1. JENKINS-30600警告:明确说明GitSCM没有使用指定的容器执行器,而是在Jenkins agent的主容器中运行,这是插件兼容性或配置缺失导致的。
  2. 权限拒绝错误:因为GitSCM在错误的容器中执行,该容器对工作目录没有写入权限——工作目录可能属于自定义容器的挂载路径,主容器用户没有读写权限,或者两个容器的UID不一致。

直接sh git clone能正常运行,是因为sh步骤默认在你指定的自定义容器内执行,权限和环境都匹配。

解决方案

方法1:Pipeline中显式指定GitSCM在自定义容器内执行

将checkout scm步骤包裹在container块中,确保GitSCM使用指定的自定义容器。同时确保Kubernetes插件版本≥1.27,支持容器内SCM checkout:

pipeline {
  agent {
    kubernetes {
      yaml '''
        apiVersion: v1
        kind: Pod
        spec:
          containers:
          - name: custom-git-container
            image: 你的自定义镜像地址
            command: ["sleep", "999999"]
      '''
    }
  }
  stages {
    stage('代码拉取') {
      steps {
        container('custom-git-container') {
          checkout scm
        }
      }
    }
  }
}

方法2:全局配置强制SCM在容器内执行

进入Jenkins全局配置页面,找到Kubernetes插件设置项,勾选**"Run SCM checkout in container"**,指定默认容器名,这样所有SCM步骤都会在指定容器内执行,无需修改每个Pipeline。

方法3:修复工作目录权限(不推荐生产环境)

确保Jenkins agent主容器和自定义容器的用户UID一致,或者将工作目录权限设置为777(允许所有用户读写)。这种方法只是临时 workaround,无法从根本解决容器执行器的问题。

总结

问题本质是JENKINS-30600导致GitSCM未在自定义容器内执行,进而引发权限拒绝。核心解决思路是让GitSCM步骤在指定的自定义容器中运行,同时保证容器对工作目录有正确的读写权限。

内容的提问来源于stack exchange,提问作者EilonA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 19:12:10