Deploying a static site using Publish and GitHub Actions

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:
      - master
    tags:
      - "*"

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 Content
        uses: peaceiris/actions-gh-pages@v2
        env:
          PERSONAL_TOKEN: ${{ secrets.DEPLOYMENT_TOKEN }}
          EXTERNAL_REPOSITORY: GITHUB_USER_NAME/GITHUB_USER_NAME.github.io
          PUBLISH_BRANCH: master
          PUBLISH_DIR: ./Output
        with:
          commitMessage: ${{ github.event.head_commit.message }}


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. Also DEPLOYMENT_TOKEN and GITHUB_USER_NAME need to be replaced, the latter one will be your GitHub Username and the deployment token needs to be created by you, and added to the specific repository where your GitHub actions is going to be run, so it'll be able to dpeloy the site to your personal, publicly available, repository @ GITHUB_USER_NAME.github.io.

Make sure a public repo exists

Please make sure you've got a repository at GITHUB_USER_NAME.github.io accessible so the GitHub Workflow can clone and push to it, and deploy your static site. If you don't please refer to to the help on GitHub Pages.

Deploying your site

First create a suitable deployment scret, please open to your GitHub Account settings and create a secre token, please make sure to not share this with anybody and keep it safe.

Next add it to the repository you'd like to deploy (your Publish repo) by going to it's settings/secrets. e.g. https://github.com/GITHUB_USER_NAME/MyWebSite/settings/secrets.

Once you've added your DEPLOYMENT_TOKEN there (you may pick any name you like, I've gone with this one in my case) you should be able to run the action and your site will be published and available under https://GITHUB_USER_NAME.github.io.

That's it!