问题1. 源码管理处填入Repository URL,报错:无法连接仓库:Error performing git command: ls-remote -h https://github.com/txy2023/GolangLearning.git HEAD

原因:
jenkins全局工具配置里默认没有添加git的路径,如果你是centos系统,可以which git获取git路径。然后从Dashboard依次点击系统管理-全局工具配置,在Path to Git executable填入git路径

问题2. 构建任务,控制台报错: ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.

原因:
拉取的分支不对,git项目默认分支一般是main,而jenkins默认拉取分支是master,因此在源码管理处修改指定分支即可


修改后,再次构建,成功拉取git项目,默认保存路径为/var/lib/jenkins/workspace/xxx
问题3. 源码管理处填入Repository URL,报错:stderr: Host key verification failed. fatal: Could not read from remote repository.

原因:
- github上没有添加
SSH key - github上添加的
SSH key不对,jenkins拉取源码时默认使用的是jenkins用户
具体方法:
- 切换到
jenkins用户,生成ssh公钥
bash
su -s /bin/sh jenkins
ssh-keygen

- 根据生成的
id_rsa.pub文件,在github上重新添加SSH Key

- 最重要的一步 ,在jenkins用户下,手动执行一次Command
/usr/bin/git ls-remote -h git@github.com:txy2023/GolangLearning.git HEAD(根据报错页面上实际的Command执行),目的是自动生成~/.ssh/known_hosts
