在 Jenkins 中连接 Git 仓库,通常分为基础配置和任务中使用两部分。下面以最常见的 Git + Jenkins Freestyle / Pipeline 为例说明。
Jenkins 默认一般已安装 Git Plugin,如果没有:
Manage Jenkins → Manage Plugins → AvailableJenkins 所在服务器需安装 Git:
# CentOS
yum install git -y
# Ubuntu
apt install git -y
# 验证
git --version
https://github.com/user/repo.gitgit@github.com:user/repo.gitManage Jenkins → Credentials → System → Global credentialsUsername with passwordssh-keygen -t rsa -C "jenkins"
SSH Username with private keygitFreestyle projectGitgit@github.com:user/repo.gitmain / master)pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main',
url: 'git@github.com:user/repo.git',
credentialsId: 'git-ssh-id'
}
}
}
}
credentialsId是在 Jenkins 凭据中配置的 ID
在 Jenkins 服务器执行:
git ls-remote git@github.com
确认 host key 已加入 known_hosts
如果你愿意,可以告诉我:
我可以给你更具体的配置示例。