Getting Started with Github Actions for .NET Core
When I pushed my recent Ghoplin mini-project to GitHub, it automatically offered to define a build action. I really liked the user experience here: GitHub offered the Action feature, detected my project type and generated a file, with project-appropriate defaults and in the correct folder.
I updated the dontnet version in the script and tried it; everything works and the task reports a success.
name: .NET Core
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.10
- name: Build
run: dotnet publish -r win-x64 --configuration ReleaseComplete
I wanted to take it a step further. GitHub has a marketplace with many different, though often sparsely documented, actions. They're really easy to plug into the workflow, though - just add new -name
item into the step list in the YAML file.
In the end, I wanted an action that would
- run on each push
- build a (minified) executable for both Windows and Linux
- let me download the resulting binaries, something the previous version wouldn't let me do
- and even create a release, when a tag is present.
I ended up using these:
Trigger: Push. This is the default.
on: [push]
.NET setup
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.100
When .NET is set up, I just run the build command twice - one step for each platform.
- name: Build for Linux
run: dotnet publish -r linux-x64 --configuration ReleaseComplete
- name: Build for Windows
run: dotnet publish -r win-x64 --configuration ReleaseComplete
Next, I need to upload the build artifacts, so that they're available for download from the Actions page.
I found on the marketplace that the action I want to use is actions/upload-artifact@v1.0.0
, so I specify it in the uses
entry.
I need to set the name
, which specifies how the file will be called in the Artifacts download menu, and the path
- which object will be uploaded under that name.
My case is really simple, since the entire project just builds into a single binary.
- name: Upload artifact - Windows
uses: actions/upload-artifact@v1.0.0
with:
name: Ghoplin.exe
path: Ghoplin/bin/ReleaseComplete/netcoreapp3.1/win-x64/publish/Ghoplin.exe
I do that analogously for Linux.
The last step is the release. GitHub won't let me create a release without an associated tag. Therefore I need the conditional if: startsWith(github.ref, 'refs/tags/')
. That makes sure this step only runs when a tag is pushed. So whenever I push a tag, this step makes a release, adds the required files and names it with the name of the tag.
Two notes: don't forget the pipe on the line saying files: |
. It tells YAML that what follows is a list of items.
And while the GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
line seems to be necessary, I did not have to do any extra steps to make it work and pass the appropriate secret.
- name: Make Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
Ghoplin/bin/ReleaseComplete/netcoreapp3.1/linux-x64/publish/Ghoplin
Ghoplin/bin/ReleaseComplete/netcoreapp3.1/win-x64/publish/Ghoplin.exe
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
This is the script in its entirety
name: .NET Core
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.100
- name: Build for Linux
run: dotnet publish -r linux-x64 --configuration ReleaseComplete
- name: Build for Windows
run: dotnet publish -r win-x64 --configuration ReleaseComplete
- name: Upload artifact - Linux
uses: actions/upload-artifact@v1.0.0
with:
name: Ghoplin
path: Ghoplin/bin/ReleaseComplete/netcoreapp3.1/linux-x64/publish/Ghoplin
- name: Upload artifact - Windows
uses: actions/upload-artifact@v1.0.0
with:
name: Ghoplin.exe
path: Ghoplin/bin/ReleaseComplete/netcoreapp3.1/win-x64/publish/Ghoplin.exe
- name: Make Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
Ghoplin/bin/ReleaseComplete/netcoreapp3.1/linux-x64/publish/Ghoplin
Ghoplin/bin/ReleaseComplete/netcoreapp3.1/win-x64/publish/Ghoplin.exe
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
I'm hoping add actions to other projects one of these days.