Muhammad Rehan Saeed
Muhammad Rehan Saeed

Reputation: 38517

Migrating Azure Pipelines Deployments to GitHub Actions

I have an Azure Pipeline build which builds NuGet packages on Windows, MacOS and Linux and a deployment job that then takes the NuGet packages built from the Windows image and publishes them to Azure Artefacts, GitHub packages and NuGet.

With GitHub actions, I have managed to build NuGet packages on Windows, MacOS and Linux but I don't know how to then create a deployment job as that feature doesn't exist. I think I need to create a new deployment job that is triggered from a successful build job, pick up the NuGet packages from the successful build and then push them. However, I don't see a trigger type that can do that.

GitHub Actions Build 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/[email protected]
      with:
        name: ${{matrix.os}}
        path: './Artefacts'

Azure Pipeline Build & Deployment 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

Upvotes: 0

Views: 613

Answers (1)

Leo Liu
Leo Liu

Reputation: 76910

Migrating Azure Pipelines Deployments to GitHub Actions

Indeed, there is no such multi-stage feature for Github action at this moment.

Just as you suspect, we could create a deployment job to deploy the artifact. We could try to create a new job which needs the existing build job, in the new job, download the artifacts and push them to azure artifact, github packages, 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

You could check the Github action jobs.<job_id>.needs and the sample for some more details.

Hope this helps.

Upvotes: 1

Related Questions