At work we've got a single Windows Server that hosts our websites using IIS. It has more than enough power to build our .NET projects alongside hosting and would free up time manually having to build projects and publish them to the IIS, whenever finished features are pushed to our master branch.
What I came up with was to use a GitHub Self-Hosted Actions Runner on the Windows Server, which would build, publish and restart the server automatically, whenever we push changes to the master branch.
Installing the Actions Runner instance
Navigate to your GitHub organization's settings page, then navigate to Actions > Runners
.
Then click on New Runner > New self-hosted runner
as seen below.
Choose Windows and follow the installations options on the page.
When running the configuration, you will be promptet to setup a Windows Service, choose yes. When asked what user should be running the service set it to NT AUTHORITY\SYSTEM
, this can be a huge security risk, only do this if you're aware of the risks. It is however required to be able to start and stop your website when publishing.
Actions configuration
Now that the Actions Runner service is setup and running, we'll have to configure our .NET repository with a GitHub action.
Start by creating a file in your repository at the following directory .github/workflows/my-dotnet-action.yml
you can change the name of the file to whatever you'd like.
Now let's edit the action like the following
name: My .NET Action
on:
push:
branches: [ master ]
jobs:
build:
runs-on: self-hosted
defaults:
run:
shell: cmd
steps:
- uses: actions/checkout@v3
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Stop example.com
run: appcmd stop sites "example.com"
- name: Publish Release
run: dotnet publish example.com --configuration Release -o C:\Path\To\Website\www\example.com
- name: Start example.com
run: appcmd start sites "example.com"
What this configuration does, is that it listens on the master branch, whenever changes are pushed it will start our self-hosted runner and begin the build process using cmd
as the preffered shell as I've previously had issues with using Windows Powershell and Powershell 7.
The build steps starts off by running a git checkout
followed by a restoring, building and testing the project. Once that's all done it will stop our website (in this case example.com) to ensure that the following publish will work without issues. At the very end we start the website again once again utilising appcmd.
Once you're done, just commit your GitHub action to master and watch it build and deploy your project. Hope it helped!