温馨提示×

centos中jenkins怎么用

小樊
50
2026-10-03 08:15:36
栏目: 智能运维

在 CentOS 上使用 Jenkins,一般流程是:安装 → 启动 → 配置 → 创建任务 → 构建。下面按实操步骤给你讲清楚。


一、安装 Jenkins(CentOS 7/8 通用)

1. 安装 Java(Jenkins 依赖)

# CentOS 7
yum install -y java-1.8.0-openjdk

# CentOS 8
dnf install -y java-11-openjdk

检查:

java -version

2. 添加 Jenkins 官方源

wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo

rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

3. 安装 Jenkins

yum install -y jenkins

二、启动 Jenkins

systemctl start jenkins
systemctl enable jenkins

查看状态:

systemctl status jenkins

如果防火墙开着:

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

三、初始化 Jenkins

浏览器访问:

http://服务器IP:8080

1. 获取初始密码

cat /var/lib/jenkins/secrets/initialAdminPassword

复制粘贴到页面中。

2. 安装插件

  • 推荐:Install suggested plugins
  • 常用插件:
    • Git
    • Maven Integration
    • Pipeline
    • SSH

3. 创建管理员账号


四、Jenkins 常见使用方式


方式一:Freestyle 项目(最简单)

1. 新建任务

  • 点击 New Item
  • 输入名称
  • 选择 Freestyle project

2. 配置 Git

  • Source Code Management → Git
  • Repository URL:https://github.com/xxx/xxx.git

3. 构建(Build)

例如执行 shell:

mvn clean package

或:

sh build.sh

方式二:Pipeline(推荐)

1. 新建 Pipeline 任务

2. 写 Jenkinsfile(示例)

pipeline {
    agent any
    stages {
        stage('拉代码') {
            steps {
                git 'https://github.com/xxx/xxx.git'
            }
        }
        stage('构建') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('部署') {
            steps {
                sh 'bash deploy.sh'
            }
        }
    }
}

五、Jenkins 目录说明(CentOS)

路径 作用
/var/lib/jenkins 主目录
/var/log/jenkins 日志
/etc/sysconfig/jenkins 配置文件
/var/lib/jenkins/secrets 密码、密钥

六、常见问题

1. 端口修改

编辑:

vi /etc/sysconfig/jenkins

修改:

JENKINS_PORT="8080"

重启:

systemctl restart jenkins

2. 权限问题

chown -R jenkins:jenkins /var/lib/jenkins

3. 构建没权限执行命令

在 Jenkins 全局配置中:

  • 使用 jenkins 用户
  • 或配置 SSH 到目标服务器

七、典型使用场景

✅ 自动拉代码
✅ 自动构建(Maven / npm / Docker)
✅ 自动部署到服务器
✅ 定时任务(如每天构建)


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

  • CentOS 版本(7 还是 8)
  • 用 Jenkins 做什么(Java / 前端 / Docker / K8s)
  • 是否要远程部署

我可以直接给你 可复制的完整配置示例。

0 踩