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:
- in
Hexo library
Write blog posts (github.com/yuliji/blog). - with
hexo generate
Generate static files. - Copy the static file to
Pages library
(Github.com/yuliji/yuli…). - in
Pages library
In thecommit
andpush
Code, 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 library
Create two Github Actions workflows:Build Pages
andPublish Pages
.Build Pages
bymaster
The branchpush
The event is triggered. His main function is to usehexo generate
Generate static files, package them, create them, and upload a new oneGithub ReleaseIn the water.- The action to create a release is triggered
Publish Pages
This workflow will clone mePages library
Download the static file package from the latest release and unzip it toPages library
Commit & push.
The specific steps are as follows:
- create
build.sh
The script. This script is responsible for generating static files, packaging, and uploading. The script also runs on your local computer. - create
gh_action_build.sh
The script. The script is run in the Github Action. It’s basically callingbuild.sh
, but install the necessary NPM libraries first. - inGithub.com/settings/to…Get a Personal Access token. Because of our
build.sh
Github CLI toolgh
. This command requires the token for authorization. - Add the Personal Access token to
Hexo library
Secrets. - Create YAML file definitions
Build 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
- create
publish
The script. The script downloads the latest static file package in Release and unzip it toPages library
Commit & push. - with
ssh-keygen
Create a pair of secret keys. Because we areHexo library
In actionsPages library
So you need to authorize the actions git command to do thisPages library
. - Add the public key generated above to
Pages library
“And grant write permission to the deploy key directory. - Add the private key generated above to
Hexo library
Secrets. - Create YAML file definitions
Publish Pages
. Clone two libraries in this Workflow and call thempublish
The script. Among themPages library
SSH 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.