Publish your Netlify site with Sourcehut builds

2020-05-27 on adnano.co

Sourcehut is a great development tool. Unfortunately, Sourcehut does not yet provide static website hosting. Until they do, the second-best option for static website hosting is Netlify.

Update (2021-02-21): Sourcehut now provides static website hosting.

Netlify only offers easy integration with GitHub, GitLab, and Bitbucket. However, we can leverage Sourcehut's builds service to automatically build and publish our website on Netlify.

1. Create a Netlify access token

Go to your Netlify settings by clicking on your profile photo. Press Applications and create a new personal access token.

2. Add the access token to your Sourcehut builds secrets

Go to Sourcehut builds. Press Manage Secrets, then add a new secret. Paste in your Netlify access token. Choose the File secret type, then choose "~/.netlify_token" as the file path.

3. Add a build manifest to your repository

This build manifest builds the site with Hugo.

# .build.yml
image: alpine/edge
packages:
- curl
- hugo
- zip
secrets:
# The id of your Netlify token build secret
- netlify-token-secret-id
sources:
# The repository of your site
- https://git.sr.ht/~user/site
tasks:
# Build the site
# Put instructions for building your site here
- build: |
    cd site
    hugo    
# Zip up the site before uploading
- zip: |
        zip -r website.zip site/public
# Upload the site zip with the Netlify API
# Make sure to replace site.netlify.com with your site subdomain
- upload: |
    curl -f \
      -H "Content-Type: application/zip" \
      -H "Authorization: Bearer $(cat .netlify_token)" \
      --data-binary "@website.zip" \
      https://api.netlify.com/api/v1/sites/site.netlify.com/deploys    

Push your changes and your site should start building automatically.