Adding artifacts to a Github Release from Azure DevOps pipeline

I recently updated an old project hosted on GitHub, this MSSQL Prometheus exporter project, to build with Azure DevOps.

The process

Every once in a while when I have a new feature or bugfix to release, I’ll create a GitHub release like the one pictured here.

Release 4

When this release is created, I want to build a generic version for Linux and a generic version for Windows and attach them to this release.

To give the step-by-step:

  1. Create GitHub release “v0.4.6” or some such tag.
  2. Azure DevOps builds the tag and creates the artifacts.
  3. Azure DevOps attaches those artifacts to the release.

Service Connections

One thing to know is that the Azure DevOps task - task: GithubRelease@0 requires a Service Connection to GitHub to be setup.

Service Connections

Code

Here’s the actual code I’m using. Do replace the build script sections with however you build your project. The point is that the same artifacts being published to Azure DevOps are also being zipped or tarred and sent to GitHub release, if there is a GitHub release.

trigger:
  branches:
    include:
    - master
    - releases/*
  tags:
    include:
    - v*

pool:
  vmImage: 'Ubuntu 18.04'

variables:
  buildConfiguration: 'Release'

steps:
- task: UseDotNet@2
  displayName: 'Use .NET Core sdk 3.1'
  inputs:
    packageType: sdk
    version: 3.1.x
    installationPath: $(Agent.ToolsDirectory)/dotnet

- script: |
    export githubTag=$(git describe --abbrev=0 --tags)
    echo "##vso[task.setvariable variable=githubTag]$githubTag"
  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
  displayName: Set tag to env variable githubTag
  failOnStderr: true

- script: |
    dotnet publish ./src/server/ --configuration $(buildConfiguration) --output $BUILD_ARTIFACTSTAGINGDIRECTORY/win_x64 --self-contained true -r win-x64
  failOnStderr: true
  displayName: Build Win x64

- script: |
    dotnet publish ./src/server/ --configuration $(buildConfiguration) --output $BUILD_ARTIFACTSTAGINGDIRECTORY/linux_x64 --self-contained true -r linux-x64
  failOnStderr: true
  displayName: Build Linux x64

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)/win_x64'
    artifactName: 'win_x64'
  displayName: Publish Win x64 artifacts

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)/linux_x64'
    artifactName: 'linux_x64'
  displayName: Publish Linux x64 artifacts

- task: ArchiveFiles@2
  displayName: Zip Win x64 artifacts
  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
  inputs:
    rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/win_x64'
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/mssql_exporter_win_x64.zip'
    replaceExistingArchive: true

- task: ArchiveFiles@2
  displayName: Tar Linux x64 artifacts
  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
  inputs:
    rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/linux_x64'
    archiveType: 'tar'
    tarCompression: 'gz'
    archiveFile: '$(Build.ArtifactStagingDirectory)/mssql_exporter_linux_x64.tar.gz'
    replaceExistingArchive: true

- task: GithubRelease@0
  displayName: 'Attach Linux x64 artifacts to GitHub Release'
  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
  inputs:
    gitHubConnection: github.com_DanielOliver
    repositoryName: DanielOliver/mssql_exporter
    assets: '$(Build.ArtifactStagingDirectory)/mssql_exporter_linux_x64.tar.gz'
    action: edit
    target: '$(Build.SourceVersion)'
    tag: '$(githubTag)'
    addChangeLog: false
    assetUploadMode: replace

- task: GithubRelease@0
  displayName: 'Attach Win x64 artifacts to GitHub Release'
  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
  inputs:
    gitHubConnection: github.com_DanielOliver
    repositoryName: DanielOliver/mssql_exporter
    assets: $(Build.ArtifactStagingDirectory)/mssql_exporter_win_x64.zip
    action: edit
    target: '$(Build.SourceVersion)'
    tag: $(githubTag)
    addChangeLog: true
    assetUploadMode: replace

Summary

Azure DevOps is a great platform with an amazing pricing tier for open-source projects and makes it easy to setup pipelines for projects. Being able to also keep your code on GitHub can be a nice bonus of choice.