Simple GitHub actions reusable workflow using .NET 6

Georgi Kamacharov
3 min readMar 29, 2022

If you’re like me and wanted to get more into GitHub actions, and ever wondered if you can reuse a workflow, then I hope this post helps you. One of the best benefits of GitHub Actions’ reusable workflows is avoiding duplication. And that is exactly what I wanted to do.

In this short post and example, I will show you how you can define a simple base workflow for building a .NET 6 project — complete with dotnet restore, build, test and publish commands. This base workflow can then be used to build and (optionally) test and publish any of your .NET 6 projects.

NOTE: this is not specific to .NET 6 and it can be applied to lower versions as well. Just take a look at the dotnet-version on line 29 and update it accordingly.

build-base.yml file

As you can see, the important part in this definition is the trigger on: workflow_call . This makes it so this workflow can only be called manually with the specified inputs. Then, the inputs are used in the steps below to restore, build and then optionally test and publish the code.

Then you can define your calling workflow as follows.

build-core.yml file

As you can see, the calling line is line 11 — uses ./.github/workflows/build-base.yml . Where build-base.yml here is our definition from above. After you specify that, then you need to specify the input parameters under the with section. And that’s it. You can then specify as many other .yml files that call the base workflow and reuse it.

Finally, from the pipeline point of view, this is what it would look like.

GitHub actions run
GitHub actions run

And that’s the whole thing. Please note that this is just a simple example of how you can use reusable GitHub Actions workflows. There are a lot of other use cases and benefits to it. I strongly encourage you to read the full GitHub documentation on it and see how you can apply it to your use case. I only hope that this post can help you get a better understanding of how you can use them, in the simplest way possible, and build them out further from there.

--

--