We already can use Travis-CI and Netlify to build and deploy Static Sites, Today we introduce a new way to automate and customize your workflows–GitHub Actions. But we’re just scratching the surface of what GitHub Actions can do. Once you get started, you’ll be able to build, package, release, update, and deploy your project in any language—on GitHub or any external system—without having to run code yourself.

Sign up for the limited public beta of GitHub Actions

Step-by-step Instructions

(1) Creat a new <username>.github.io repository

This’s the repository that will contain the fully rendered version of your Hugo website.For [User and Organization Pages sites] (<username>/<username>.github.io repository), we have to set master branch as our publishing branch. We should creat a new branch named ‘source’ to store all markdown codes for blog.

source branch

(2) Add SSH deploy key

Generate your deploy key with the following command.

1
2
3
4
ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f master -N ""
# You will get 2 files:
#   master.pub (public key)
#   master     (private key)

note: On Windows, we can use Git Bash, which is the Git command line too.

Next, Go to Repository Settings

  • Go to Deploy Keys and add your public key with the Allow write access
  • Go to Secrets and add your private key as ACTIONS_DEPLOY_KEY
Add your public keySuccess
Add your private keySuccess

(3) Create your workflow

Add your workflow setting YAML file .github/workflows/main.yml and push to the source branch. We can refer to the following project, and make some modifications.

⭐️ Repository type - Project

An example workflow for Hugo.

peaceiris/actions-hugo - GitHub

peaceiris/actions-hugo latest version peaceiris/actions-gh-pages latest version

Add a .github/workflows/main.yml file with content like the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
name: github pages

on:
  push:
    branches:
    - source

jobs:
  build-deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
      # with:
      #   submodules: true

    - name: Setup Hugo
      uses: peaceiris/[email protected]
      with:
        hugo-version: '0.58.3'

    - name: Build
      run: hugo --gc --minify

    - name: Deploy
      uses: peaceiris/[email protected]
      env:
        ACTIONS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
        PUBLISH_BRANCH: master
        PUBLISH_DIR: ./public
     #with:
     #  keepFiles: true

Note:

1.push on ‘source’ branch will trigger the workflow,fina/ly publish on ‘master’ branch.

2.If you want the action to add new files but leave existing ones untouched, set the optional parameter keepFiles to true.

That’s it. See the action.yml file for more information on the available configuration options.

(4) Build and Deployment

|

Summary

At this point, We have everything setup to automatically build and publish your site whenever you push to master. Happy blogging!