首页 > 解决方案 > 将 Azure Pipelines 部署迁移到 GitHub Actions

问题描述

我有一个 Azure Pipeline 构建,它在 Windows、MacOS 和 Linux 上构建 NuGet 包,以及一个部署作业,然后获取从 Windows 映像构建的 NuGet 包并将它们发布到 Azure Artefacts、GitHub 包和 NuGet。

通过 GitHub 操作,我已设法在 Windows、MacOS 和 Linux 上构建 NuGet 包,但我不知道如何创建部署作业,因为该功能不存在。我想我需要创建一个从成功的构建作业触发的新部署作业,从成功的构建中获取 NuGet 包,然后推送它们。但是,我没有看到可以做到这一点的触发器类型。

GitHub Actions 构建 YAML

name: Build
on:
  push:
    branches:
      - '*'
    tags:
      - '*'
env:
  DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
  DOTNET_CLI_TELEMETRY_OPTOUT: true
  MINVERBUILDMETADATA: build.$(Build.BuildId)
jobs:
  build:
    name: Build-${{matrix.os}}
    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        lfs: true
        fetch-depth: 0
    - name: 'Git Fetch Tags'
      run: git fetch --tags
      shell: pwsh
    - name: 'Install .NET Core SDK'
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.301
    - name: 'Dotnet Tool Restore'
      run: dotnet tool restore
      shell: pwsh
    - name: 'Dotnet Cake Build'
      run: dotnet cake --target=Build
      shell: pwsh
    - name: 'Dotnet Cake Test'
      run: dotnet cake --target=Test
      shell: pwsh
    - name: 'Dotnet Cake Pack'
      run: dotnet cake --target=Pack
      shell: pwsh
    - name: 'Publish Artefacts'
      uses: actions/upload-artifact@v1.0.0
      with:
        name: ${{matrix.os}}
        path: './Artefacts'

Azure Pipeline 构建和部署 YAML

trigger:
  branches:
    include:
    - '*'
  tags:
    include:
    - '*'
variables:
  DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
  DOTNET_CLI_TELEMETRY_OPTOUT: true
  MINVERBUILDMETADATA: build.$(Build.BuildId)
stages:
- stage: Build
  jobs:
  - job: Build
    strategy:
      matrix:
        Linux:
          matrixName: Ubuntu
          vmImageName: ubuntu-latest
        Mac:
          matrixName: Mac
          vmImageName: macos-latest
        Windows:
          matrixName: Windows
          vmImageName: windows-latest
    pool:
      vmImage: $(vmImageName)
    timeoutInMinutes: 10
    steps:
    - checkout: self
      lfs: true
    - task: UseDotNet@2
      displayName: 'Install .NET Core SDK'
      inputs:
        packageType: 'sdk'
        useGlobalJson: true
    - pwsh: 'dotnet tool restore'
      displayName: 'Dotnet Tool Restore'
      failOnStderr: true
    - pwsh: 'dotnet cake --target=Build'
      displayName: 'Dotnet Cake Build'
      failOnStderr: true
    - pwsh: 'dotnet cake --target=Test'
      displayName: 'Dotnet Cake Test'
      failOnStderr: true
    - pwsh: 'dotnet cake --target=Pack'
      displayName: 'Dotnet Cake Pack'
      failOnStderr: true
    - task: PublishTestResults@2
      displayName: 'Publish Test Results'
      inputs:
        testResultsFormat: 'VSTest'
        testResultsFiles: '**/*.trx'
    - task: PublishCodeCoverageResults@1
      inputs:
        codeCoverageTool: cobertura
        summaryFileLocation: '**/*.cobertura.xml'
    - publish: './Artefacts'
      artifact: $(matrixName)
      displayName: 'Publish Artefacts'
- stage: Deploy
  jobs:
  - deployment: AzureArtefacts
    displayName: 'Azure Artefacts'
    condition: ne(variables['Build.Reason'], 'PullRequest')
    pool:
      vmImage: windows-latest
    environment: 'Azure Artefacts'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: NuGetToolInstaller@1
            displayName: 'NuGet Install'
          - task: NuGetAuthenticate@0
            displayName: 'NuGet Authenticate'
          - pwsh: nuget push $(Agent.BuildDirectory)\Windows\*.nupkg -Source $(AzureArtefactsSource) -ApiKey AzureArtifacts -SkipDuplicate
            displayName: 'NuGet Push'
            failOnStderr: true
  - deployment: GitHub
    condition: ne(variables['Build.Reason'], 'PullRequest')
    pool:
      vmImage: windows-latest
    environment: 'GitHub'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: NuGetToolInstaller@1
            displayName: 'NuGet Install'
          - pwsh: nuget source Add -Name GitHub -Source https://nuget.pkg.github.com/GITHUB-USERNAME -UserName GITHUB-USERNAME -Password $(GitHubPersonalAccessToken)
            displayName: 'NuGet Add Source'
            failOnStderr: true
          - pwsh: nuget push $(Agent.BuildDirectory)\Windows\*.nupkg -Source GitHub -SkipDuplicate
            displayName: 'NuGet Push'
            failOnStderr: true
  - deployment: NuGet
    condition: and(ne(variables['Build.Reason'], 'PullRequest'), startsWith(variables['Build.sourceBranch'], 'refs/tags/'))
    pool:
      vmImage: windows-latest
    environment: 'NuGet'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: NuGetToolInstaller@1
            displayName: 'Install NuGet'
          - pwsh: |
              Get-ChildItem $(Agent.BuildDirectory)\Windows -Filter *.nupkg |
              Where-Object { !$_.Name.Contains('preview') } |
              ForEach-Object { nuget push $_ -Source https://api.nuget.org/v3/index.json -ApiKey $(NuGetApiKey) -SkipDuplicate }
            displayName: 'NuGet Push'
            failOnStderr: true

标签: yamlazure-pipelinesazure-pipelines-release-pipelinegithub-actions

解决方案


将 Azure Pipelines 部署迁移到 GitHub Actions

确实,目前 Github 动作没有这样的多阶段功能。

正如您所怀疑的,我们可以创建一个部署作业来部署工件。我们可以尝试创建一个需要现有构建作业的新作业,在新作业中,下载工件并将它们推送到 azure artifact、github 包、nuget:

jobs:
  job_1:
    name: Build

  job_2:
    name: Deploy
    needs: job_1
    runs-on: windows-latest
    steps:
      - name: Download math result for job 1
      uses: actions/download-artifact@v1
      with:
        name: xxx

您可以查看 Github 操作jobs.<job_id>.needs示例以获取更多详细信息。

希望这可以帮助。


推荐阅读