Automatically Deploy Hugo Blog to Remote Server Using GitHub Actions
Once all the content of the Hugo blog is ready, it can be deployed to a remote server. This article explains how to automate the deployment of a Hugo blog to a remote server using GitHub Actions.
For security reasons, it is not recommended to log in to the remote server using a password. Instead, you can use SSH keys to log in. Create a new user on the remote server, generate SSH keys for that user, and configure GitHub Actions to use those keys to log in to the remote server and deploy the Hugo blog.
Generate SSH Key Pair Locally
The SSH key pair generated locally is used for the new user to log in to the remote server. To generate an SSH key pair locally:
|
|
Follow the prompts to set the file save path (set to ~/.ssh/github-blog-deploy
) and passphrase (can be empty).
The generated files include:
- Public key file:
github-blog-deploy.pub
- Private key file:
github-blog-deploy
Configure SSH Public Key for New User on Remote Server
-
Log in to the remote server:
1
ssh root@SSH_IP -p SSH_PORT
-
Create a new user:
1
sudo adduser deployuser
-
Configure SSH login for the new user:
1 2 3 4 5
sudo mkdir /home/deployuser/.ssh sudo chmod 700 /home/deployuser/.ssh sudo touch /home/deployuser/.ssh/authorized_keys sudo chmod 600 /home/deployuser/.ssh/authorized_keys sudo chown -R deployuser:deployuser /home/deployuser/.ssh
-
Copy the locally generated public key to the remote server:
Add the contents of the
github-blog-deploy.pub
file to the/home/deployuser/.ssh/authorized_keys
file:1
cat ~/.ssh/github-blog-deploy.pub | ssh root@SSH_IP -p SSH_PORT 'cat >> /home/deployuser/.ssh/authorized_keys'
Or manually copy the public key content and paste it into the
authorized_keys
file:1
sudo nano /home/deployuser/.ssh/authorized_keys
Paste the content of the
github-blog-deploy.pub
file and save. -
Set directory permissions for the new user: Create the website deployment directory and set permissions:
1
sudo chown -R deployuser:deployuser /var/www/public
Update GitHub Actions Configuration File
-
Add the private key to GitHub Secrets:
- Log in to GitHub and go to the repository where the Hugo blog is located.
- Click
Settings
->Secrets and variables
->Actions
->New repository secret
. - Add a new Secret,
SSH_PRIVATE_KEY
, with the value being the content of thegithub-blog-deploy
file (private key). Add the three SecretsSSH_PRIVATE_KEY
,SSH_IP
, andSSH_PORT
according to the actual situation.
-
Update the GitHub Actions configuration file:
Add the following file content:
|
|