# 从 GitLab CI/CD 迁移到 GitHub Actions

GitHub Actions 和 GitLab CI/CD 共享多个配置相似之处，这使得迁移到 GitHub Actions 相对简单。

## 简介

GitLab CI/CD 和 GitHub Actions 两者都允许创建可自动生成、测试、发布、发布和部署代码的工作流。 GitLab CI/CD 并在 GitHub Actions 工作流配置中共享一些相似之处：

* 工作流程配置文件以 YAML 编写并存储在代码仓库中。
* 工作流程包括一项或多项作业。
* 作业包括一个或多个步骤或单个命令。
* 作业可以在托管或自托管计算机上运行。

存在一些差异，本指南将向你展示重要差异，以便可以将工作流迁移到 GitHub Actions其中。

## 作业

GitLab CI/CD 中的作业与其中 GitHub Actions的工作非常相似。 在这两个系统中，作业具有以下特征：

* 作业包含一系列按顺序运行的步骤或脚本。
* 作业可在单独的计算机或单独的容器中运行。
* 默认情况下作业并行运行，但可以配置为按顺序运行。

可在作业中运行脚本或 shell 命令。 在 GitLab CI/CD 中，使用 `script` 键指定脚本步骤。 在中 GitHub Actions，所有脚本都使用 `run` 密钥指定。

下面是每个系统的语法示例：

### GitLab CI/CD 的作业语法

```yaml
job1:
  variables:
    GIT_CHECKOUT: "true"
  script:
    - echo "Run your script here"
```

### GitHub Actions 用于作业的语法

```yaml
jobs:
  job1:
    steps:
      - uses: actions/checkout@v6
      - run: echo "Run your script here"
```

## 运行器

运行器是运行作业的机器。 GitLab CI/CD 和 GitHub Actions 都提供运行器的托管和自托管版本。 在 GitLab CI/CD 中，`tags` 用于在不同平台上运行作业，而在 GitHub Actions 中，则通过 `runs-on` 键来实现。

下面是每个系统的语法示例：

### GitLab CI/CD 的运行器语法

```yaml
windows_job:
  tags:
    - windows
  script:
    - echo Hello, %USERNAME%!

linux_job:
  tags:
    - linux
  script:
    - echo "Hello, $USER!"
```

### GitHub Actions 的运行器语法

```yaml
windows_job:
  runs-on: windows-latest
  steps:
    - run: echo Hello, %USERNAME%!

linux_job:
  runs-on: ubuntu-latest
  steps:
    - run: echo "Hello, $USER!"
```

有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idruns-on)”。

## Docker 映像

GitLab CI/CD 和 GitHub Actions 都支持在 Docker 镜像中运行作业。 在 GitLab CI/CD 中，Docker 映像是通过 `image` 键定义的，而在 GitHub Actions 中，则是通过 `container` 键定义的。

下面是每个系统的语法示例：

### GitLab CI/CD 的 Docker 映像语法

```yaml
my_job:
  image: node:20-bookworm-slim
```

### GitHub Actions Docker 映像的语法

```yaml
jobs:
  my_job:
    container: node:20-bookworm-slim
```

有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idcontainer)”。

## 条件和表达式语法

GitLab CI/CD 使用 `rules` 确定作业是否在特定条件下运行。
GitHub Actions 使用 `if` 关键字来阻止作业在条件未满足时运行。

下面是每个系统的语法示例：

### GitLab CI/CD 的条件和表达式语法

```yaml
deploy_prod:
  stage: deploy
  script:
    - echo "Deploy to production server"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
```

### GitHub Actions 条件和表达式的语法

```yaml
jobs:
  deploy_prod:
    if: contains( github.ref, 'master')
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploy to production server"
```

有关详细信息，请参阅“[对工作流和操作中的表达式求值](/zh/actions/reference/workflows-and-actions/expressions)”。

## 作业之间的依赖关系

GitLab CI/CD 和 GitHub Actions 都允许您为作业设置依赖项。 在这两个系统中，作业默认会并行运行，但可以在 GitHub Actions 中使用 `needs` 键显式指定作业依赖关系。 GitLab CI/CD 还具有 `stages` 的概念，其中作业分阶段同时运行，但下一阶段将在前一阶段的所有作业完成时开始。 您可以在 GitHub Actions 中使用 `needs` 键重现此场景。

下面是每个系统的语法示例： 工作流首先同时运行两个名为 `build_a` 和 `build_b` 的作业，当这些作业完成后，另一个名为 `test_ab` 的作业将运行。 最后，`test_ab` 完成后，`deploy_ab` 作业将运行。

### GitLab CI/CD 作业之间的依赖关系语法

```yaml
stages:
  - build
  - test
  - deploy

build_a:
  stage: build
  script:
    - echo "This job will run first."

build_b:
  stage: build
  script:
    - echo "This job will run first, in parallel with build_a."

test_ab:
  stage: test
  script:
    - echo "This job will run after build_a and build_b have finished."

deploy_ab:
  stage: deploy
  script:
    - echo "This job will run after test_ab is complete"
```

### GitHub Actions 作业之间的依赖关系的语法

```yaml
jobs:
  build_a:
    runs-on: ubuntu-latest
    steps:
      - run: echo "This job will be run first."

  build_b:
    runs-on: ubuntu-latest
    steps:
      - run: echo "This job will be run first, in parallel with build_a"

  test_ab:
    runs-on: ubuntu-latest
    needs: [build_a,build_b]
    steps:
      - run: echo "This job will run after build_a and build_b have finished"

  deploy_ab:
    runs-on: ubuntu-latest
    needs: [test_ab]
    steps:
      - run: echo "This job will run after test_ab is complete"
```

有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idneeds)”。

## 调度工作流程

GitLab CI/CD 和 GitHub Actions 都允许按特定时间间隔运行工作流。 在 GitLab CI/CD 中，管道计划使用 UI 进行配置，而 GitHub Actions 可以使用“on”键按计划间隔触发工作流。

有关详细信息，请参阅“[触发工作流的事件](/zh/actions/reference/workflows-and-actions/events-that-trigger-workflows#schedule)”。

## 变量和机密

GitLab CI/CD 和 GitHub Actions 都支持在管道或工作流配置文件中设置变量，并通过 GitLab 或 GitHub 的 UI 创建机密信息。

有关详细信息，请参阅 [在变量中存储信息](/zh/actions/how-tos/write-workflows/choose-what-workflows-do/use-variables) 和 [机密](/zh/actions/concepts/security/secrets)。

## 缓存

GitLab CI/CD 并在 GitHub Actions 配置文件中提供一种方法来手动缓存工作流文件。

下面是每个系统的语法示例：

### GitLab CI/CD 的缓存语法

```yaml
image: node:latest

cache:
  key: $CI_COMMIT_REF_SLUG
  paths:
    - .npm/

before_script:
  - npm ci --cache .npm --prefer-offline

test_async:
  script:
    - node ./specs/start.js ./specs/async.spec.js
```

### GitHub Actions 缓存的语法

```yaml
jobs:
  test_async:
    runs-on: ubuntu-latest
    steps:
    - name: Cache node modules
      uses: actions/cache@v4
      with:
        path: ~/.npm
        key: v1-npm-deps-${{ hashFiles('**/package-lock.json') }}
        restore-keys: v1-npm-deps-
```

## Artifacts

GitLab CI/CD 和 GitHub Actions 都可以将作业生成的文件和目录作为制品上传。 在 GitHub Actions 中，工件可用于在多个作业中保留数据。

下面是每个系统的语法示例：

### GitLab CI/CD 的工件语法

```yaml
script:
artifacts:
  paths:
    - math-homework.txt
```

### GitHub Actions 的工件语法

```yaml
- name: Upload math result for job 1
  uses: actions/upload-artifact@v4
  with:
    name: homework
    path: math-homework.txt
```

有关详细信息，请参阅“[使用工作流工件存储和共享数据](/zh/actions/tutorials/store-and-share-data)”。

## 数据库和服务容器

这两个系统都允许您包括用于数据库、缓存或其他依赖项的其他容器。

在 GitLab CI/CD 中，作业的容器通过 `image` 关键字指定，而 GitHub Actions 使用 `container` 关键字。 在这两个系统中，使用 `services` 键指定附加服务容器。

下面是每个系统的语法示例：

### GitLab CI/CD 的数据库和服务容器语法

```yaml
container-job:
  variables:
    POSTGRES_PASSWORD: postgres
    # The hostname used to communicate with the
    # PostgreSQL service container
    POSTGRES_HOST: postgres
    # The default PostgreSQL port
    POSTGRES_PORT: 5432
  image: node:20-bookworm-slim
  services:
    - postgres
  script:
    # Performs a clean installation of all dependencies
    # in the `package.json` file
    - npm ci
    # Runs a script that creates a PostgreSQL client,
    # populates the client with data, and retrieves data
    - node client.js
  tags:
    - docker
```

### GitHub Actions 数据库和服务容器的语法

```yaml
jobs:
  container-job:
    runs-on: ubuntu-latest
    container: node:20-bookworm-slim

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres

    steps:
      - name: Check out repository code
        uses: actions/checkout@v6

      # Performs a clean installation of all dependencies
      # in the `package.json` file
      - name: Install dependencies
        run: npm ci

      - name: Connect to PostgreSQL
        # Runs a script that creates a PostgreSQL client,
        # populates the client with data, and retrieves data
        run: node client.js
        env:
          # The hostname used to communicate with the
          # PostgreSQL service container
          POSTGRES_HOST: postgres
          # The default PostgreSQL port
          POSTGRES_PORT: 5432
```

有关详细信息，请参阅“[与 Docker 服务容器通信](/zh/actions/tutorials/use-containerized-services/use-docker-service-containers)”。