温馨提示×

centos怎样集成jenkins和git

小樊
44
2026-09-06 10:19:47
栏目: 智能运维

CentOS 上把 JenkinsGit 集成,通常分为以下几个步骤:安装 Git、安装 Jenkins、在 Jenkins 中配置 Git、创建 Jenkins 任务并关联 Git 仓库。

下面以 CentOS 7/8 为例,给出一个常见且实用的做法。


一、安装 Git

sudo yum install -y git

验证:

git --version

二、安装 Jenkins

1. 添加 Jenkins 仓库

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

2. 安装 JDK(Jenkins 依赖)

sudo yum install -y java-11-openjdk-devel

3. 安装 Jenkins

sudo yum install -y jenkins

4. 启动 Jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins

查看状态:

sudo systemctl status jenkins

三、开放防火墙端口(默认 8080)

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

浏览器访问:

http://<服务器IP>:8080

四、初始化 Jenkins

  1. 查看初始管理员密码:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  1. 登录后:
    • 安装 推荐插件
    • 创建管理员用户

五、安装 Git 插件(通常已默认安装)

  1. 进入:
Manage Jenkins → Manage Plugins
  1. 搜索并确认已安装:
    • Git plugin
    • Git client plugin

六、在 Jenkins 中配置 Git

1. 配置 Git 路径

Manage Jenkins → Global Tool Configuration

找到 Git

  • Name: Default
  • Path to Git executable: /usr/bin/git

(可用 which git 查看路径)


2.(可选)配置 Git 凭据(私有仓库)

Manage Jenkins → Credentials → System → Global credentials

添加:

  • 类型:Username with password(Git 账号)
  • SSH Username with private key(推荐)

七、创建 Jenkins 任务并集成 Git

1. 新建任务

New Item → Freestyle project

2. 配置源码管理

  • 选择 Git
  • Repository URL:
    https://github.com/yourname/yourrepo.git
    
    git@github.com:yourname/yourrepo.git
    
  • Credentials:选择上面配置的凭据
  • Branch:*/main*/master

3. 配置构建触发器(可选)

  • ✅ Poll SCM
    H/5 * * * *
    
  • 或配合 GitHub Webhook / GitLab Webhook

4. 添加构建步骤

例如:

echo "Building..."
mvn clean package
# 或
npm install && npm run build

八、验证集成是否成功

  1. 手动点击 Build Now
  2. 查看:
Console Output
  1. 看到类似信息说明成功:
Cloning repository https://github.com/xxx/xxx.git

九、常见问题

1. Jenkins 无法执行 git

报错:

git: command not found

解决:

  • 确认 Git 已安装
  • Jenkins 中 Git 路径配置正确

2. 私有仓库权限失败

  • 使用 SSH key
  • 或在 Jenkins 凭据中配置正确账号

3. CentOS 8 已停止维护

可改用:

sudo dnf install -y git

如果你愿意,可以告诉我:

  • 用的是 GitLab / GitHub / Gitee
  • Jenkins 是否需要 自动构建(Webhook)
  • 构建的是 Java /前端 /Python

我可以给你一套更精确的配置示例。

0