Skip to content

Jenkins - Git 设置

将 Jenkins 与 Git 集成对于大多数现代 CI/CD 工作流至关重要,它允许 Jenkins 在代码推送变更或按计划自动拉取 (checkout) 源码。

Jenkins 需要 ‘Git plugin’ 来与 Git 仓库交互。通常在初始设置时作为“推荐插件”的一部分安装。如果未安装,或要确保拥有该插件:

  1. 导航到 Manage Jenkins → Plugins。
  2. 点击 ‘Available Plugins’ 选项卡。
  3. 在过滤器框中输入 ‘Git plugin’。
  4. 勾选 ‘Git plugin’ 旁边的复选框。
  5. 点击 ‘Install without restart’ 或 ‘Download now and install after restart’。Jenkins 将下载并安装插件。
  6. 如有必要,重启 Jenkins(尽管插件安装通常不再需要)。

Git 插件通常会捆绑 ‘Git client plugin’,如果系统路径 (system path) 中未找到 Git 可执行文件 (executable),它会提供该文件。

步骤 2: 配置 Git 工具(可选但推荐)

Section titled “步骤 2: 配置 Git 工具(可选但推荐)”

虽然 Git 客户端插件提供了 Git 可执行文件,但明确配置 Git 仍然是个好习惯,特别是当你需要特定版本,或者 Jenkins 用户默认的系统 PATH 中没有 Git 时。

  1. 导航到 Manage Jenkins → Global Tool Configuration。
  2. 向下滚动到 ‘Git’ 部分。
  3. 点击 ‘Add Git’。
  4. 提供一个 ‘Name’(例如,Default 或 Git-2.x)。
  5. 选项 1: 系统 Git: 如果 Git 已安装在控制器/代理节点 (controller/agents) 上并且在 PATH 中,你可以将 ‘Path to Git executable’ 留空,或者明确提供路径(例如 /usr/bin/git)。
  6. 选项 2: 自动安装: 勾选 ‘Install automatically’。选择一个安装程序(例如 ‘Install from git-scm.com’)并选择一个版本。Jenkins 将下载并管理此 Git 安装。
  7. 点击 ‘Save’。

步骤 3: 配置作业/流水线使用 Git

Section titled “步骤 3: 配置作业/流水线使用 Git”

现在你可以配置一个 Jenkins 项目(流水线或自由风格作业)从 Git 仓库拉取代码。

在声明式流水线 (Declarative Pipeline) 中使用 Git (Jenkinsfile)

Section titled “在声明式流水线 (Declarative Pipeline) 中使用 Git (Jenkinsfile)”

标准方式是使用 git 步骤或在 pipeline 块中使用 SCM 定义。

pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo 'Checking out code from Git...'
// Simple checkout from a public repository
// git url: 'https://github.com/your-username/your-repo.git', branch: 'main'
// Checkout from a private repository using credentials
git url: 'https://github.com/your-username/your-private-repo.git',
branch: 'develop',
credentialsId: 'github-ssh-key-credential' // 来自 Jenkins Credentials 的 ID
}
}
stage('Build') {
steps {
// 在此处添加你的构建步骤,例如:
sh 'mvn clean package'
}
}
}
}

在自由风格项目 (Freestyle Project) 中使用 Git

Section titled “在自由风格项目 (Freestyle Project) 中使用 Git”
  1. 创建一个新的 ‘Freestyle project’ 或配置一个已有的项目。
  2. 在项目配置页面,向下滚动到 ‘Source Code Management’(源码管理)部分。
  3. 选择 ‘Git’。
  4. Repository URL: 输入你的 Git 仓库 URL (HTTPS 或 SSH)。
  5. Credentials: 如果是私有仓库,点击 ‘Add’ → ‘Jenkins’ 来选择或添加必要的凭据(例如 Username/Password, SSH Username with private key, Secret text 用于个人访问令牌 PAT)。从下拉菜单中选择合适的 Credential ID。
  6. Branches to build: 指定要构建的分支(例如 */main, */develop, */feature/*)。*/main 是常用设置。
  7. Additional Behaviours: 如有需要,配置高级选项,如浅克隆 (shallow clone)(拉取更快,历史较少)、拉取策略 (checkout strategy)、稀疏拉取 (sparse checkout) 等。
  8. 根据需要配置构建触发器 (build triggers)(例如 ‘Poll SCM’、‘GitHub hook trigger for GITScm polling’)和构建步骤 (build steps)。
  9. 点击 ‘Save’。

对于私有仓库,安全地存储凭据至关重要。

  • 导航到 Manage Jenkins → Credentials → System → Global credentials (unrestricted)(全局凭据(无限制))或创建一个特定域 (domain)。
  • 点击 ‘Add Credentials’。
  • 选择凭据的 ‘Kind’(类型):
    • Username with password: 用于 HTTPS 身份验证。
    • SSH Username with private key: 用于 SSH 身份验证(推荐用于安全)。你可以直接粘贴私钥,或让 Jenkins 从控制器上的文件中加载。
    • Secret text: 用于个人访问令牌 (PATs)(例如 GitHub PAT)。
  • 填写详细信息(Username, Password/Private Key/Secret, ID, Description)。‘ID’ 是你在作业配置中将使用的标识符。
  • 点击 ‘Create’。

配置好 Git 集成后,Jenkins 就可以自动获取你的源代码,从而构成了你的 CI/CD 流程的基础。