Deploying Saga (Swift) on Github pages
Sunntig,24 Oktoober, 2021

I have already covered how to deploy your Swift Saga site on Vercel and for windows affecionados on Azure but there is another simple and free option:Github pages

If you are looking for a simple way to host your static website done with made with Saga in Swift you are probably use Github to keep tracks of your changes anyway so having it deployed in Github pages is certainly convernient.

Step 1: Build and deploy Setup

Github pages doesn’t have as many options as other providers: for example you can’t set a deployment folder: it only works with gh-pages branch or, if you use main or master branch, it will only consider /root or /docs. In the default setup of Saga our deploy folder is /deploy so how to solve this issue ?

All you have to do is to create a simple github action under /.github/workflows as follow:

file github-pages.yml

name: Github Pages Build 🏗

on:
  push:
    branches:
    - main
    
jobs:
  gh-pages:
    runs-on: ubuntu-latest
    container:
      image: 'swift:5.5'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        
      - name: Build Swift Saga📱
        run: swift run
        
      - name: Deploy 🌍
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./deploy
          cname: www.YOURDOMAINNAME.COM
          publish_branch: gh-pages



All you need to change in the above file is the cname. This is needed to use your custom domain name. **note that with the default setting of saga this solution work bests only if you have a custom domain. If you plan to use the YOURNAME.github.io your might encounter some issues related to absolute paths ** this because your github url will be yourname.github.com/YOURREPOSITORY/index.html but some the generator presumes that your site is in the root folder.

Step 2: Set your custom domain name on Github

You can now set up the domain in your respository under Settings–>Pages

If you want your static site to be reasonably successfull you should have your own domain name this would ensure that you are not locked in by a particular provider or technology and you can always switch easily to another hosting. I would recommend Porkbun as they usually have a very good pricing and easy DNS setup.

So inside porkbun (or your domain registry) you need to set up 2 DNS zones as follow:

porkbun dns

if your registry doesn’t allow you to create ALIAS records you need to create an A zone for the root domain (@ or leave the sub domain blank) pointing to the github IP addresses

185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153

you can also see the Github guide here

I recommend you to set your domain with the www. prefix on github: in this way the root domain (without WWW) will be automatically redirected. Github also takes care of your SSL certificate.

Once the DNS is setup you should see the confirmation under Settings–>Pages that the SSL certificate has been created, you can force visitors to SSL and you are ready to go live! As soon as you push the Saga code in your repository your site will be deployed in github pages.

Is Github pages a good solution?

Indeed it is! There is a soft limit of 100GB traffic a month which is plenty for most websites. From my tests deployment is pretty fast (even faster than Azure in my test).It is not a sophisticated provided in terms of options (it was and still is designed to work with Jekyll. The price is FREE for a public repository but if you want to use Github pages for private repositories yo ucan do that with a pro account that is 4$ a month.