GitHub Actions is GitHub’s continuous integration service.

We can think of an Action as a short function-specific script, and multiple actions can form a Workflow that is triggered by various GitHub events. We can use GitHub Actions for continuous integration (CI) and continuous deployment (CD) of software.

In this post, I show you how I used GitHub Actions to automate publishing my Hexo blog.

Hexo and making Pages

My personal blog, Dao eleven’s DevOps site, is Hexo generated. People who want to learn about Hexo can go to Hexo’s website.

After Hexo generates static files, I submit them to the GitHub Pages repository. This allows you to have a free personal blog using GitHub Pages.

Previously, I had been manually publishing my blog on my computer, and the process was as follows:

  • inHexo libraryWrite blog posts (github.com/yuliji/blog).
  • withhexo generateGenerate static files.
  • Copy the static file toPages library (Github.com/yuliji/yuli…).
  • inPages libraryIn thecommitandpushCode, release.

When I read about Github Actions, I got the idea of using Github Actions to automatically publish blogs.

Automatically publish your blog with GitHub Actions

My design is that all the code is in Hexo.

  • Hexo libraryCreate two Github Actions workflows:Build PagesandPublish Pages.
  • Build PagesbymasterThe branchpushThe event is triggered. His main function is to usehexo generateGenerate static files, package them, create them, and upload a new oneGithub ReleaseIn the water.
  • The action to create a release is triggeredPublish PagesThis workflow will clone mePages libraryDownload the static file package from the latest release and unzip it toPages libraryCommit & push.

The specific steps are as follows:

  1. createbuild.shThe script. This script is responsible for generating static files, packaging, and uploading. The script also runs on your local computer.
  2. creategh_action_build.shThe script. The script is run in the Github Action. It’s basically callingbuild.sh, but install the necessary NPM libraries first.
  3. inGithub.com/settings/to…Get a Personal Access token. Because of ourbuild.shGithub CLI toolgh. This command requires the token for authorization.
  4. Add the Personal Access token toHexo librarySecrets.
  5. Create YAML file definitionsBuild Pages, the main function here is to inject the above token and callgh_action_build.sh. Complete code inhere.
    - name: Generate pages
      env:
        GITHUB_TOKEN: The ${{ secrets.TOKEN }} # injection token
    
      run: ${GITHUB_WORKSPACE}/bin/gh_action_build.sh # call script
    Copy the code
  6. createpublishThe script. The script downloads the latest static file package in Release and unzip it toPages libraryCommit & push.
  7. withssh-keygenCreate a pair of secret keys. Because we areHexo libraryIn actionsPages librarySo you need to authorize the actions git command to do thisPages library.
  8. Add the public key generated above toPages library“And grant write permission to the deploy key directory.
  9. Add the private key generated above toHexo librarySecrets.
  10. Create YAML file definitionsPublish Pages. Clone two libraries in this Workflow and call thempublishThe script. Among themPages librarySSH key requires the private key set above. Complete code inhere.
    - uses: actions/checkout@v2
      with:
        repository: 'yuliji/yuliji.github.io'
        path: 'pages'
        ssh-key: The ${{ secrets.PAGE_REPO_SSH_KEY }}  Create Pages library with write permission key
    Copy the code

complete

That way, ALL I had to do was submit the latest article to the Master branch of the Hexo library, and the new article was automatically published to Github Pages.