注意
GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。
简介
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 的作业语法
job1:
variables:
GIT_CHECKOUT: "true"
script:
- echo "Run your script here"
GitHub Actions 作业的语法
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 的运行器语法
windows_job:
tags:
- windows
script:
- echo Hello, %USERNAME%!
linux_job:
tags:
- linux
script:
- echo "Hello, $USER!"
GitHub Actions 运行程序的语法
windows_job:
runs-on: windows-latest
steps:
- run: echo Hello, %USERNAME%!
linux_job:
runs-on: ubuntu-latest
steps:
- run: echo "Hello, $USER!"
有关详细信息,请参阅“GitHub Actions 的工作流语法”。
Docker 映像
GitLab CI/CD 都 GitHub Actions 支持在 Docker 映像中运行作业。 在 GitLab CI/CD 中,Docker 映像是使用 image 密钥定义的,而 GitHub Actions 其中是使用密钥完成的 container 。
下面是每个系统的语法示例:
GitLab CI/CD 的 Docker 映像语法
my_job:
image: node:20-bookworm-slim
GitHub Actions Docker 映像的语法
jobs:
my_job:
container: node:20-bookworm-slim
有关详细信息,请参阅“GitHub Actions 的工作流语法”。
条件和表达式语法
GitLab CI/CD 使用 rules 确定作业是否在特定条件下运行。
GitHub Actions 使用 if 关键字防止作业运行,除非满足条件。
下面是每个系统的语法示例:
GitLab CI/CD 的条件和表达式语法
deploy_prod:
stage: deploy
script:
- echo "Deploy to production server"
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
GitHub Actions 条件和表达式的语法
jobs:
deploy_prod:
if: contains( github.ref, 'master')
runs-on: ubuntu-latest
steps:
- run: echo "Deploy to production server"
有关详细信息,请参阅“对工作流和操作中的表达式求值”。
作业之间的依赖关系
GitLab CI/CD 都 GitHub Actions 允许为作业设置依赖项。 在这两个系统中,作业默认并行运行,但可以使用密钥显式needs指定作业GitHub Actions依赖项。 GitLab CI/CD 还具有 stages 的概念,其中作业分阶段同时运行,但下一阶段将在前一阶段的所有作业完成时开始。 可以使用密钥重新创建此方案GitHub Actionsneeds。
下面是每个系统的语法示例: 工作流首先同时运行两个名为 build_a 和 build_b 的作业,当这些作业完成后,另一个名为 test_ab 的作业将运行。 最后,test_ab 完成后,deploy_ab 作业将运行。
GitLab CI/CD 作业之间的依赖关系语法
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 作业之间的依赖关系的语法
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 的工作流语法”。
调度工作流程
GitLab CI/CD 都 GitHub Actions 允许在特定时间间隔运行工作流。 在 GitLab CI/CD 中,管道计划使用 UI 进行配置,而 GitHub Actions 可以使用“on”键按计划间隔触发工作流。
有关详细信息,请参阅“触发工作流的事件”。
变量和机密
GitLab CI/CD 并支持 GitHub Actions 在管道或工作流配置文件中设置变量,以及使用 GitLab 或 GitHub UI 创建机密。
缓存
GitLab CI/CD 并在 GitHub Actions 配置文件中提供一种方法来手动缓存工作流文件。
下面是每个系统的语法示例:
GitLab CI/CD 的缓存语法
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 缓存的语法
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 的工件语法
script:
artifacts:
paths:
- math-homework.txt
GitHub Actions 项目的语法
- name: Upload math result for job 1
uses: actions/upload-artifact@v3
with:
name: homework
path: math-homework.txt
有关详细信息,请参阅“使用工作流工件存储和共享数据”。
数据库和服务容器
这两个系统都允许您包括用于数据库、缓存或其他依赖项的其他容器。
在 GitLab CI/CD 中,使用密钥指定image作业的容器,同时使用GitHub Actionscontainer密钥。 在这两个系统中,使用 services 键指定附加服务容器。
下面是每个系统的语法示例:
GitLab CI/CD 的数据库和服务容器语法
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 数据库和服务容器的语法
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 服务容器通信”。