Azure DevOps yaml files for piplines can be created as templates to avoid duplicating same set of tasks in multiple areas of the pipelines. For GitHub actions workflow, we have seen here in "Build and Unit Tests for .NET Apps with GitHub Actions" the same steps are repeated in both Windows and Linux, build and unit test run. The solution to avoid duplicating same steps in multiple jobs in a GitHub actions workflow is the usage of composite actions. Let's modify our workflow implemented in "Build and Unit Tests for .NET Apps with GitHub Actions" to use composite action to build and unit test, and reuse that in both Windows and Linux job.
Defining the composite action
Composite action must be created in a yaml file named as action.yml. The file should be located in a path such as below.
.github/actions/action_name_folder/action.yml
For our build and unit test action let's use path as .github/actions/build_and_unittest/action.yml Lets define the build, unit test and publish test result steps in our action. But first we need one input paramter defined, to take input whether we are building on Linux or Windows platform to publish our test results accordingly. We can also define outputs from composite actions which we can dicuss in a next post.
The input can be defined as shown below for a composite action. Here we define build_os as our input to the composite action. Then we have to specify the action should run using composite. Then we can define the steps we need to execute in the action. The full composite action yaml for build and unit test can be found here in GitHub.
The input can be consumed in steps as shown below. Syntax is somewhat similar to Azure DevOps pipelines yaml template parameter usage.
Using the composite action
We can then consume the composite action we created in the GitHub action wokflow as sown below in multiple jobs. The build and unit test composite action is used in both Windows and Linux jobs, and notice how the input parameter is passed from the job to the composite action. The full workflow code utilizing the composite action is available in here in GitHub..
No comments:
Post a Comment