About migrating from Jenkins with GitHub Actions Importer
The instructions below will guide you through configuring your environment to use GitHub Actions Importer to migrate Jenkins pipelines to GitHub Actions.
Prerequisites
- A Jenkins account or organization with pipelines and jobs that you want to convert to GitHub Actions workflows.
- Access to create a Jenkins personal API token for your account or organization.
- 一个可在其中运行基于 Linux 的容器并可安装所需工具的环境。
-
Docker 已安装并正在运行。
-
已安装 GitHub CLI。
注意
GitHub Actions Importer 容器和 CLI 不需要安装在 CI 平台所在的同一服务器上。
-
Limitations
There are some limitations when migrating from Jenkins to GitHub Actions with GitHub Actions Importer. For example, you must migrate the following constructs manually:
- Mandatory build tools
- Scripted pipelines
- Secrets
- Self-hosted runners
- Unknown plugins
For more information on manual migrations, see Migrating from Jenkins to GitHub Actions.
Installing the GitHub Actions Importer CLI extension
-
安装 GitHub Actions Importer CLI 扩展:
Bash gh extension install github/gh-actions-importer
gh extension install github/gh-actions-importer
-
验证是否已安装扩展:
$ 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.
Configuring credentials
The configure
CLI command is used to set required credentials and options for GitHub Actions Importer when working with Jenkins and GitHub.
-
Create a GitHub personal access token (classic). For more information, see 管理个人访问令牌.
Your token must have the
workflow
scope.After creating the token, copy it and save it in a safe location for later use.
-
Create a Jenkins API token. For more information, see Authenticating scripted clients in the Jenkins documentation.
After creating the token, copy it and save it in a safe location for later use.
-
In your terminal, run the GitHub Actions Importer
configure
CLI command:gh actions-importer configure
The
configure
command will prompt you for the following information:- For "Which CI providers are you configuring?", use the arrow keys to select
Jenkins
, press Space to select it, then press Enter. - For "Personal access token for GitHub", enter the value of the personal access token (classic) that you created earlier, and press Enter.
- For "Base url of the GitHub instance", enter the URL for 你的 GitHub Enterprise Server 实例, and press Enter.
- For "Personal access token for Jenkins", enter the value for the Jenkins personal API token that you created earlier, and press Enter.
- For "Username of Jenkins user", enter your Jenkins username and press Enter.
- For "Base url of the Jenkins instance", enter the URL of your Jenkins instance, and press Enter.
An example of the
configure
command is shown below:$ 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.
- For "Which CI providers are you configuring?", use the arrow keys to select
-
In your terminal, run the GitHub Actions Importer
update
CLI command to connect to GitHub Packages Container registry and ensure that the container image is updated to the latest version:gh actions-importer update
The output of the command should be similar to below:
Updating ghcr.io/actions-importer/cli:latest... ghcr.io/actions-importer/cli:latest up-to-date
Perform an audit of Jenkins
You can use the audit
command to get a high-level view of all pipelines in a Jenkins server.
The audit
command performs the following steps:
- Fetches all of the projects defined in a Jenkins server.
- Converts each pipeline to its equivalent GitHub Actions workflow.
- Generates a report that summarizes how complete and complex of a migration is possible with GitHub Actions Importer.
Running the audit command
To perform an audit of a Jenkins server, run the following command in your terminal:
gh actions-importer audit jenkins --output-dir tmp/audit
Inspecting the audit results
指定的输出目录中的文件包含审核结果。 有关审核结果的摘要,请参阅 audit_summary.md
文件。
审核摘要包含以下部分。
管道
“管道”部分包含有关由 GitHub Actions Importer 完成的转换率的概要统计信息。
下面列出了“管道”部分中可能出现的一些关键术语:
- “成功”管道已将所有管道构造和单个项目自动转换为其 GitHub Actions 等效项。
- “部分成功”管道已转换所有管道构造,但有一些单个项目未自动转换为其 GitHub Actions 等效项。
- “不受支持”管道是 GitHub Actions Importer 不支持的定义类型。
- “失败”管道在转换时遇到错误。 这可能是以下三个原因之一造成的:
- 该管道最初配置错误且无效。
- GitHub Actions Importer 在转换它时遇到内部错误。
- 网络响应失败,导致管道无法访问,这通常是由于凭据无效所致。
生成步骤
“生成步骤”部分概述了跨所有管道使用的各个生成步骤,以及由 GitHub Actions Importer 自动转换的生成步骤数。
下面列出了“生成步骤”部分中可能出现的一些关键术语:
- “已知”生成步骤是自动转换为等效操作的步骤。
- “未知”生成步骤是未自动转换为等效操作的步骤。
- “不受支持”生成步骤是满足以下任一条件的步骤:
- 从根本上不受 GitHub Actions 支持。
- 以与 GitHub Actions 不兼容的方式进行配置。
- “操作”是转换后的工作流中使用的操作的列表。 这对于以下情况可能很重要:
- 如果使用 GitHub Enterprise Server,收集要同步到实例的操作列表。
- 定义所使用的操作的组织级允许列表。 此操作列表是安全或合规性团队可能需要审查的操作的综合列表。
手动任务
“手动任务”部分概述了 GitHub Actions Importer 无法自动完成且必须由你手动完成的任务。
下面列出了“手动任务”部分中可能出现的一些关键术语:
- “机密”是在转换后的管道中使用的存储库或组织级机密。 必须在 GitHub Actions 中手动创建这些机密,才能使这些管道正常运行。 有关详细信息,请参阅“Using secrets in GitHub Actions”。
- “自托管运行器”是指在转换后的管道中引用的运行器(不是 GitHub 托管的运行器)的标签。 需要手动定义这些运行器,才能使这些管道正常运行。
文件
审核报告的最后一部分提供审核期间写入磁盘的所有文件的清单。
每个管道文件都包含审核中的各种文件,包括:
- GitHub 中定义的原始管道。
- 用于转换管道的任何网络响应。
- 转换后的工作流文件。
- 可用于排查管道转换失败问题的堆栈跟踪。
此外,workflow_usage.csv
文件包含一个以逗号分隔的列表,其中列出了每个成功转换的管道所使用的所有操作、机密和运行器。 这有助于确定哪些工作流使用哪些操作、机密或运行器,并且可用于进行安全评审。
Forecast potential build runner usage
You can use the forecast
command to forecast potential GitHub Actions usage by computing metrics from completed pipeline runs in your Jenkins server.
Prerequisites for running the forecast command
In order to run the forecast
command against a Jenkins instance, you must install the paginated-builds
plugin on your Jenkins server. This plugin allows GitHub Actions Importer to efficiently retrieve historical build data for jobs that have a large number of builds. Because Jenkins does not provide a method to retrieve paginated build data, using this plugin prevents timeouts from the Jenkins server that can occur when fetching a large amount of historical data. The paginated-builds
plugin is open source, and exposes a REST API endpoint to fetch build data in pages, rather than all at once.
To install the paginated-builds
plugin:
- On your Jenkins instance, navigate to
https://<your-jenkins-instance>/pluginManager/available
. - Search for the
paginated-builds
plugin. - Check the box on the left and select Install without restart.
Running the forecast command
To perform a forecast of potential GitHub Actions, run the following command in your terminal. By default, GitHub Actions Importer includes the previous seven days in the forecast report.
gh actions-importer forecast jenkins --output-dir tmp/forecast
Inspecting the forecast report
The forecast_report.md
file in the specified output directory contains the results of the forecast.
Listed below are some key terms that can appear in the forecast report:
- The job count is the total number of completed jobs.
- The pipeline count is the number of unique pipelines used.
- Execution time describes the amount of time a runner spent on a job. This metric can be used to help plan for the cost of GitHub-hosted runners.
- This metric is correlated to how much you should expect to spend in GitHub Actions. This will vary depending on the hardware used for these minutes. You can use the GitHub Actions pricing calculator to estimate the costs.
- Queue time metrics describe the amount of time a job spent waiting for a runner to be available to execute it.
- Concurrent jobs metrics describe the amount of jobs running at any given time. This metric can be used to define the number of runners you should configure.
Additionally, these metrics are defined for each queue of runners in Jenkins. This is especially useful if there is a mix of hosted or self-hosted runners, or high or low spec machines, so you can see metrics specific to different types of runners.
Perform a dry-run migration of a Jenkins pipeline
You can use the dry-run
command to convert a Jenkins pipeline to its equivalent GitHub Actions workflow.
Running the dry-run command
You can use the dry-run
command to convert a Jenkins pipeline to an equivalent GitHub Actions workflow. A dry-run creates the output files in a specified directory, but does not open a pull request to migrate the pipeline.
To perform a dry run of migrating your Jenkins pipelines to GitHub Actions, run the following command in your terminal, replacing my-jenkins-project
with the URL of your Jenkins job.
gh actions-importer dry-run jenkins --source-url my-jenkins-project --output-dir tmp/dry-run
Inspecting the converted workflows
You can view the logs of the dry run and the converted workflow files in the specified output directory.
如果有任何 GitHub Actions Importer 无法自动转换的内容,例如未知生成步骤或部分成功管道,你可能需要创建自定义转换器来进一步自定义转换过程。 有关详细信息,请参阅“Extending GitHub Actions Importer with custom transformers”。
Perform a production migration of a Jenkins pipeline
You can use the migrate
command to convert a Jenkins pipeline and open a pull request with the equivalent GitHub Actions workflow.
Running the migrate command
To migrate a Jenkins pipeline to GitHub Actions, run the following command in your terminal, replacing the target-url
value with the URL for your GitHub repository, and my-jenkins-project
with the URL for your Jenkins job.
gh actions-importer migrate jenkins --target-url https://github.com/:owner/:repo --output-dir tmp/migrate --source-url my-jenkins-project
The command's output includes the URL to the pull request that adds the converted workflow to your repository. An example of a successful output is similar to the following:
$ 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 之前必须手动完成的步骤。 例如,此部分可能会提供创建工作流中使用的任何机密。
- 转换后的工作流文件。 选择拉取请求中的“Files changed”选项卡,查看将添加到 GitHub 仓库的工作流文件****。
检查完拉取请求后,可以将其合并以将工作流添加到 GitHub 仓库。
Reference
This section contains reference information on environment variables, optional arguments, and supported syntax when using GitHub Actions Importer to migrate from Jenkins.
Using environment variables
GitHub Actions Importer 使用环境变量进行身份验证配置。 这些变量在使用 configure
命令执行配置过程时设置。 有关详细信息,请参阅“配置凭据”部分。
GitHub Actions Importer uses the following environment variables to connect to your Jenkins instance:
-
GITHUB_ACCESS_TOKEN
: The personal access token (classic) used to create pull requests with a converted workflow (requiresrepo
andworkflow
scopes). -
GITHUB_INSTANCE_URL
: The URL to the target GitHub instance (for example,https://github.com
). -
JENKINS_ACCESS_TOKEN
: The Jenkins API token used to view Jenkins resources.注意
This token requires access to all jobs that you want to migrate or audit. In cases where a folder or job does not inherit access control lists from their parent, you must grant explicit permissions or full admin privileges.
-
JENKINS_USERNAME
: The username of the user account that created the Jenkins API token. -
JENKINS_INSTANCE_URL
: The URL of the Jenkins instance. -
JENKINSFILE_ACCESS_TOKEN
(Optional) The API token used to retrieve the contents of aJenkinsfile
stored in the build repository. This requires therepo
scope. If this is not provided, theGITHUB_ACCESS_TOKEN
will be used instead.
These environment variables can be specified in a .env.local
file that is loaded by GitHub Actions Importer when it is run.
Using optional arguments
有一些可选参数可以结合 GitHub Actions Importer 子命令使用来自定义迁移。
--source-file-path
You can use the --source-file-path
argument with the forecast
, dry-run
, or migration
subcommands.
By default, GitHub Actions Importer fetches pipeline contents from source control. The --source-file-path
argument tells GitHub Actions Importer to use the specified source file path instead. You can use this option for Jenkinsfile and multibranch pipelines.
If you would like to supply multiple source files when running the forecast
subcommand, you can use pattern matching in the file path value. For example, gh forecast --source-file-path ./tmp/previous_forecast/jobs/*.json
supplies GitHub Actions Importer with any source files that match the ./tmp/previous_forecast/jobs/*.json
file path.
Jenkinsfile pipeline example
In this example, GitHub Actions Importer uses the specified Jenkinsfile as the source file to perform a dry run.
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
You can use the --config-file-path
argument with the audit
, dry-run
, and migrate
subcommands.
By default, GitHub Actions Importer fetches pipeline contents from source control. The --config-file-path
argument tells GitHub Actions Importer to use the specified source files instead.
When you use the --config-file-path
option with the dry-run
or migrate
subcommands, GitHub Actions Importer matches the repository slug to the job represented by the --source-url
option to select the pipeline. It uses the config-file-path
to pull the specified source file.
Audit example
In this example, GitHub Actions Importer uses the specified YAML configuration file to perform an audit.
gh actions-importer audit jenkins --output-dir path/to/output/ --config-file-path path/to/jenkins/config.yml
To audit a Jenkins instance using a config file, the config file must be in the following format, and each repository_slug
value must be unique:
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
Supported syntax for Jenkins pipelines
The following tables show the type of properties GitHub Actions Importer is currently able to convert. For more details about how Jenkins pipeline syntax aligns with GitHub Actions, see Migrating from Jenkins to GitHub Actions.
For information about supported Jenkins plugins, see the github/gh-actions-importer
repository.
Supported syntax for Freestyle pipelines
Jenkins | GitHub Actions | Status |
---|---|---|
docker template | jobs.<job_id>.container | Supported |
build | jobs | Partially supported |
build environment | env | Partially supported |
build triggers | on | Partially supported |
general | runners | Partially supported |
Supported syntax for Jenkinsfile pipelines
Jenkins | GitHub Actions | Status |
---|---|---|
docker | jobs.<job_id>.container | Supported |
stage | jobs.<job_id> | Supported |
agent | runners | Partially supported |
environment | env | Partially supported |
stages | jobs | Partially supported |
steps | jobs.<job_id>.steps | Partially supported |
triggers | on | Partially supported |
when | jobs.<job_id>.if | Partially supported |
inputs | inputs | Unsupported |
matrix | jobs.<job_id>.strategy.matrix | Unsupported |
options | jobs.<job_id>.strategy | Unsupported |
parameters | inputs | Unsupported |
Environment variables syntax
GitHub Actions Importer uses the mapping in the table below to convert default Jenkins environment variables to the closest equivalent in GitHub Actions.
Jenkins | GitHub Actions |
---|---|
${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 }} |
Legal notice
部分内容改编自 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.