January 12th, 2021

Generate a GitHub Actions workflow with Visual Studio or the dotnet CLI

Damian Brady
Senior Cloud Advocate

It can feel like a lot to get started with GitHub Actions or Azure Pipelines – it’s a whole new skillset. Because the alternatives for deployment are so easy (right-click publish for example), teams sometimes question whether implementing a pipeline is worth the effort. It might be agreed that CI/CD is a “good thing”, but is it the most important thing right now?

Even as a “seasoned builder-of-pipelines”, I often spend a lot of time with a failing build before it finally “goes green” and I can move on!

Failed builds are common before you get things working

Making it easy in Visual Studio

When I joined the Cloud Advocacy team at Microsoft as a DevOps specialist, one of my advocacy teammates was Tim Heuer. We had some… robust… discussions about how important a full end-to-end DevOps pipeline was. I’ve always been on the side of building a full pipeline from day 1, but especially for small projects, he was less convinced. Right-click publish worked fine for him! The learning curve was too steep for what seemed a small benefit.

Fast-forward to today, and Tim is on the .NET team at Microsoft. One big focus of his is flattening that learning curve. His team (as well as many other teams at Microsoft) have been working hard to make it as simple as possible for .NET developer to build CI/CD pipelines. They want people to be successful in a way that gives them all the benefits of DevOps!

Back in September 2020, the Visual Studio team announced a new feature for ASP.NET Core projects. Essentially, it allowed Visual Studio users to right-click-publish, but rather than just pushing to prod, they’d be able to generate a GitHub Actions workflow!

GitHub Actions from Visual Studio

But .NET is a true cross-platform framework now, and not everyone has (or needs) Visual Studio installed. Personally, I haven’t had Visual Studio installed on my primary machine for over a year! So while this is a great feature, it’s not one I use, and it doesn’t work for an increasing number of .NET developers.

dotnet new workflow

I recently came across a tweet from Tim where he submitted a pull request to a project to add a CI pipeline (definitely not the Tim I first met!) For people familiar with GitHub Actions, it was a fairly straightforward addition, but if not, that learning curve is steep! More impressive than the PR is the fact Tim’s starting point was a generated workflow using the dotnet CLI and a template he wrote!

Den Delimarsky thanking Tim Heuer for his PR

How do I use it?

The template is fairly simple – it just creates a GitHub Actions workflow file in the correct location (assuming you run it in the root of your repository) that will build and test your .NET project. While it doesn’t do a lot on the surface, it’s perfect to get you over that steep learning curve!

Tim blogged about it in more detail, but here are the basics:

To use it, navigate to the root of your repo and type dotnet new workflow in your console of choice.

dotnet new workflow with instructions to install

If you don’t have the template installed already, you get clear instructions on how to install it. It’s a single command – dotnet new -i TimHeuer.GitHubActions.Templates::1.0.4 – but the version may be different. Run that command and then dotnet new workflow again.

dotnet new workflow

What you’ll get is a straightforward GitHub Actions workflow to build and test your .NET application. Keep in mind this is a starting point. It’s not going to do everything you might want, but it’s a solid base to build from.

Your new generated workflow

At time of writing there are two additional arguments you can specify: – -sdk or --sdk-version lets you choose the .NET SDK – -b or --branch lets you choose the branch that will trigger your workflow

Two arguments for dotnet new workflow

More for .NET devs

These are just two of the blog posts Tim has written lately on using GitHub Actions for .NET. Check them out on his blog!

Category
CI/CDDevOps

Author

Damian Brady
Senior Cloud Advocate

Damian is a Cloud Advocate specializing in DevOps and MLOps. After spending a year in Toronto, Canada, he returned to Australia - the land of the dangerous creatures and beautiful beaches - in 2018. Formerly a dev at Octopus Deploy and a Microsoft MVP, he has a background in software development and consulting in a broad range of industries. In Australia, he co-organised the Brisbane .Net User Group, and launched the now annual DDD Brisbane conference. He regularly speaks at conferences, ...

More about author

0 comments

Discussion are closed.