Loading

记录jenkins容器重新拉起后代码拉取失败

记录jenkins容器重新拉起后代码拉取失败

stage('Add Host Key') {
    steps {
        sh '''
            mkdir -p ~/.ssh
            chmod 700 ~/.ssh
            ssh-keyscan gitee.com >> ~/.ssh/known_hosts
            chmod 644 ~/.ssh/known_hosts
        '''
    }
}

stage('代码检出') {
    steps {
        checkout([
            $class: 'GitSCM',
            branches: [[name: '*/prod']],
            extensions: [[
                $class: 'CloneOption',
                depth: 0,
                noTags: false,          // 关键:获取标签
            ]],
            userRemoteConfigs: [[
                credentialsId: 'gitee-ssh-key',
                url: 'git@gitee.com:zhpj07/plangz.git'
            ]]
        ])
        
        // 同步远程标签
        sshagent(credentials: ['gitee-ssh-key']) {
            sh "git fetch --tags --force"
        }
    }
}

构建日志:

[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Add Host Key)
[Pipeline] sh
+ mkdir -p /root/.ssh
+ chmod 700 /root/.ssh
+ ssh-keyscan gitee.com
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
+ chmod 644 /root/.ssh/known_hosts
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (代码检出)
[Pipeline] checkout
The recommended git tool is: NONE
using credential gitee-ssh-key
Warning: JENKINS-30600: special launcher org.jenkinsci.plugins.docker.workflow.WithContainerStep$Decorator$1@1f1e0125; decorates hudson.Launcher$LocalLauncher@1b21861e will be ignored (a typical symptom is the Git executable not being run inside a designated container)
Cloning the remote Git repository
Cloning repository git@gitee.com:zhpj07/plangz.git
 > git init /var/jenkins_home/workspace/plangz # timeout=10
Fetching upstream changes from git@gitee.com:zhpj07/plangz.git
 > git --version # timeout=10
 > git --version # 'git version 2.39.5'
using GIT_SSH to set credentials 
Verifying host key using known hosts file
 > git fetch --tags --force --progress -- git@gitee.com:zhpj07/plangz.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url git@gitee.com:zhpj07/plangz.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
 > git rev-parse refs/remotes/origin/prod^{commit} # timeout=10
Checking out Revision 4d1988c7efe3dbecc1096c75a730e44a85aa5697 (refs/remotes/origin/prod)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 4d1988c7efe3dbecc1096c75a730e44a85aa5697 # timeout=10
Commit message: "gateway服务构建速度优化2"
 > git rev-list --no-walk 4d1988c7efe3dbecc1096c75a730e44a85aa5697 # timeout=10
[Pipeline] sshagent
[ssh-agent] Using credentials zhpj07
$ docker exec e7e55efb1f14c74001e76f4896fc9125e14ce76b996b05524fc97c8cb960185c ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-bemBe7YAoZKA/agent.62
SSH_AGENT_PID=68
Running ssh-add (command line suppressed)
Identity added: /var/jenkins_home/workspace/plangz@tmp/private_key_13347194825979235967.key (zpj1750@163.com)
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
+ git fetch --tags --force
[Pipeline] }
$ docker exec --env ******** --env ******** e7e55efb1f14c74001e76f4896fc9125e14ce76b996b05524fc97c8cb960185c ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 68 killed;
[ssh-agent] Stopped.
[Pipeline] // sshagent
[Pipeline] }

调整了 docker-compose.yaml 中挂载的目录,重新拉起之后,再构建时报错:

[Pipeline] stage
[Pipeline] { (Add Host Key)
[Pipeline] sh
+ mkdir -p /root/.ssh
+ chmod 700 /root/.ssh
+ ssh-keyscan gitee.com
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
# gitee.com:22 SSH-2.0-ssh-pilot-1.8.4
+ chmod 644 /root/.ssh/known_hosts
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (代码检出)
[Pipeline] checkout
The recommended git tool is: NONE
using credential gitee-ssh-key
Warning: JENKINS-30600: special launcher org.jenkinsci.plugins.docker.workflow.WithContainerStep$Decorator$1@bb3908f; decorates hudson.Launcher$LocalLauncher@79a30d62 will be ignored (a typical symptom is the Git executable not being run inside a designated container)
Cloning the remote Git repository
Cloning repository git@gitee.com:zhpj07/plangz.git
 > git init /var/jenkins_home/workspace/plangz # timeout=10
Fetching upstream changes from git@gitee.com:zhpj07/plangz.git
 > git --version # timeout=10
 > git --version # 'git version 2.39.5'
using GIT_SSH to set credentials 
Verifying host key using known hosts file
 > git fetch --tags --force --progress -- git@gitee.com:zhpj07/plangz.git +refs/heads/*:refs/remotes/origin/* # timeout=10
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- git@gitee.com:zhpj07/plangz.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: No ED25519 host key is known for gitee.com and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Add Host Key​步骤中的路径改为使用绝对路径:

stage('Add Host Key') {
    steps {
        sh '''
            mkdir -p /root/.ssh
            chmod 700 /root/.ssh
            ssh-keyscan gitee.com >> /root/.ssh/known_hosts
            chmod 644 /root/.ssh/known_hosts
        '''
    }
}

posted @ 2025-06-05 20:10  zhpj  阅读(37)  评论(0)    收藏  举报