← 返回博客
·Linkmetax DevOps 团队·12 分钟阅读
从 Jenkins 到 GitHub Actions:现代企业 CI/CD 自动化流水线迁移与提效指南
Jenkins 迁移到 GitHub Actions 完整方法论。Pipeline 语法对照、Runner 选型、密钥迁移、Self-hosted 部署、5 个真实迁移案例。
CI/CDJenkinsGitHub Actions
「我们 Jenkins 跑了 5 年,每周挂 1 次,运维快崩溃了。想换 GitHub Actions,但是 200+ 个 pipeline 怎么迁?」
这是 2024-2026 年 DevOps 团队的高频任务。Jenkins 老态毕露(插件管理、单点故障、UI 落后),GitHub Actions / GitLab CI 是新标准。这篇拆 Jenkins → GA 完整迁移路线 + 平滑过渡技巧。
一、为什么 Jenkins 该换了
Jenkins 的痛点
- 插件地狱:1,800+ 插件、版本冲突、安全漏洞
- 单点故障:Master 挂全公司 CI 停
- 维护成本:专人运维、升级失败常发生
- UI 落后:Blue Ocean 也救不了
- 声明式 vs Scripted:两套语法并存,乱
GitHub Actions 的优势
- 托管:GitHub 维护,几乎不挂
- YAML 配置:版本控制 + Review 友好
- Marketplace:2 万+ Action,复用度高
- 集成 PR:CI 状态直接显示在 PR
- 价格:公开仓库免费、私有仓库 2,000 分钟/月免费
何时不该迁
- 复杂的 Jenkins Pipeline 用了大量 Groovy → 迁成本高
- 自建机房 + 强合规 → 自托管更适合
- 大量 Windows / 嵌入式构建 → 看 Runner 能不能跑
二、迁移前评估
Step 1:清点现状
- Jenkins 多少 Job / Pipeline?
- 哪些是核心、哪些是僵尸?
- 用了哪些插件?
- 自定义 Library 有多少?
Step 2:分类
- 简单 Job(< 50 行 Groovy)→ 直接迁
- 中等 Job(50-500 行)→ 重构 + 迁
- 复杂 Job(> 500 行)→ 评估收益再决定
Step 3:确定目标
- 全部用 GitHub Hosted Runner(最简单)
- 部分用 Self-hosted Runner(需 Linux/Win 自建)
- 保留 Jenkins 跑特殊任务(混合模式)
三、Pipeline 语法对照表
触发条件
# Jenkins (declarative)
triggers {
cron('H/15 * * * *')
}
# GitHub Actions
on:
schedule:
- cron: '*/15 * * * *'
push:
branches: [main]
pull_request:
branches: [main]
阶段(Stage / Step)
// Jenkins
stage('Build') {
steps {
sh 'mvn clean package'
}
}
# GitHub Actions
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: mvn clean package
环境变量
// Jenkins
environment {
NODE_ENV = 'production'
}
# GitHub Actions
env:
NODE_ENV: production
密钥
// Jenkins
withCredentials([string(credentialsId: 'API_KEY', variable: 'API_KEY')]) {
sh 'curl -H "Authorization: $API_KEY" ...'
}
# GitHub Actions
- run: curl -H "Authorization: ${{ secrets.API_KEY }}" ...
并行
// Jenkins
parallel(
test_unit: { sh 'npm test' },
test_lint: { sh 'npm run lint' }
)
# GitHub Actions
jobs:
test_unit:
runs-on: ubuntu-latest
steps:
- run: npm test
test_lint:
runs-on: ubuntu-latest
steps:
- run: npm run lint
矩阵(Matrix)
# GitHub Actions(比 Jenkins 简洁)
strategy:
matrix:
node-version: [18, 20, 22]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
四、Self-hosted Runner(企业自托管)
何时需要
- 构建依赖内网资源(公司 npm 仓库 / 私有镜像)
- 不能上公网(合规)
- 需要特殊硬件(GPU / Mac M 系列)
- 想用自己的 IP 出口
部署方法
# 1. 在 GitHub 仓库 Settings → Actions → Runners → New self-hosted
# 2. 按提示在服务器跑:
mkdir actions-runner && cd actions-runner
curl -o actions-runner-linux-x64.tar.gz -L \
https://github.com/actions/runner/releases/download/v2.319.1/actions-runner-linux-x64-2.319.1.tar.gz
tar xzf ./actions-runner-linux-x64.tar.gz
./config.sh --url https://github.com/your-org --token YOUR_TOKEN
# 3. 作为服务运行
sudo ./svc.sh install
sudo ./svc.sh start
高可用 Runner 集群
- 多台服务器装 Runner
- 一台挂不影响整体
- 自动负载均衡
Runner 在 K8s 上
- actions-runner-controller(ARC)
- 按需自动起 Runner Pod
- 用完销毁
- 资源利用率最高
五、密钥与凭证迁移
Jenkins 凭证管理
- 存在 Jenkins 内部(不安全)
- 或对接 Vault / AWS Secrets Manager
GitHub Actions 凭证
- Repository Secrets:仓库级
- Organization Secrets:组织级共享
- Environment Secrets:环境级(production / staging)
高级:OIDC(推荐 ⭐)
- 不用 long-lived key
- GitHub Actions 用 OIDC token 直接换 AWS / Azure / GCP 临时凭证
- 安全大幅提升
# 配 OIDC 后
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789:role/GitHubActions
aws-region: us-east-1
六、5 个迁移真实案例
Case 1:50 人创业公司
- Jenkins: 30 个 pipeline
- 迁移用时:3 周
- 迁移后:CI 失败率从 20% 降到 5%
Case 2:金融公司(合规重)
- 全部 self-hosted Runner
- OIDC 集成 AWS
- 用时:3 月
Case 3:跨国电商
- Hybrid:GitHub Hosted Runner + Self-hosted Runner
- 海外用 Hosted、国内用 Self-hosted
- 用时:6 月
Case 4:保留 Jenkins
- 业务发现 Jenkins 跑 ML 训练(GPU 调度好)
- 决定:GA 跑日常 CI、Jenkins 跑特殊任务
- 混合模式 OK
Case 5:一刀切换
- 200 个 pipeline 重写
- 用时:12 月
- 痛但彻底
七、迁移工具
自动转换工具
- GitHub Actions Importer(官方):
gh actions-importer migrate jenkins \ --target-url https://github.com/your-org/your-repo \ --jenkins-url https://your-jenkins.com - 自动转换 60-70% 简单 pipeline
- 复杂的还得手工调
AI 辅助迁移
- 把 Jenkinsfile 喂给 Claude / GPT
- 让 AI 转成 YAML
- 人工 review
八、性能与成本
GitHub Hosted Runner
- 免费额度:私有仓库 2,000 min/月
- 超出后:$0.008/min(Linux)/ $0.064/min(Windows)/ $0.08/min(Mac)
成本测算
某 50 人团队:
- 月 CI 跑时间:1 万分钟
- 免费 2k 分钟 → 付费 8k 分钟
- 费用 $64/月
- 比维护 Jenkins 服务器(月 1,500 元)便宜
Self-hosted 成本
- 服务器:ECS 4C8G 月费 ~1,000 元
- 维护:50% 一个人
- 适合超大量 CI
九、5 个最佳实践
实践 1:用 reusable workflow
# .github/workflows/build.yml
on: workflow_call
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm install && npm test
在其他 workflow 引用:
jobs:
test:
uses: ./.github/workflows/build.yml
实践 2:用 composite action
把重复步骤封装为 action
实践 3:缓存依赖
- uses: actions/setup-node@v4
with:
cache: 'npm'
缩短构建时间 50%+
实践 4:分支保护
- main 分支必须 CI 通过才能 merge
- 强制 review
实践 5:监控 + 告警
- CI 失败率监控
- 时长 P95 监控
- 异常告警到飞书 / 钉钉
十、决策矩阵
| 你的现状 | 推荐 | |---|---| | < 50 人 + Jenkins 简单 | 迁移 GitHub Actions | | 50-500 人 + Jenkins 复杂 | 逐步迁移,6-12 月 | | > 500 人 + 关键业务 | 混合模式:GA 新项目、Jenkins 老项目 | | 强合规 + 自建机房 | GitLab Self-hosted | | 多语言 / 多平台 | GitHub Actions + Self-hosted Runner |
写在最后
我们整理了 《从 0 构建企业级高效持续集成流水线配置合集》:
- GitHub Actions 完整模板(前端 / 后端 / Mobile / 部署)
- Jenkins → GA 迁移指南
- Self-hosted Runner 高可用部署
- OIDC + AWS/Azure 集成模板
- 5 个真实企业迁移案例
📥 留下联系方式获取 → 联系销售取合集 →
或了解我们的 企业 DevOps 方案,含 CI/CD 迁移咨询 + 实施 + 培训。
📥PDF 白皮书
下载《从 Jenkins 到 GitHub Actions:现代企业 CI/CD 自动化流水线迁移与提效指南》PDF 完整版
留下邮箱,立刻获取本文 PDF + 后续企业 AI / 软件采购干货
- ✓ 含全部图表、检查清单、参考链接
- ✓ 可用于内部分享 / 招投标资料引用
- ✓ 后续更新自动推送 · 不发垃圾邮件
