Skip to main content

使用 GitHub Actions Importer 从 Jenkins 迁移

了解如何使用 GitHub Actions Importer 自动将 Jenkins 管道迁移到 GitHub Actions。

法律通告

关于使用 GitHub Actions Importer 从 Jenkins 迁移

以下说明将指导你配置环境以使用 GitHub Actions Importer 将 Jenkins 管道迁移到 GitHub Actions。

先决条件

  • 具有要转换为 GitHub Actions 工作流的管道和作业的 Jenkins 帐户或组织。

  • 为帐户或组织创建 Jenkins 个人 API 令牌的访问权限。

  • 一个可在其中运行基于 Linux 的容器并可安装所需工具的环境。

    注意:GitHub Actions Importer 容器和 CLI 不需要安装在 CI 平台所在的同一服务器上。

限制

使用 GitHub Actions Importer 从 Jenkins 迁移到 GitHub Actions 时,存在一些限制。 例如,必须手动迁移以下构造:

  • 必需的生成工具
  • 脚本化管道
  • 机密
  • 自托管运行程序
  • 未知插件

有关手动迁移的详细信息,请参阅“从 Jenkins 迁移到 GitHub Actions”。

安装 GitHub Actions Importer CLI 扩展

  1. 安装 GitHub Actions Importer CLI 扩展:

    Bash
    gh extension install github/gh-actions-importer
    
  2. 验证是否已安装扩展:

    $ gh actions-importer -h
    Options:
      -?, -h, --help  Show help and usage information
    
    Commands:
      update     Update to the latest version of GitHub Actions Importer.
      version    Display the version of GitHub Actions Importer.
      configure  Start an interactive prompt to configure credentials used to authenticate with your CI server(s).
      audit      Plan your CI/CD migration by analyzing your current CI/CD footprint.
      forecast   Forecast GitHub Actions usage from historical pipeline utilization.
      dry-run    Convert a pipeline to a GitHub Actions workflow and output its yaml file.
      migrate    Convert a pipeline to a GitHub Actions workflow and open a pull request with the changes.
    

配置凭据

configure CLI 命令用于在使用 Jenkins 和 GitHub 时为 GitHub Actions Importer 设置所需的凭据和选项。

  1. 创建 GitHub personal access token (classic)。 有关详细信息,请参阅“管理个人访问令牌”。

    令牌必须具有 workflow 范围。

    创建令牌后,将其复制并保存在安全的位置供之后使用。

  2. 创建 Jenkins API 令牌。 有关详细信息,请参阅 Jenkins 文档中的对脚本化客户端进行身份验证

    创建令牌后,将其复制并保存在安全的位置供之后使用。

  3. 在终端中,运行 GitHub Actions Importer configure CLI 命令:

    gh actions-importer configure
    

    configure 命令将提示你输入以下信息:

    • 对于“正在配置哪些 CI 提供程序?”,请使用箭头键选择 Jenkins,按空格键将其选中,然后按 Enter
    • 对于“Personal access token for GitHub”,输入前面创建的 personal access token (classic) 的值,然后按 Enter
    • 对于“GitHub 实例的基 URL”,按 Enter 以接受默认值 (https://github.com)。
    • 对于“Personal access token for Jenkins”,输入之前创建的 Jenkins 个人 API 令牌的值,然后按 Enter
    • 对于“Jenkins 用户的用户名”,请输入 Jenkins 用户名,然后按 Enter
    • 对于“Jenkins 实例的基 URL”,请输入 Jenkins 实例的 URL,然后按 Enter

    configure 命令的示例如下所示:

    $ gh actions-importer configure
    ✔ Which CI providers are you configuring?: Jenkins
    Enter the following values (leave empty to omit):
    ✔ Personal access token for GitHub: ***************
    ✔ Base url of the GitHub instance: https://github.com
    ✔ Personal access token for Jenkins: ***************
    ✔ Username of Jenkins user: admin
    ✔ Base url of the Jenkins instance: https://localhost
    Environment variables successfully updated.
    
  4. 在终端中,运行 GitHub Actions Importer update CLI 命令以连接到 GitHub Packages Container registry,并确保容器映像已更新到最新版本:

    gh actions-importer update
    

    命令的输出应类似于以下内容:

    Updating ghcr.io/actions-importer/cli:latest...
    ghcr.io/actions-importer/cli:latest up-to-date
    

对 Jenkins 执行审核

可使用 audit 命令获取 Jenkins 服务器中所有管道的概要视图。

audit 命令执行以下步骤:

  1. 提取 Jenkins 服务器中定义的所有项目。
  2. 将每个管道转换为其等效的 GitHub Actions 工作流。
  3. 生成一个报告,汇总使用 GitHub Actions Importer 进行迁移的完成程度和复杂性。

运行审核命令

若要对 Jenkins 服务器执行审核,请在终端中运行以下命令:

gh actions-importer audit jenkins --output-dir tmp/audit

检查审核结果

指定的输出目录中的文件包含审核结果。 有关审核结果的摘要,请参阅 audit_summary.md 文件。

审核摘要包含以下部分。

管道

“管道”部分包含有关由 GitHub Actions Importer 完成的转换率的概要统计信息。

下面列出了“管道”部分中可能出现的一些关键术语:

  • “成功”管道已将所有管道构造和单个项目自动转换为其 GitHub Actions 等效项。
  • “部分成功”管道已转换所有管道构造,但有一些单个项目未自动转换为其 GitHub Actions 等效项。
  • “不受支持”管道是 GitHub Actions Importer 不支持的定义类型。
  • “失败”管道在转换时遇到错误。 这可能是以下三个原因之一造成的:
    • 管道配置错误,在 Bamboo 中无效。
    • GitHub Actions Importer 在转换它时遇到内部错误。
    • 网络响应失败,导致管道无法访问,这通常是由于凭据无效所致。

生成步骤

“生成步骤”部分概述了跨所有管道使用的各个生成步骤,以及由 GitHub Actions Importer 自动转换的生成步骤数。

下面列出了“生成步骤”部分中可能出现的一些关键术语:

  • “已知”生成步骤是自动转换为等效操作的步骤。
  • “未知”生成步骤是未自动转换为等效操作的步骤。
  • “不受支持”生成步骤是满足以下任一条件的步骤:
    • 从根本上不受 GitHub Actions 支持。
    • 以与 GitHub Actions 不兼容的方式进行配置。
  • “操作”是转换后的工作流中使用的操作的列表。 这对于以下情况可能很重要:
    • 如果使用 GitHub Enterprise Server,收集要同步到实例的操作列表。
    • 定义所使用的操作的组织级允许列表。 此操作列表是安全或合规性团队可能需要审查的操作的综合列表。

手动任务

“手动任务”部分概述了 GitHub Actions Importer 无法自动完成且必须由你手动完成的任务。

下面列出了“手动任务”部分中可能出现的一些关键术语:

  • “机密”是在转换后的管道中使用的存储库或组织级机密。 必须在 GitHub Actions 中手动创建这些机密,才能使这些管道正常运行。 有关详细信息,请参阅“在 GitHub Actions 中使用机密”。
  • “自托管运行器”是指在转换后的管道中引用的运行器(不是 GitHub 托管的运行器)的标签。 需要手动定义这些运行器,才能使这些管道正常运行。

文件

审核报告的最后一部分提供审核期间写入磁盘的所有文件的清单。

每个管道文件都包含审核中的各种文件,包括:

  • GitHub 中定义的原始管道。
  • 用于转换管道的任何网络响应。
  • 转换后的工作流文件。
  • 可用于排查管道转换失败问题的堆栈跟踪。

此外,workflow_usage.csv 文件包含一个以逗号分隔的列表,其中列出了每个成功转换的管道所使用的所有操作、机密和运行器。 这有助于确定哪些工作流使用哪些操作、机密或运行器,并且可用于进行安全评审。

预测潜在的生成运行器使用情况

可以使用 forecast 命令通过计算 Jenkins 服务器中已完成管道运行的指标来预测可能的 GitHub Actions 使用情况。

运行预测命令的先决条件

若要对 Jenkins 实例运行 forecast 命令,必须在 Jenkins 服务器上安装 paginated-builds 插件。 通过此插件,GitHub Actions Importer 可有效地检索具有大量生成的作业的历史生成数据。 由于 Jenkins 不提供检索分页生成数据的方法,因此使用此插件可防止 Jenkins 服务器在提取大量历史数据时发生超时。 paginated-builds 插件为开源插件,公开了一个 REST API 终结点以提取页面中的生成数据,而不是一次性提取所有数据。

安装 paginated-builds 插件:

  1. 在 Jenkins 实例上,导航到 https://<your-jenkins-instance>/pluginManager/available
  2. 搜索 paginated-builds 插件。
  3. 选中左侧的框,然后选择“安装但不重启”。

运行预测命令

若要对潜在 GitHub Actions 执行预测,请在终端中运行以下命令。 默认情况下,GitHub Actions Importer 在预测报告中包括过去七天的信息。

gh actions-importer forecast jenkins --output-dir tmp/forecast

检查预测报告

指定的输出目录中的 forecast_report.md 文件包含预测结果。

下面列出了预测报告中可能出现的一些关键术语:

  • “作业计数”是已完成作业的总数。
  • “管道计数”是使用的独立管道数。
  • “执行时间”指的是运行器在一项作业中所用的时间。 此指标可用于帮助计划 GitHub 托管的运行器的成本。
    • 此指标与你应在 GitHub Actions 中花费的成本相关。 这会因这些分钟所使用的硬件而异。 可以使用 GitHub Actions 定价计算器来估算成本。
  • “队列时间”指标用于描述等待运行器可用于执行作业的时间。
  • “并发作业”指标用于描述在任何给定时间运行的作业数量。 此指标可用于定义应配置的运行器数。

此外,这些指标是针对 Jenkins 中的每个运行器队列定义的。 这在混合使用托管/自承载运行器或者高规格/低规格的计算机时特别有用,这样就可以查看特定于不同类型运行器的指标。

执行 Jenkins 管道的试运行迁移

可以使用 dry-run 命令将 Jenkins 管道转换为其等效的 GitHub Actions 工作流。

运行试运行命令

可以使用 dry-run 命令将 Jenkins 管道转换为等效的 GitHub Actions 工作流。 试运行在指定目录中创建输出文件,但不打开拉取请求来迁移管道。

若要执行将 Jenkins 管道迁移到 GitHub Actions 的试运行,请在终端中运行以下命令,将 my-jenkins-project 替换为 Jenkins 作业的 URL。

gh actions-importer dry-run jenkins --source-url my-jenkins-project --output-dir tmp/dry-run

检查转换后的工作流

可以在指定的输出目录中查看试运行日志和转换后的工作流文件。

如果有任何 GitHub Actions Importer 无法自动转换的内容,例如未知生成步骤或部分成功管道,你可能需要创建自定义转换器来进一步自定义转换过程。 有关详细信息,请参阅“使用自定义转换器扩展 GitHub Actions 导入工具”。

执行 Jenkins 管道的生产迁移

可以使用 migrate 命令转换 Jenkins 管道,并使用等效的 GitHub Actions 工作流打开拉取请求。

运行迁移命令

若要将 Jenkins 管道迁移到 GitHub Actions,请在终端中运行以下命令,将 target-url 值替换为 GitHub Enterprise Cloud 存储库的 URL,并将 my-jenkins-project 替换为 Jenkins 作业的 URL。

gh actions-importer migrate jenkins --target-url https://github.com/:owner/:repo --output-dir tmp/migrate --source-url my-jenkins-project

命令的输出包括将转换后的工作流添加到存储库的拉取请求的 URL。 成功输出的示例类似于以下内容:

$ gh actions-importer migrate jenkins --target-url https://github.com/octo-org/octo-repo --output-dir tmp/migrate --source-url http://localhost:8080/job/monas_dev_work/job/monas_freestyle
[2022-08-20 22:08:20] Logs: 'tmp/migrate/log/actions-importer-20220916-014033.log'
[2022-08-20 22:08:20] Pull request: 'https://github.com/octo-org/octo-repo/pull/1'

检查拉取请求

migrate 命令成功运行的输出包含一个指向新拉取请求的链接,此拉取请求将转换后的工作流添加到存储库。

拉取请求的一些重要元素包括:

  • 在拉取请求说明中,有一个名为“手动步骤”的部分,其中列出了在完成将管道迁移到 GitHub Actions 之前必须手动完成的步骤。 例如,此部分可能会提供创建工作流中使用的任何机密。
  • 转换后的工作流文件。 选择拉取请求中的“已更改的文件”选项卡,查看将添加到 GitHub Enterprise Cloud 存储库的工作流文件。

检查完拉取请求后,可以将其合并以将工作流添加到 GitHub Enterprise Cloud 存储库。

参考

本部分包含使用 GitHub Actions Importer 从 Jenkins 迁移时涉及的环境变量、可选参数和支持的语法的参考信息。

使用环境变量

GitHub Actions Importer 使用环境变量进行身份验证配置。 这些变量在使用 configure 命令执行配置过程时设置。 有关详细信息,请参阅“配置凭据”部分。

GitHub Actions Importer 使用以下环境变量连接到 Jenkins 实例:

  • GITHUB_ACCESS_TOKEN:用于通过转换后的工作流创建拉取请求的 personal access token (classic)(需要 repoworkflow 范围)。

  • GITHUB_INSTANCE_URL:目标 GitHub 实例的 URL(例如 https://github.com)。

  • JENKINS_ACCESS_TOKEN:用于查看 Jenkins 资源的 Jenkins API 令牌。

    注意:此令牌需要对要迁移或审核的所有作业的访问权限。 如果文件夹或作业未从其父级继承访问控制列表,则必须授予显式权限或完全管理员权限。

  • JENKINS_USERNAME:创建 Jenkins API 令牌的用户帐户的用户名。

  • JENKINS_INSTANCE_URL:Jenkins 实例的 URL。

  • JENKINSFILE_ACCESS_TOKEN:(可选)用于检索存储在生成存储库中的 Jenkinsfile 的内容的 API 令牌。 这需要 repo 范围。 如果未提供,则将改用 GITHUB_ACCESS_TOKEN

这些环境变量可以在 GitHub Actions Importer 运行时加载的 .env.local 文件中指定。

使用可选参数

有一些可选参数可以结合 GitHub Actions Importer 子命令使用来自定义迁移。

--source-file-path

可以将 --source-file-path 参数与 forecastdry-runmigration 子命令结合使用。

默认情况下,GitHub Actions Importer 从源代码管理中提取管道内容。 --source-file-path 参数指示 GitHub Actions Importer 改用指定的源文件路径。 可将此选项用于 Jenkinsfile 和多分支管道。

如果要在运行 forecast 子命令时提供多个源文件,可以在文件路径值中使用模式匹配。 例如,gh forecast --source-file-path ./tmp/previous_forecast/jobs/*.json 为 GitHub Actions Importer 提供与 ./tmp/previous_forecast/jobs/*.json 文件路径匹配的任何源文件。

Jenkinsfile 管道示例

在此示例中,GitHub Actions Importer 使用指定的 Jenkinsfile 作为源文件来执行试运行。

gh actions-importer dry-run jenkins --output-dir path/to/output/ --source-file-path path/to/Jenkinsfile --source-url :url_to_jenkins_job

--config-file-path

可以将 --config-file-path 参数与 auditdry-runmigrate 子命令结合使用。

默认情况下,GitHub Actions Importer 从源代码管理中提取管道内容。 --config-file-path 参数指示 GitHub Actions Importer 改用指定的源文件。

--config-file-path 选项与 dry-runmigrate 子命令一起使用时,GitHub Actions Importer 会将存储库数据域与 --source-url 选项表示的作业进行匹配以选择管道。 它使用 config-file-path 拉取指定的源文件。

“审核”示例

在此示例中,GitHub Actions Importer 使用指定的 YAML 配置文件来执行审核。

gh actions-importer audit jenkins --output-dir path/to/output/ --config-file-path path/to/jenkins/config.yml

若要使用配置文件审核 Jenkins 实例,配置文件必须采用以下格式,并且每个 repository_slug 值都必须是唯一的:

source_files:
  - repository_slug: pipeline-name
    path: path/to/Jenkinsfile
  - repository_slug: multi-branch-pipeline-name
    branches:
      - branch: main
        path: path/to/Jenkinsfile
      - branch: node
        path: path/to/Jenkinsfile

Jenkins 管道支持的语法

下表显示了 GitHub Actions Importer 当前能够转换的属性类型。 有关 Jenkins 管道语法如何与 GitHub Actions 保持一致的详细信息,请参阅“从 Jenkins 迁移到 GitHub Actions”。

有关支持的 Jenkins 插件的信息,请参阅 github/gh-actions-importer 存储库

自由风格管道支持的语法

JenkinsGitHub 操作状态
docker 模板jobs.<job_id>.container支持
buildjobs部分支持
构建环境env部分支持
生成触发器on部分支持
常规runners部分支持

Jenkinsfile 管道支持的语法

JenkinsGitHub 操作状态
dockerjobs.<job_id>.container支持
阶段 (stage)jobs.<job_id>支持
代理runners部分支持
环境env部分支持
stagesjobs部分支持
stepsjobs.<job_id>.steps部分支持
触发器on部分支持
whenjobs.<job_id>.if部分支持
inputsinputs不支持
矩形图jobs.<job_id>.strategy.matrix不支持
选项jobs.<job_id>.strategy不支持
parametersinputs不支持

环境变量语法

GitHub Actions Importer 使用下表中的映射将默认 Jenkins 环境变量转换为 GitHub Actions 中最接近的等效项。

JenkinsGitHub 操作
${BUILD_ID}${{ github.run_id }}
${BUILD_NUMBER}${{ github.run_id }}
${BUILD_TAG}${{ github.workflow }}-${{ github.run_id }}
${BUILD_URL}${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
${JENKINS_URL}${{ github.server_url }}
${JOB_NAME}${{ github.workflow }}
${WORKSPACE}${{ github.workspace }}

部分内容改编自 MIT 许可证下的 https://github.com/github/gh-actions-importer/

MIT License

Copyright (c) 2022 GitHub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.