Preface
Hugo is one of the popular open source static website generators, which is used to generate final static pages to be placed on a web server for deployment.
Typically, the workflow might be something like this: write the article locally -> build the page locally -> upload the public folder to the cloud server. This step is a bit tedious, and this article simplifies the build and upload steps.
Here, we introduce how to automatically build static pages based on Github Actions and publish them to cloud servers to automatically update the content of the pages.
This article is based on the CaiJimmy/hugo-theme-stack-starter template of the Stack theme, which saves us a lot of work because it comes with Github Actions, we only need to modify the .github/workflows/deploy.yml
file in the template to automate publishing to the cloud server.
Pre-requisites:
- Linux-based cloud server
- Have created your own repository based on CaiJimmy/hugo-theme-stack-starter.
- Know Git commands
Of course, other themes will have similar quick-start templates, much the same.
Steps to implement
Modify the .github/workflows/deploy.yml
file:
|
|
The code is well commented, so I’ll expand on it here. If you’re familiar with Github Actions, it should be pretty easy to understand. If you’re not, it’s okay, just copy it.
The focus is on the last step:
|
|
Use the burnett 01/rsync-deployments
workflow, which uses Linux’s rsync command to synchronize files to the corresponding cloud server.
remote_path
is the directory where the site is mounted on the cloud server, e.g. I’m using nginx here, and the mount directory is/home/nginx/html/
.remote_host
is the public IP of the cloud server.remote_port
is the ssh login port, the default is 22, if you have changed it, just fill in the corresponding port.remote_user
is the username of the cloud server.remote_key
is the key for the cloud server, this should be noted, not the ssh login password, but the key created through the cloud server vendor’s console. For example, I’m using Aliyun’s server here, create one on the cloud server ECS page, Network & Security - Key Pairs, and you’ll get axxxx. pem
file, save this file, you’ll need it later.
Then, go to the GitHub blog repository and add the secret key in the Settings - Secrets and variables - Actions interface.
Create the secret:
- DEPLOY_HOST for the public IP.
- DEPLOY_PORT for the ssh port
- DEPLOY_USER for the username.
- DEPLOY_KEY with the contents of the xxx. pem file.
After that, log in to your cloud server and check if rsync is installed and if it is running:
|
|
Active: inactive (dead)
means it’s not started, we set it to boot automatically:
|
|
Then use systemctl status rsync
to see:
|
|
After adding the line Condition: start condition failed at Thu 2023-11-30 23:29:42 CST; 3 days ago
, it works!
Verify
|
|
After the git trilogy head over to GitHub to see the results!
Also, check if your blog has been updated.
Summary
In this article, we have introduced the method of Github Action to automatically build and publish to cloud servers, of course, you can also publish directly to GitHub Pages, since this is simpler, you can directly refer to Automatically Build Hugo Blog Based on Github Action.