从 Jenkins 迁移到 GitLab CI

适用场景

希望减少 Jenkins 插件和主从维护,统一代码、流水线和制品到 GitLab。迁移难点在 Jenkinsfile 语义、凭据、构建环境和制品传递。

前置条件

步骤

  1. 导出 Jenkins 配置:
kubectl -n jenkins get configmap -o yaml > jenkins-config.yaml
find $JENKINS_HOME/jobs -name config.xml -maxdepth 3
  1. 梳理构建步骤和凭据:
grep -nE 'sh\(|withCredentials|archiveArtifacts|publish|docker' Jenkinsfile
  1. 安装 GitLab Runner:
helm repo add gitlab https://charts.gitlab.io
helm install gitlab-runner gitlab/gitlab-runner \
  --namespace gitlab-runner --create-namespace \
  --set gitlabUrl=https://gitlab.example.com \
  --set runnerRegistrationToken='***'
  1. 编写 .gitlab-ci.yml:
stages: [build, test, package]

variables:
  DOCKER_BUILDKIT: "1"

build:
  stage: build
  image: maven:3.9-eclipse-temurin-21
  script:
    - mvn -B clean package
  artifacts:
    paths: [target/*.jar]

test:
  stage: test
  image: maven:3.9-eclipse-temurin-21
  script:
    - mvn -B test

package:
  stage: package
  image: docker:27
  services: [docker:27-dind]
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  1. 迁移凭据到 GitLab CI/CD Variables,使用 masked/protected。

  2. 配置缓存和制品过期时间,避免存储膨胀。

  3. 并行运行 Jenkins 和 GitLab CI,对比构建结果后再切换。

验证

回滚

常见坑

相关命令