This is the second post about Publish, the static website building tool by John Sundell.
I have already shared how to get started and today we will focus on publishing the website.
As today Publish does not include an official method to deploy to github pages but a PR is already open. Although we don’t have that support we can publish just using the github deploy method.
In order to deploy your blog in Github Pages you have to create a new repository where only the published blog will live. This means that if you already have a repo with your site source code, you will have to create a new one to host the static files.
Just create an empty Github repository and you are good to go.
On the previous post we created our own theme, but we didn’t focus much in the publish method. It may look like this right now:
try Blog().publish(
withTheme: .blog
)
This is just enough to generate a site that you can manually upload to whatever hosting you choose. If you want publish to deploy the site for you, you need to add the deployUsing argument. In our case we want to deploy using github so it will look like:
try Blog().publish(
withTheme: .blog,
deployedUsing: .gitHub("yourusername/yourrepo"),
)
Use the repo you have just created as the target repository and now you can just run:
publish deploy
Then Publish will deploy the generated files to your empty repo. You can use this command each time you want to deploy your site.
In order to activate your site, you have to enable Github Pages for the repository with your static files. To do that just go to your repository settings, scroll down to GitHub Pages and configure it. You can use a custom domain if you want to and choose master as the source (that is why you have a repo with site content only).
This will create a CNAME file in your repository telling GitHub the domains it can handle. Also keep in mind you will have to change your domain DNS CNAME registry to your Github page url.
If you prefer to use https with a custom domain you will have to use Cloudflare (A free CDN service) that will also improve your site speed. Please let me know if you are interested in a blog post about the Cloudflare setup.
Right now you should have your website running, available to the public and deployed using a one line command.
This is good enough for most people, but we can go one step further and make it automatic so a new website version will be deployed each time we commit changes to our blog repository.
We can do this for free using Github Actions. We will create a new Github Action workflow in the repo that hosts our blog source that will:
In order to create your first Github Action, go to the Actions tab in your source repo and tap New Workflow. You may choose the getting started Workflow and delete all the content.
Here you have the YAML file I use in my own repo:
name: Publish
on:
push:
branches: [ master ]
jobs:
build:
runs-on: macOS-latest
steps:
- uses: actions/checkout@v2
- name: Generate blog
run: swift run Blog
- name: Publish in github pages repo
uses: crazy-max/ghaction-github-pages@v1.4.0
with:
repo: youruser/yourrepository
target_branch: master
keep_history: true
allow_empty_commit: false
build_dir: Output
committer_name: yourname
commit_message: Update blog content
env:
GITHUB_PAT: ${{ secrets.REPO_TOKEN }}
- name: Add & Commit changes
uses: EndBug/add-and-commit@v4.0.1
with:
author_name: yourname
message: Commit new output
The last step to get this working is creating a Github token with access to your Github Page repository and adding it inside your source repository Secrets as REPO_TOKEN (you can find secrets inside your repository settings).
Now each time you commit to the master branch of your source repository Github will publish the content for you. Isn’t it awesome?
Automation feels like magic as you no longer have to go through Publish, but it also means that each time you commit your blogpost it will get published and all your readers can see it. To avoid this I created a drafts folder next to my blogposts. It is still possible to read them if people gets into the repository, but they are not available in the published website.
You can also use branches and even create pull requests if that makes sense to you.
I’m not going to lie, I added all this automation because I wanted to write my blogposts using my iPad and running publish deploy from the iPad is just not possible (as today 😛).
My setup is quite easy but works really well:
So the process when I want to start a new blog post is:
Once the draft is ready to be published I just move it to the posts folder, push my repository to remote and Github Actions takes care of publishing it.
I hope you have enjoyed reading this and it helps you improve your blogging flow so you can share more interesting content with all your readers. Please let me know if you want to know more about something or there is something you missed 🙂.