Homepage /Articles/Publish series/Deploy a Publish website in GitHub
Publish logo indicating Publish series number 2Nov 14, 2021

Deploy a Publish website in GitHub

This is part of Publish Posts Series.

Create the repositories

It's possible to just use one repository to act a the publisher and also the one that serves the pages. But in order to use a private repo for the publisher, this article will walk you through the creation of two repos, one for the publisher and another to host the html files.

Repo to host the website

Go to Github and create a repo as Public. Let's call it MyWebsite.

Activate Github Pages under Settings > Pages > Source select the branch, probably main, and then use /(root).

Make sure you are using HTTPS and assign your custom domain if you have one.

Repo to store your publisher

Go to Github and create a repo as Private. Let's call it MyWebsite-Publisher.

In this repo all the code, except for the Output folder, should be committed.

From now on, all the changes should only be committed in this repo. The other one will be automatically updated by the Github Action.

Setup Github Action to automatically publish new changes

First, you'll need create a Personal Access Token (PAT) with public_repo and repo_deployment rights. You'll find it under Settings > Developer Settings > Personal Access Tokens in your profile, not the repo.

Once you obtained the PAT go to MyWebsite-Publisher and add it as a secret named PAT_TOKEN. You'll find the secrets in Settings > Secrets.

Now you'll need to create the action. In order to do that on the root folder of MyWebsite-Publisher repo, create this folder tree .github/workflows/.

$ mkdir .github
$ cd .github
$ mkdir workflows
$ cd workdlows

Then in .github/workflows/ create a yaml file (let's name it deploy.yml) and include the following:

name: Deploy Github Pages

on:
  push:
    branches:
    - main

jobs:
  gh-pages:
    runs-on: ubuntu-latest
    container:
      image: "swift:5.4"
    steps:
    - name: Checkout 🛎️
      uses: actions/checkout@v1

    - name: Install Publish 📚
      run: |
        git clone https://github.com/JohnSundell/Publish.git
        cd Publish
        make

    # Needed for JamesIves/github-pages-deploy-action@4.1.5
    - name: Install rsync 📚 
      run: |
        apt-get update && apt-get install -y rsync

    - name: Generate Site 🏗
      run: |
        publish generate

    - name: Deploy 🚀
      uses: JamesIves/github-pages-deploy-action@4.1.5
      with:
        branch: main
        folder: ./Output
        target-folder: ./
        token: ${{ secrets.PAT_TOKEN }}
        repository-name: <YOUR_GITHUB_HANDLE>/MyWebsite

After that commit all the local changes and push it to Github repo. In the tab Actions will show that an action is running and when finished MyWebsite repo should be updated and deploying your website.