在 CentOS 上把 Jenkins 和 Git 集成,通常分为以下几个步骤:安装 Git、安装 Jenkins、在 Jenkins 中配置 Git、创建 Jenkins 任务并关联 Git 仓库。
下面以 CentOS 7/8 为例,给出一个常见且实用的做法。
sudo yum install -y git
验证:
git --version
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum install -y java-11-openjdk-devel
sudo yum install -y jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
查看状态:
sudo systemctl status jenkins
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
浏览器访问:
http://<服务器IP>:8080
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Manage Jenkins → Manage Plugins
Manage Jenkins → Global Tool Configuration
找到 Git:
Default/usr/bin/git(可用 which git 查看路径)
Manage Jenkins → Credentials → System → Global credentials
添加:
New Item → Freestyle project
https://github.com/yourname/yourrepo.git
或git@github.com:yourname/yourrepo.git
*/main 或 */masterH/5 * * * *
例如:
echo "Building..."
mvn clean package
# 或
npm install && npm run build
Console Output
Cloning repository https://github.com/xxx/xxx.git
报错:
git: command not found
解决:
可改用:
sudo dnf install -y git
如果你愿意,可以告诉我:
我可以给你一套更精确的配置示例。