下午好,我遇到了无法通过 sshagent 使用 git 命令的问题。我的想法是,我将更新上传到 GitHub,触发一个钩子,jenkins 连接到远程节点,停止容器,从 GitHub 接收更新,然后再次启动容器。我能够编写一个启动和停止容器的管道。

pipeline {
    agent any

    stages {
        stage('SSH') {
            steps {
                sshagent(credentials: ['docker-work']) {
                    sh "ssh -tt -o StrictHostKeyChecking=no root@192.168.0.103 docker-compose -f /home/CloudToDoList/CloudToDoList/docker-compose.yml stop"
                    sh "ssh -tt -o StrictHostKeyChecking=no root@192.168.0.103 docker-compose -f /home/CloudToDoList/CloudToDoList/docker-compose.yml up -d"
                }
            }
        }
    }
}

但 git 命令存在问题

pipeline {
    agent any

    stages {
        stage('SSH') {
            steps {
                sshagent(credentials: ['docker-work']) {
                    sh "ssh -tt -o StrictHostKeyChecking=no root@192.168.0.103 git --git-dir=/home/CloudToDoList/CloudToDoList/.git log"
                }
            }
        }
    }
}

如果我使用这样的管道,它会进入无休止的下载。但命令本身正在运行,无论存储库如何,所有内容都会正确输出(我使用日志只是为了测试)

root@docker-work:~# git --git-dir=/home/CloudToDoList/CloudToDoList/.git log
commit 660e12c50978995333eebeeecdcf2a71d6747961 (HEAD -> master, origin/master)
Author: root <root@docker-work.samovar.ru>
Date:   Thu Oct 24 14:27:28 2024 +0000

    готовая страница Note, File, Task, GroupTask, TaskForGroup

0