Re: Hexo builds a personal blog from scratch (3)

This chapter is about how to remotely deploy local personal projects to GitHub Pages, involving GitHub's project repository, the use of Git, and Hexo's remote deployment.

1. Install hexo-deployer-gitthe plugin

To deploy a Hexo project to GitHub, you need to install a plugin first. Open a command window in the root directory of the Hexo project and enter:

npm install hexo-deployer-git --save

2. Create a repository on GitHub

The name of the repository can be arbitrarily named, but this repository is used as our blog repository, so try to name it in the form of {username}.github.io.

For example, if my GitHub username is lewky, I will name this repository lewky.github.io. (Why it is named like this, it will be explained later)

3. Modify the local project configuration file

Find the following in _config.yml:

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type:

Configure the GitHub repository link we just created:

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: [email protected]:/{user}/{repository}.git
  branch: master
  message:

Please note that if the warehouse address here is written as: https://github.com/{user}/{repository}.gitit may not be successful in the subsequent deployment, and needs to be https://github.comchanged to [email protected]:.

In addition, the branch and message here can be left blank, the branch will be the master branch by default, and the message will use the following format template by default:

Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}

4. SSH key creation and configuration

The most critical step is here, we need to generate a pair of key pairs, and then configure the public key to the GitHub account.

4.1 Generate RSA key pair

First use Git Bash to enter:

cd ~/.ssh

~Refers to the root directory of the current user, that is C:\Users\{user}\; and the .sshdirectory generally stores the public SSH key file:

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub

In addition, there is a known_hostsfile in which SSH records the public key of each computer we have accessed.

If the path can be found cd ~/.sshafter , copy the contents of the id_rsa.pub file in that directory to the clipboard. If the path is not found, execute the command:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

This command will generate a new SSH key, the meaning of the parameters here:

-t: type,生成的密钥类型
-b: bits,指定密钥长度,对于RSA密钥,最小要求768位,默认是2048位。DSA密钥必须恰好是1024位,一般越长越安全。
-C: comment,提供一个新注释

Then you will see the following prompt:

Enter file in which to save the key (/c/Users/123/.ssh/id_rsa):

Press Enter here to save the SSH key to the default address, namely:C:\Users\{user}\

If an RSA private key already exists, you will be prompted:

/c/Users/123/.ssh/id_rsa already exists.
Overwrite (y/n)?

Enter y here to regenerate the RSA key pair; then you will see the following prompt:

Enter passphrase (empty for no passphrase):

Press Enter here, indicating that no password is set; then you will be prompted to enter a duplicate password, still press Enter.

Enter same passphrase again:

At this time, our SSH key is generated, go to the ~/.sshdirectory and copy the contents of the id_rsa.pub file inside to the clipboard.

4.2 Configuring SSH keys on GitHub

Then log in to our GitHub account:

  • Go to Settings page
  • Select SSH and GPG keys
  • Click on New SSH key
  • Fill in the Title (used to give the public key a name to distinguish it from other public keys)
  • Then copy the public key we just copied into the Key
  • Finally, click Add SSH key, then GitHub will ask you to enter the account password for confirmation.

4.3 Verify ssh connection

Using Git Bash enter:

Then you will see:

The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

Enter yes and you will see:

Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
Hi lewky! You've successfully authenticated, but GitHub does not provide shell access.

At this time, the public key of github.com is saved in the known_hosts file. If we execute it again ssh -T [email protected], we don't need to enter yes, we will see directly:

Hi lewky! You've successfully authenticated, but GitHub does not provide shell access.

4.4 Deploying to GitHub Pages

input the command:

hexo d
或者
hexo g -d

The latter command means to generate a static page and deploy it to a remote warehouse. The first deployment will take a little longer. After the deployment is successful, you will see:

 * [new branch]      HEAD -> master
INFO  Deploy done: git

Then log in to GitHub and enter our project repository, you can see that there are many more files, and their messages are in the default format:

Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}

Next, click Settings to enter the settings page of the repository, find the Github Pages item, select the Master branch as the source, and save it; then the repository will be deployed to https://{username}.github.io/{ repository name} .

If you want to access your blog directly through https://{username}.github.io/ , you can change the repository name to {username}.github.io; this way you don't need to add the repository name to the url to access .

Next, start enjoying your personal blog :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324906865&siteId=291194637