Github Actions automatically deploys hugo to Github Pages
Preface
I recently planned to upgrade the theme of my blog. Since I was playing around with Github Actions, I planned to use it to implement CICD. After writing and submitting the article, the static files generated by hugo will be automatically deployed to Github pages, Netlify, Vercel, Cloud flare Pages and other third-party platforms. Today, I will record the automatic deployment process of Github pages, which will save a lot of trouble.
Since the Hugo static website generator used by the website involves sensitive configuration, I will put the source code of Hugo in a private warehouse, and then put the front-end static pages generated by Hugo in the Github public warehouse. In this way, we need to prepare two Github warehouses, and then implement CICD through github actions in the private warehouse.
Github Actions official documentation: https://docs.github.com/zh/actions
Github Pages official standard: https://pages.github.com/
Github Token configuration
Since it involves deployment, authentication will be used in this process, and Github provides three token authentication methods:
- github_token
- deploy_key
- personal_token
In fact, in each workflow, GitHub will automatically create a unique GITHUB_TOKEN key for workflow authentication. GITHUB_TOKEN will expire when the job is completed or up to 24 hours later. This is a temporary authentication strategy.
Note: Since we are here involving cross-warehouse deployment, github_token cannot be used.
For more github_token, please refer to: https://docs.github.com/actions/security-guides/automatic-token-authentication
personal_token is mainly used to generate tokens with personal accounts.
https://github.com/settings/tokens
For deploy_token, you mainly generate a key pair yourself, attach the public key to the github repository, and store the private key yourself.
For more deploy_token references: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/managing-deploy-keys
Here we use the deploy_token method for authentication, attaching the public key to the deploy-token of the public repository and the private key to the secret of the private repository.
Create deploy_token
Public repository wzdushu/wzdushu.github.io configures Deploy keys and sets the content of gh-pages-wzdushu.pub.
Create a secret in a private repository, specify the name: ACTIONS_DEPLOY_KEY The content is gh-pages-wzdushu
Github Actions configuration
Create a workflow in a private repository, my path here is: .github/workflows/cicd.yml, edit this file as follows:
|
|
on There are three triggering methods here:
- push: execute when git push is pushed.
- workflow_dispatch: manually execute the actions of the project repository.
- schedule: scheduled execution
Since the new version of the hugo theme here depends on Sass, it needs to be installed here, which is not required for everyone.
The deployment here does not use the official actions, but uses the open source peaceiris/actions-gh-pages, mainly because this repository is more friendly to Hugo, MkDocs, Gatsby, mdBook, Next, Nuxt, etc.
For other configurations, please refer to the official documentation of github actions, and will not be explained one by one.
Execute deployment
Later, we happily submitted the article to Github as usual, triggered GitHub Actions to generate static files and pushed them to GitHub Pages, and spent the rest of the time drinking a cup of coffee and waiting for it to finish its work.
Summary: The function of Github actions to automatically deploy is not limited to deploying static pages today. For actual projects and production environments, applications are often deployed to k8s clusters or some other PAAS platforms, such as Alibaba Cloud ACK, AWS’s EKS, Azure ’s AKS, etc. Of course, Github Actions also has other functions similar to Jenkins or Gitlab CI.