Publish application artifacts for linux using Github Actions

I have a simple ASP.Net Core 3.0 razor page project that I need to deploy on an Ubuntu server. I am doing development on my Macbook Pro, so I need to be able to build the project on ubuntu. I could setup an ubuntu virtual machine, but since Github released Github Actions that might be a better way and easier way to get it done.

Getting started with Github actions is quite simple, all the workflow is defined in an yaml file and stored in a folder named `.github/workflows` at the root of the repository. First thing was to create a workflow file `.github/workflows/release.yml`.

The first thing I like to do in the workflow file is to give the workflow a name. The next thing is to specify what should trigger the workflow. This is done with the `on` part.

on:
  push:
    tags:
      - 'v*'
Specifying what should trigger the workflow

I wanted to trigger a release build when I created and pushed a tag with a version number. When I then created a tag v1.0.0 the release build triggered and the artifacts was published as a Github release.

Then it is time to define the jobs and steps that should be executed and also the enviroment that should be used. In this example I specify a matrix with only one element. This is because I only needed the ubuntu release artifacts, but on a normal commit I like to be able to build the project on windows and macos as well. With this setup, the matrix can just be extended.

In the first step I just run the dotnet publish command to get the artifact that I want to publish. Then I zip the artifact folder and publish to Github. To be able to first create a release and then upload the artifact, I specify a GITHUB_TOKEN from the secret store. For the GITHUB_TOKEN, I did not need to add the secret to the secret store that is in the settings of the repository. The oportunity is here to define tokens and password that not should be in clear text.

name: ASP.NET Core Publish

on: 
  push:
    tags:
      - 'v*'

jobs:
  build_and_test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
    steps:
    - uses: actions/checkout@v1
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.0.100
    - name: Publish with dotnet
      run: dotnet publish -c Release -o ./artifacts
    - name: Zip artifacts
      uses: montudor/action-zip@v0.1.0
      with:
        args: zip -qq -r ./release.zip ./artifacts
    - name: Create release
      id: create_release
      uses: actions/create-release@v1.0.0
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        draft: false
        prerelease: false
    - name: Upload Release Asset
      id: upload-release-asset
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./release.zip
        asset_name: release.zip
        asset_content_type: application/zip

Wrap up

I am a huge fan of build configuration and build steps checked into the repository where it belongs. What I spent most time doing when setting up the Github Action was the Github Action stuff itself and that is probably because I never have used it before. From now on, I am using Github actions on the personal projects that I have (at least the ones that I need to build). One other thing that I like is that it is a great way to get the binaries for other platform than the one I am devloping at.

Teis Lindemark

Passionate software developer and beer brewer

Bergen, Norway https://teilin.net