Marcus Kida's Blog

Hey! 👋 I'm Marcus, a Freelance Senior iOS Developer 📱 CTO-as-a-Service and CEO @Bearologics. I'm solving problems through code. Want to hire me? 👉 Let's talk!

Prerequisite

We'll assume you know how to use Xcode to work with Swift packages and how to push code to GitHub, other than that you should learn all the required skills, to publish your own static website, in this post.

To get up and running with your own static site please follow the instructions on how to install Publish here.

Once done with this let's spin up a site using

$ mkdir MyWebsite
$ cd MyWebsite
$ publish new
$ open Package.swift

You'll now be greated by Xcode and able to add some content to your site and adjust your template, going deeper into details on how to customize the site or add content would exceed this post, so we're going to jump right onto deploying your site.

To deploy your site using GitHub Actions you're going to need a Workflow, let's get going with some that'll build your site and deploy it to your personal GitHub User.

Adding a GitHub Workflow

Please take a look at the following GitHub Workflow:

name: Build and Deploy MyWebsite

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout Source
        uses: actions/checkout@v1
      - name: Build Site
        run: swift run MyWebsite
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
            github_token: ${{ secrets.GITHUB_TOKEN }}
            publish_dir: ./Output

This code needs to go into your repository at .github/workflows/deploy.yml.

There's a few things you'd need to adust, please replace MyWebsite by the name you've picked when creating your site in the first place. GITHUB_TOKEN will be automatically populated.

Deploying your site

Please create a repository named GITHUB_USER_NAME.github.io, commit the code with your deploy.yml and, once pushed, this will deploy the build artifacts from ./Output to a gh-pages branch which will then be accessible at GITHUB_USER_NAME.github.io.

That's it!

Using your own custom domain

If you'd like to use your own custom domain, you need a CNAME file containing this domain, e.g. my-website.example.com.

To achieve this, just add the following property to the Deploy step of your deploy.yml:

       - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
            github_token: ${{ secrets.GITHUB_TOKEN }}
            publish_dir: ./Output
            cname: my-website.example.com

Then set up a CNAME record pointing from your my-website subdomain to GITHUB_USER_NAME.github.io, e.g.:

my-website 10800 IN CNAME GITHUB_USER_NAME.github.io.
Tagged with: