温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jenkins如何配置

发布时间:2026-07-16 13:13:03 来源:亿速云 阅读:88 作者:小樊 栏目:编程语言

Jenkins 的配置可以分为安装、基础配置、插件、权限、任务、分布式、通知几个层面。下面按从零到可用的顺序给你一个完整、实用的 Jenkins 配置指南(适用于 Linux / Windows / Docker 场景)。


一、Jenkins 安装

1️⃣ 方式一:Docker(最推荐,简单)

docker run -d \
  --name jenkins \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

启动后访问:

http://localhost:8080

2️⃣ 方式二:Linux(Ubuntu / CentOS)

Ubuntu

sudo apt update
sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins -y

启动:

sudo systemctl start jenkins
sudo systemctl enable jenkins

二、首次配置 Jenkins

1️⃣ 解锁 Jenkins

查看初始密码:

cat /var/jenkins_home/secrets/initialAdminPassword

或 Docker:

docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

2️⃣ 安装插件

推荐选择:

  • Install suggested plugins

常用插件(后续可补):

  • Git
  • GitHub
  • Pipeline
  • Blue Ocean
  • Docker Pipeline
  • Credentials Binding
  • Role-based Authorization Strategy

三、全局系统配置(重点)

进入:

Manage Jenkins → System Configuration

1️⃣ 全局工具配置(Global Tool Configuration)

配置:

  • JDK
  • Git
  • Maven / Gradle
  • NodeJS
  • Docker

示例(Git):

Name: Default Git
Path: /usr/bin/git

2️⃣ 系统配置(Configure System)

重要项:

  • Jenkins URL
  • 全局环境变量
  • 邮箱通知(SMTP)
  • 构建丢弃策略(全局)

四、用户与权限配置(非常重要)

1️⃣ 启用权限管理

Manage Jenkins → Security → Enable security

选择:

  • Logged-in users can do anything(简单)
  • Role-Based Strategy(推荐生产)

2️⃣ 角色权限(Role-based)

插件:

Role-based Authorization Strategy

角色示例:

  • admin:全部权限
  • dev:构建、查看
  • readonly:只读

五、凭证(Credentials)配置

Manage Jenkins → Manage Credentials

常用凭证类型:

  • Username + Password
  • SSH Private Key
  • Git Token
  • Docker Registry

示例(Git SSH):

Kind: SSH Private Key
ID: git-ssh

六、创建 Job(任务)

1️⃣ Freestyle 项目(入门)

步骤:

  1. New Item → Freestyle project
  2. 源码管理 → Git
  3. 构建触发器
  4. 构建步骤(Shell / Bat)
  5. 构建后操作

示例 Shell:

echo "Building..."
npm install
npm run build

2️⃣ Pipeline(强烈推荐)

示例 Jenkinsfile

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/your/repo.git'
            }
        }
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
    }
}

七、构建触发器配置

常见方式:

  • 手动构建
  • Poll SCM(定时检测 Git)
  • Webhook(GitHub / GitLab)
  • 定时构建(Cron)

示例 Cron:

H/15 * * * *

八、分布式构建(Master + Agent)

1️⃣ 添加 Agent

Manage Jenkins → Nodes → New Node

配置:

  • 远程工作目录
  • 启动方式(SSH / JNLP)

九、通知配置

1️⃣ 邮件通知

Configure System → E-mail Notification

2️⃣ 企业微信 / 钉钉(插件)

插件:

  • DingTalk
  • WeChat Notification

十、常见配置建议(经验)

✅ 使用 Pipeline + Jenkinsfile
✅ 所有敏感信息放 Credentials
✅ 不要给所有人管理员权限
✅ 定期清理旧构建
✅ 使用 Docker Agent 隔离环境


十一、你可以继续问我

你可以直接说:

  • ✅「Jenkins + GitLab 怎么配置?」
  • ✅「Jenkins Pipeline 实战示例」
  • ✅「Jenkins + Docker + Spring Boot」
  • ✅「Jenkins 权限怎么细分?」
  • ✅「Jenkins 构建失败如何排查?」

我可以直接给你可复制配置或 Jenkinsfile

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI