Deploying a static website with Netlify and Github Actions

Where I automate the deployment of my blog
Published on Tuesday, 20 August 2024

Deploying a static website with Netlify and Github Actions

I thought it would be fun to share my setup for building and deploying my blog. As mentioned in this other post, my blog is a static website generated using Statiq. It is hosted on Netlify, which has a lot of turn-key configs for setting up your site's deployment needs. It doesn't support dotnet projects though.

So instead of leaning on an integration from Netlify, I decided to use Github Actions to build and deploy my site. This way, I can have a bit more control over the build process, and I can make sure that the build is done using the same version of the .NET SDK that I use locally. Maybe you want to try something similar, so I'll share my setup here.

The full config looks like this, but I'll break it down for you below:

name: Deploy

on:
  push:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true
      - name: Setup .NET Core SDK 8.0.x
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 8.0.x
      - name: Install Netlify CLI
        run: npm install netlify-cli -g
      - name: Install dependencies
        run: dotnet restore
      - name: Build
        run: dotnet build --configuration Release --no-restore
      - name: Run
        run: cd Blog && dotnet run
      - name: Deploy to netlify
        run: cd Blog && netlify deploy --dir=output --prod
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}

Alright, so firstly, I only run it on main, which makes sure that I only ever deploy what is in the main branch. This allows me to have experimental branches that won't go to production

name: Deploy

on:
  push:
    branches: [ "main" ]

Next, I get all of the requirements set up:

  • I check out the branch
  • I set up the .NET SDK
  • I install the Netlify CLI

The Netlify CLI is what I'm using to push my web assets to Netlify after building.

      - uses: actions/checkout@v3
        with:
          submodules: true
      - name: Setup .NET Core SDK 8.0.x
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 8.0.x
      - name: Install Netlify CLI
        run: npm install netlify-cli -g
      - name: Install dependencies
        run: dotnet restore

Finally, I build the app, run the script that generates all of the static assets, and deploy it to Netlify.

The only funny business here is that I need to bring in my Netlify CLI auth token and the site ID, so that I can authenticate with the Netlify API and deploy to the right site. To securely use these, I am making use of Github Actions runner secrets, so that I don't have to expose these in my repository.

      - name: Build
        run: dotnet build --configuration Release --no-restore
      - name: Run
        run: cd Blog && dotnet run
      - name: Deploy to netlify
        run: cd Blog && netlify deploy --dir=output --prod
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}

That's all really. The Netlify CLI will deploy all the files in my output directory and refresh whatever caching they have set up, and everything is live immediately. In addition, the RSS feed will also be rebuild and deployed along with the rest of the static sites, so anyone consuming the website through RSS will get the latest updates as well.



Blog Logo

Hi! thanks for dropping in to pick my brain

I write on this blog for my own benefit. I write because I like to, and because it helps me process topics. It is also my own little home on the web and a place for me to experiment.

Since you're here though, I'd love to hear your thoughts on what you've read here, so please leave a comment below. Also, if you like what you read and want to give a small tip, fell free to:

Buy Me A Coffee