Skip to main content

从 Azure Pelines 迁移到 GitHub Actions

GitHub Actions 和 Azure Pipelines 具有一些相似的配置,这使得迁移到 GitHub Actions 很简单。

简介

Azure Pipelines 和 GitHub Actions 都允许您创建能自动构建、测试、发布、发行和部署代码的工作流程。 Azure Pelines 和 GitHub Actions 的工作流程配置有一些相似之处:

  • 工作流程配置文件以 YAML 编写并存储在代码仓库中。
  • 工作流程包括一项或多项作业。
  • 作业包括一个或多个步骤或单个命令。
  • 步骤或任务可以重复使用并与社区共享。

有关详细信息,请参阅“了解 GitHub Actions”。

主要差异

从 Azure Pipelines 迁移时,考虑以下差异:

  • Azure Pipelines 支持旧版经典编辑器,这样你便可以在 GUI 编辑器中定义 CI 配置,而不是在 YAML 文件中创建管道定义。 GitHub Actions 使用 YAML 文件来定义工作流程,不支持图形编辑器。
  • Azure Pelines 允许您在作业定义中省略一些结构。 例如,如果您只有一个作业,则无需定义作业,只需要定义其步骤。 GitHub Actions 需要明确的配置,且不能省略 YAML 结构。
  • Azure Pipelines 支持 YAML 文件中定义的阶段,可用于创建部署工作流。 GitHub Actions 要求您将阶段分成单独的 YAML 工作流程文件。
  • 可以使用功能选择本地 Azure Pipelines 构建代理。 通过标签可以选择 GitHub Actions 自托管的运行器。

迁移作业和步骤

Azure Pelines 中的作业和步骤非常类似于 GitHub Actions 中的作业和步骤。 在这两个系统中,作业具有以下特征:

  • 作业包含一系列按顺序运行的步骤。
  • 作业在单独的虚拟机或单独的容器中运行。
  • 默认情况下作业并行运行,但可以配置为按顺序运行。

迁移脚本步骤

可以将脚本或 shell 命令作为工作流程中的步骤运行。 在 Azure Pipelines 中,可以使用 script 键或 bashpowershellpwsh 键指定脚本步骤。 也可以将脚本指定为 Bash 任务PowerShell 任务的输入。

在 GitHub Actions 中,所有脚本都使用 run 键指定。 若要选择特定的 shell,可以在提供脚本时指定 shell 键。 有关详细信息,请参阅“GitHub Actions 的工作流语法”。

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

Azure Pipelines 的脚本步骤语法

jobs:
  - job: scripts
    pool:
      vmImage: 'windows-latest'
    steps:
      - script: echo "This step runs in the default shell"
      - bash: echo "This step runs in bash"
      - pwsh: Write-Host "This step runs in PowerShell Core"
      - task: PowerShell@2
        inputs:
          script: Write-Host "This step runs in PowerShell"

GitHub Actions 的脚本步骤语法

jobs:
  scripts:
    runs-on: windows-latest
    steps:
      - run: echo "This step runs in the default shell"
      - run: echo "This step runs in bash"
        shell: bash
      - run: Write-Host "This step runs in PowerShell Core"
        shell: pwsh
      - run: Write-Host "This step runs in PowerShell"
        shell: powershell

脚本错误处理中的差异

在 Azure Pipelines 中,可将脚本配置为“在有任何输出发送到 stderr 时出错”。 GitHub Actions 不支持此配置。

GitHub Actions 尽可能将 shell 配置为“快速失败”,如果脚本中的一个命令退出并有错误代码,则会立即停止脚本。 相反,Azure Pipelines 需要明确配置为在出错时立即退出。 有关详细信息,请参阅“GitHub Actions 的工作流语法”。

Windows 上默认 shell 的差异

在 Azure Pipelines 中,Windows 平台上脚本的默认 shell 是命令 shell (cmd.exe)。 在 GitHub Actions 中,Windows 平台上脚本的默认 shell 是 PowerShell 。 PowerShell 在内置命令、变量扩展和流控制方面存在若干差异。

如果您运行的是简单的命令,则可以在 PowerShell 中运行命令 shell 脚本,而无需进行任何更改。 但在大多数情况下,您需要使用 PowerShell 语法更新脚本,或者指示 GitHub Actions 使用命令 shell 而不是 PowerShell 来运行脚本。 为此,可以将 shell 指定为 cmd

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

Azure Pipelines 语法 - 默认使用 CMD

jobs:
  - job: run_command
    pool:
      vmImage: 'windows-latest'
    steps:
      - script: echo "This step runs in CMD on Windows by default"

GitHub Actions 语法 - 指定 CMD

jobs:
  run_command:
    runs-on: windows-latest
    steps:
      - run: echo "This step runs in PowerShell on Windows by default"
      - run: echo "This step runs in CMD on Windows explicitly"
        shell: cmd

有关详细信息,请参阅“GitHub Actions 的工作流语法”。

迁移条件和表达式语法

Azure Pipelines 和 GitHub Actions 可以有条件地运行步骤。 在 Azure Pipelines 中,条件表达式使用 condition 键来指定。 在 GitHub Actions 中,条件表达式使用 if 键来指定。

Azure Pelines 使用表达式中的函数来有条件地执行步骤。 相反,GitHub Actions 使用 infix 表示法。 例如,必须将 Azure Pipelines 中的 eq 函数替换为 GitHub Actions 中的 == 运算符。

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

Azure Pipelines 的条件表达式语法

jobs:
  - job: conditional
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - script: echo "This step runs with str equals 'ABC' and num equals 123"
        condition: and(eq(variables.str, 'ABC'), eq(variables.num, 123))

GitHub Actions 的条件表达式语法

jobs:
  conditional:
    runs-on: ubuntu-latest
    steps:
      - run: echo "This step runs with str equals 'ABC' and num equals 123"
        if: ${{ env.str == 'ABC' && env.num == 123 }}

有关详细信息,请参阅“表达式”。

作业之间的依赖关系

Azure Pipelines 和 GitHub Actions 允许您为作业设置依赖项。 在这两个系统中,默认情况下作业并行运行,但可以明确指定作业依赖项。 在 Azure Pipelines 中,这通过 dependsOn 键来完成。 在 GitHub Actions 中,这通过 needs 键来完成。

下面是每个系统的语法示例: 工作流启动第一个名为 initial 的作业,当该作业完成时,两个分别名为 fanout1fanout2 的作业将会运行。 最后,当这些作业完成后,作业 fanin 将会运行。

Azure Pipelines 的作业之间依赖关系的语法

jobs:
  - job: initial
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - script: echo "This job will be run first."
  - job: fanout1
    pool:
      vmImage: 'ubuntu-latest'
    dependsOn: initial
    steps:
      - script: echo "This job will run after the initial job, in parallel with fanout2."
  - job: fanout2
    pool:
      vmImage: 'ubuntu-latest'
    dependsOn: initial
    steps:
      - script: echo "This job will run after the initial job, in parallel with fanout1."
  - job: fanin:
    pool:
      vmImage: 'ubuntu-latest'
    dependsOn: [fanout1, fanout2]
    steps:
      - script: echo "This job will run after fanout1 and fanout2 have finished."

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

jobs:
  initial:
    runs-on: ubuntu-latest
    steps:
      - run: echo "This job will be run first."
  fanout1:
    runs-on: ubuntu-latest
    needs: initial
    steps:
      - run: echo "This job will run after the initial job, in parallel with fanout2."
  fanout2:
    runs-on: ubuntu-latest
    needs: initial
    steps:
      - run: echo "This job will run after the initial job, in parallel with fanout1."
  fanin:
    runs-on: ubuntu-latest
    needs: [fanout1, fanout2]
    steps:
      - run: echo "This job will run after fanout1 and fanout2 have finished."

有关详细信息,请参阅“GitHub Actions 的工作流语法”。

将任务迁移到操作

Azure Pipelines 使用“任务”,这是可在多个工作流中重复使用的应用程序组件。 GitHub Actions 使用“操作”,这可用于执行任务和自定义工作流。 在这两个系统中,您可以指定要运行的任务或操作的名称,以及任何必需的输入作为键/值对。

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

Azure Pipelines 的任务语法

jobs:
  - job: run_python
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - task: UsePythonVersion@0
        inputs:
          versionSpec: '3.7'
          architecture: 'x64'
      - script: python script.py

GitHub Actions 的操作语法

jobs:
  run_python:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-python@v4
        with:
          python-version: '3.7'
          architecture: 'x64'
      - run: python script.py

你可以在 GitHub Marketplace 中找到可用于工作流的操作,也可以创建自己的操作。 有关详细信息,请参阅“创建操作”。