
从 GitHub 无缝迁移到 GitLab:分步指南
如果您一直在使用许多 CI/CD 工具之一,现在正在考虑切换到 GitLab,您可能想知道迁移过程的难度。迁移通常是 DevSecOps 团队在评估新解决方案时需要考虑的问题。这是因为从一个系统迁移到另一个系统可能需要大量的努力、复杂性和陡峭的学习曲线。我们想告诉您,迁移到 GitLab 的 AI 驱动的 DevSecOps 平台非常简单。
在这篇文章中,我们将逐步指导您完成整个过程。我们将介绍如何使用我们的 项目导入 功能,从 GitHub 迁移到 GitLab。我们还将解释如何手动将 GitHub Actions 迁移到 GitLab 管道。
哪些数据可以从 GitHub 迁移到 GitLab?
GitLab 的内置导入程序允许将 GitHub 项目自动迁移到 GitLab。您可以直接从 GitLab 的项目创建 UI 访问此内置导入程序。在 UI 中,您可以选择要迁移到 GitLab 的数据。以下数据可以迁移
- 存储库描述
- Git 存储库数据
- 分支保护规则
- 协作者(成员)
- 问题
- 拉取请求
- Wiki 页面
- 里程碑
- 标签
- 发行说明内容
- 发行说明附件
- 评论附件
- 问题描述附件
- 拉取请求描述附件
- 拉取请求审查评论
- 常规问题和拉取请求评论
- Git 大文件存储 (LFS) 对象
- 拉取请求审查
- 拉取请求分配的审阅者
- 拉取请求“合并者”信息
- 拉取请求讨论中的评论回复
- 拉取请求审查评论建议
- 问题和拉取请求事件
GitHub 和 GitLab 使用不同的术语来表示相同的概念,因此在迁移过程中必须执行映射。例如,当协作者或成员被迁移时,来自 GitHub 的权限将映射到以下适当的 GitLab 角色
GitHub 角色 | GitLab 角色 |
---|---|
读取 | 访客 |
分类 | 报告者 |
写入 | 开发者 |
维护 | 维护者 |
管理员 | 所有者 |
以下是如何在 GitLab 中引用 GitHub 中的一些术语
GitHub 术语 | GitLab 术语 |
---|---|
拉取请求 | 合并请求 |
操作 | 管道 |
将数据迁移到 GitLab 的先决条件
现在您已经知道可以导入哪些内容,让我们回顾一下执行迁移的先决条件。使用 GitLab 导入程序,您可以将项目从“GitHub.com”或“GitHub Enterprise”导入到“GitLab.com”或“自托管 GitLab”,前提是您满足以下要求
- 您必须是您要从 GitHub 导入的 GitLab 目标组的维护者。
- 存储库中的每个 GitHub 作者和受让人必须在 GitHub 上拥有一个公开的电子邮件地址,该地址应与其 GitLab 电子邮件地址匹配。
- GitHub 帐户必须具有公开的电子邮件地址。
- 您必须在 GitLab 上启用 GitHub 导入源(仅在自托管 GitLab 中)。
迁移用户时,GitLab 使用 GitHub 中的公开电子邮件地址在 GitLab 上使用相同的电子邮件地址验证用户。由于电子邮件所有权是唯一的,因此 GitLab 验证有助于确认您已设置具有有效权限的有效用户。
将数据从 GitHub 导入 GitLab
让我们看一下如何执行迁移。
我将迁移我的项目,即 Reddit 情感分析器,从 GitHub 迁移到 GitLab。Reddit 情感分析器包含一个拉取请求(在 GitLab 中称为合并请求)、问题和评论。
注意:虽然您可能没有我的项目的权限,但分步过程适用于您拥有的任何项目。我使用我的项目是为了让您看到如何在下一节中迁移 GitHub Actions。
- 使用 项目创建界面 在 GitLab 中创建一个项目。
- 选择“导入项目”框。这使您可以从外部源迁移数据。
- 在“从导入项目”下,单击“GitHub”按钮。这将带您进入“使用 GitHub 身份验证”页面。
- 单击“使用 GitHub 身份验证”按钮。如果您愿意,您也可以指定来自 GitHub 的 个人访问令牌,并使用“repo 范围”。这将带您进入 GitHub 授权应用程序。在“授权 GitLab.com”页面上,您可以授予对您要迁移的项目的 GitHub 组织 的访问权限。
- 单击您要迁移的项目的组织的“授予”按钮。
- 单击“授权 gitlabhq”按钮以授予 GitLab 对所选组织的访问权限。然后您将被带到导入选择页面。
- 从这里,您可以选择要导入的项目。
注意:您选择的迁移项目越多,导入所需的时间就越长。
- 接下来,设置要迁移 GitHub 项目的 GitLab 位置。
- 单击“导入”按钮,导入过程将开始。您可以在 UI 中看到进度。导入完成后,状态将更改为“已完成”。
您现在应该在工作区中拥有导入的项目。例如,我的项目位于 https://gitlab.com/awkwardferny/reddit-sentiment-analyzer。
在检查导入的项目时,您将在 GitLab 上看到以下内容
- 存储库迁移后
- 问题迁移后
- 合并请求迁移后
将 GitHub Actions 迁移到 GitLab CI/CD
从 GitHub 迁移项目后,您会注意到没有 GitHub Actions 正在运行。不用担心;手动迁移它们是一个简单的过程。让我们开始迁移 GitHub Actions 的过程。
- 检查
.github/workflows
文件夹中的 GitHub Actions。在您刚刚导入的 项目 中,您应该看到三个 Action 文件:lint.yml
、smoke.yml
和unit.yml
。让我们看一下这些文件的详细信息。lint.yml
:此文件包含使用 flake8 对源代码执行 lint 的 Action。此 action 使用python:3.10
Docker 镜像,并在执行 lint 之前安装应用程序要求。yamlname: "Lint" on: push: branches: [master] pull_request: branches: [master] jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python 3.10 uses: actions/setup-python@v4 with: python-version: "3.10" - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
smoke.yml
:此文件包含通过运行 CLI 帮助菜单执行冒烟测试的 Action。它使用python:3.10
Docker 镜像,并在执行冒烟测试之前安装应用程序要求。yamlname: "Smoke Tests" on: push: branches: [master] pull_request: branches: [master] jobs: smoke-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python 3.10 uses: actions/setup-python@v4 with: python-version: "3.10" - name: Install dependencies run: | python -m pip install --upgrade pip pip install setuptools if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Install Sentiment Analysis Application run: | python setup.py install - name: Run smoke tests run: | reddit-sentiment --help
unit.yml
:此文件包含使用 pytest 执行单元测试的 Action。它使用python:3.10
Docker 镜像,并在运行单元测试之前安装应用程序要求。让我们将这些 Actions 迁移到 GitLab。yamlname: "Unit Tests" on: push: branches: [master] pull_request: branches: [master] jobs: unit-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python 3.10 uses: actions/setup-python@v4 with: python-version: "3.10" - name: Install dependencies run: | python -m pip install --upgrade pip pip install pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with pytest run: | python -m pip install --upgrade pip if [ -f test-requirements.txt ]; then pip install -r test-requirements.txt; fi pytest tests/
- 转到 GitLab 上最近导入的项目,并打开 Web IDE。
- 在根目录下创建一个名为
.gitlab-ci.yml
的文件。此文件定义了 GitLab 管道。 - 添加以下配置,这将把 GitHub Actions 作为 GitLab 管道中的作业添加。请注意描述每个部分的注释。您可以看到,在 GitLab 中执行的脚本与 GitHub Actions 中的脚本相匹配。主要区别在于用于设置作业和阶段的语法。要详细了解如何创建和配置管道,请查看 GitLab CI/CD 文档。yaml
# This creates the stages in which the jobs will run. By default, all # jobs will run in parallel in the stage. Once the jobs are completed # successfully, you move on to the next stage. The way jobs run # is completely configurable. stages: - test # With the include statement, you can quickly add jobs that have # been pre-defined in external YAML files. The SAST job I've included below # is provided and maintained by GitLab and adds Static Application # Security Testing (SAST) to your pipeline. include: - template: Jobs/SAST.gitlab-ci.yml # This is the unit test job that does exactly what is defined in # the GitHub Action in unit.yml. You can see it uses the python:3.10 # Docker image, installs the application dependencies, and then runs # the unit tests with pytest. It was added with a simple copy and # paste and minor syntax changes. unit: image: python:3.10 stage: test before_script: - python -m pip install --upgrade pip - pip install pytest - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi script: - pytest tests/ # This is the lint job that does exactly what is defined in the # GitHub Action in lint.yml. You can see it uses the python:3.10 # Docker image, installs the application dependencies, and then # performs the linting with flake8. It was added with a simple copy # and paste and minor syntax changes. lint: image: python:3.10 stage: test before_script: - python -m pip install --upgrade pip - pip install flake8 - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi script: - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics # This is the smoke test job that does exactly what is defined in # the GitHub Action in smoke.yml. You can see it uses the python:3.10 # Docker image, installs the application dependencies, and then runs # the smoke tests with the Reddit sentiment analysis CLI. It was # added with a simple copy and paste and minor syntax changes. smoke: image: python:3.10 stage: test before_script: - python -m pip install --upgrade pip - pip install setuptools - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - python setup.py install script: - reddit-sentiment --help
- 让我们将代码签入。在 Web IDE 中,点击“源代码管理”标签,它是侧边栏中从顶部算起的第三个图标。然后点击“提交”按钮,选择“继续”,瞧,现在你应该有一个正在运行的管道了。
- 检查管道并确保作业正常运行。回到你的项目,点击管道图标。你可以看到我们创建的四个作业已经运行了。
- 点击“unit”作业,你会看到单元测试已经成功运行。
$ pytest tests/ ============================= test session starts ============================== platform linux -- Python 3.10.11, pytest-7.3.1, pluggy-1.0.0 rootdir: /builds/awkwardferny/reddit-sentiment-analyzer collected 2 items tests/test_scraper.py .. [100%] ============================== 2 passed in 0.09s =============================== Cleaning up project directory and file based variables 00:00 Job succeeded
总结
将项目从 GitHub 迁移到 GitLab 初看起来可能很困难。但是,正如本指南所展示的,这个过程远没有那么复杂。通过适当的规划并遵循概述的步骤,迁移到 GitLab 平台可以是平稳和直接的。
这是一篇由 GitLab 赞助的文章。GitLab 是一个全面的基于 Web 的 DevSecOps 平台,提供 Git 代码库管理、问题跟踪、持续集成和部署管道功能。它提供开源和专有版本,旨在涵盖整个 DevOps 生命周期,使其成为寻求使用单一平台管理代码和运营数据的团队的热门选择。