Jenkins:连接到 JetBrains Space 上的 Git 存储库时“主机密钥验证失败”

问题描述 投票:0回答:1

我正在尝试设置一个 Jenkins 管道,使用 SSH 从 JetBrains Space 克隆存储库。但是,我不断遇到错误“主机密钥验证失败”。这是我的 Jenkinsfile 的相关部分:

    pipeline {
    agent any

    environment {
        DOCKER_BUILDKIT = "1"
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    withCredentials([sshUserPrivateKey(credentialsId: 'space-ssh-key', keyFileVariable: 'SSH_PRIVATE_KEY')]) {
                        sh '''
                            mkdir -p ~/.ssh
                            echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
                            chmod 600 ~/.ssh/id_rsa
                            git clone ssh://[email protected]/my-proj/main/my-rpoj.git .
                        '''
                    }
                }
            }
        }
        stage('Build Docker Images') {
            steps {
                script {
                    sh 'chmod +x ./build/build-docker-images.kts'
                    sh './build/build-docker-images.kts'
                }
            }
        }
        stage('Deploy Application') {
            steps {
                script {
                    sshagent(['space-ssh-key']) {
                        sh '''
                            scp -r . ${env.DEPLOY_USER}@${env.DEPLOY_SERVER}:/var/www/app
                            ssh ${env.DEPLOY_USER}@${env.DEPLOY_SERVER} << EOF
                              cd /var/www/app
                              docker-compose down
                              docker-compose up -d
                            EOF
                        '''
                    }
                }
            }
        }
    }
}

尽管使用 ssh-keyscan 添加主机密钥,我仍然收到以下错误:

Failed to connect to repository: Command "git ls-remote -h -- ssh://[email protected]/myproj-dev/main/myproj.git HEAD" returned status code 128:
stdout:
stderr: No RSA host key is known for git.jetbrains.space 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.

我已经证实:

Jenkins 中的 SSH 私钥已正确设置。 主机密钥将添加到 ~/.ssh/known_hosts 中。 当我在 Jenkins 服务器上手动运行命令时,它们工作正常。这是我到目前为止所尝试过的:

手动将主机密钥添加到 ~/.ssh/known_hosts。 在 Jenkinsfile 中包含 ssh-keyscan 步骤以添加主机密钥。 我缺少什么?如何确保Jenkins中的主机密钥验证通过? enter image description here

jenkins ssh jenkins-pipeline rsa jetbrains-space-automation
1个回答
0
投票

Jenkins 不提供 git 主机密钥验证的每个作业设置,您只能在管理 Jenkins -> 安全性 -> Git 主机密钥验证配置中全局更改它。将其设置为

Accept first connection

一些你没有要求的建议:

  1. 检查

    ~/.ssh/known_hosts
    的密钥是不明确的,因为
    ~
    在不同的用户下评估为不同的值。检查 Jenkins 运行的用户。您可能以其他用户身份登录,对吧?

  2. ssh-keyscan
    在管道中有点太晚了,因为 Jenkins 需要连接到 git 服务器来检索你的管道。

  3. SSH_PRIVATE_KEY
    保存临时文件名,而不是键值。所以
    echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    不是您想要的,您正在寻找
    cat "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    cp "$SSH_PRIVATE_KEY" ~/.ssh/id_rsa

  4. 很少需要使用

    sh
    进行手动签出,隐式签出在您的管道中完全没问题。

© www.soinside.com 2019 - 2024. All rights reserved.