将 Azure Pipelines 部署迁移到 GitHub 个操作

Migrating Azure Pipelines Deployments to GitHub Actions

我有一个 Azure Pipeline 构建,它在 Windows、MacOS 和 Linux 上构建 NuGet 包,还有一个部署作业,然后采用从 Windows 图像构建的 NuGet 包并发布他们到 Azure Artefacts、GitHub 包和 NuGet。

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

GitHub 操作构建 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 管道构建和部署 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

Migrating Azure Pipelines Deployments to GitHub Actions

的确,目前Github动作没有这种多阶段的特性。

正如您所怀疑的,我们可以创建部署作业来部署工件。我们可以尝试创建一个 需要 现有构建作业的新作业,在新作业中,下载工件并将它们推送到 azure 工件,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 and the sample 了解更多详情。

希望对您有所帮助。