阅读(4503) (0)

Hexo 一键部署

2020-10-14 14:15:32 更新
  1. 安装 hexo-deployer-git
$ npm install hexo-deployer-git --save
  1. 修改配置。
deploy:
type: git
repo: <repository url> #https://bitbucket.org/JohnSmith/johnsmith.bitbucket.io
branch: [branch]
message: [message]
参数 描述 默认
repo 库(Repository)地址
branch 分支名称 gh-pages (GitHub) coding-pages (Coding.net) master (others)
message 自定义提交信息 Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }})
token Optional token value to authenticate with the repo. Prefix with $ to read token from environment variable
  1. 生成站点文件并推送至远程库。执行 hexo clean && hexo deployYou will be prompted with username and password of the target repository, unless you authenticate with a token or ssh key.hexo-deployer-git does not store your username and password. Use git-credential-cache to store them temporarily.
  2. 登入 Github/BitBucket/Gitlab,请在库设置(Repository Settings)中将默认分支设置为_config.yml配置中的分支名称。稍等片刻,您的站点就会显示在您的 Github Pages 中。

这一切是如何发生的?

当执行 hexo deploy 时,Hexo 会将 public 目录中的文件和目录推送至 _config.yml 中指定的远端仓库和分支中,并且完全覆盖该分支下的已有内容。

For 使用 Git 管理站点目录的用户由于 Hexo 的部署默认使用分支 master,所以如果你同时正在使用 Git 管理你的站点目录,你应当注意你的部署分支应当不同于写作分支。 一个好的实践是将站点目录和 Pages 分别存放在两个不同的 Git 仓库中,可以有效避免相互覆盖。 Hexo 在部署你的站点生成的文件时并不会更新你的站点目录。因此你应该手动提交并推送你的写作分支。

此外,如果您的 Github Pages 需要使用 CNAME 文件自定义域名,请将 CNAME 文件置于 source 目录下,只有这样 hexo deploy 才能将 CNAME 文件一并推送至部署分支。

Heroku

安装 hexo-deployer-heroku

$ npm install hexo-deployer-heroku --save

修改配置。

deploy:
type: heroku
repo: <repository url>
message: [message]
参数 描述
repo Heroku 库(Repository)地址
message 自定提交信息 (默认为 Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }})

Netlify

Netlify 是一个提供网络托管的综合平台。它集持续集成(CI)CDN 自定义域名 HTTPS 持续部署(CD)等诸多功能于一身。您可以通过以下两种方式将 Hexo 站点部署到 Netlify。

首先,也是最通用的方式,就是使用 Netlify 提供的网页端用户界面。前往新建一个网站页面,选择需要关联的 Github/BitBucket/Gitlab 库,然后遵循网站提示。

另一种方式是使用 Netlify 提供的命令行客户端工具 Node based CLI 管理和部署您的站点。

此外,您还可以在项目的README中增加一个 部署至Netlify按钮,这样其他用户在 fork 或 clone 了您的项目之后可以方便快捷地一键部署。

Rsync

安装 hexo-deployer-rsync

$ npm install hexo-deployer-rsync --save

修改配置。

deploy:
type: rsync
host: <host>
user: <user>
root: <root>
port: [port]
delete: [true|false]
verbose: [true|false]
ignore_errors: [true|false]
参数 描述 默认值
host 远程主机的地址
user 使用者名称
root 远程主机的根目录
port 端口 22
delete 删除远程主机上的旧文件 true
verbose 显示调试信息 true
ignore_errors 忽略错误 false
rsync 部署模块的工作方式需要注意的是,要求您提供的实际上是一个能通过 SSH 登陆远程主机的 Linux 用户。Hexo 会自动处理关于rsync 使用的一切操作。因此,您需要在远程主机上为您的 Hexo 站点建立一个用户,并允许其通过 SSH 登陆。不过,这里的port,的确是指 rsync 监听的端口,请确保防火墙打开了该端口。

OpenShift

安装 hexo-deployer-openshift

$ npm install hexo-deployer-openshift --save

修改配置。

deploy:
type: openshift
repo: <repository url>
message: [message]
参数 描述
repo OpenShift 库(Repository)地址
message 自定提交信息 (默认为 Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }})

FTPSync

安装 hexo-deployer-ftpsync

$ npm install hexo-deployer-ftpsync --save

修改配置。

deploy:
type: ftpsync
host: <host>
user: <user>
pass: <password>
remote: [remote]
port: [port]
ignore: [ignore]
connections: [connections]
verbose: [true|false]
参数 描述 默认值
host 远程主机的地址
user 使用者名称
pass 密码
remote 远程主机的根目录 /
port 端口 21
ignore 忽略的文件或目录
connections 使用的连接数 1
verbose 显示调试信息 false
FTP 部署可能出现的问题您可能需要预先通过其他方式将所有文件上传到远程主机中。否则初次使用 ftpsync 插件就可能出现报错。另外,由于 FTP 协议的特征,它每传送一个文件就需要一次握手,相对速度较慢。

SFTP

安装 hexo-deployer-sftp

$ npm install hexo-deployer-sftp --save

修改配置。

deploy:
type: sftp
host: <host>
user: <user>
pass: <password>
remotePath: [remote path]
port: [port]
privateKey: [path/to/privateKey]
passphrase: [passphrase]
agent: [path/to/agent/socket]
参数 描述 默认值
host 远程主机的地址
user 使用者名称
pass 密码
remotePath 远程主机的根目录 /
port 端口 22
privateKey ssh私钥的目录地址
passphrase (可省略)ssh私钥的密码短语
agent ssh套接字的目录地址 $SSH_AUTH_SOCK

Vercel

Vercel is a cloud platform that enables developers to host Jamstack websites and web services that deploy instantly, scale automatically, and requires no supervision, all with zero configuration. They provide a global edge network, SSL encryption, asset compression, cache invalidation, and more.

Step 1: Add a build script to your package.json file:

{
"scripts": {
  "build": "hexo generate"
}
}

Step 2: Deploy your Hexo Website to Vercel

To deploy your Hexo app with a Vercel for Git Integration_blank, make sure it has been pushed to a Git repository.

Import the project into Vercel using the Import Flow_blank. During the import, you will find all relevant options preconfigured for you; however, you can choose to change any of these options, a list of which can be found here_blank.

After your project has been imported, all subsequent pushes to branches will generate Preview Deployments_blank, and all changes made to the Production Branch (commonly “main”) will result in a Production Deployment_blank.

Alternatively, you can click the deploy button below to create a new project

Deploy Vercel

21云盒子

  1. 在 21云盒子中, 创建一个新的 静态网页,然后使用以下设置:
  • 构建命令: yarn && hexo deploy
  • 发布目录: public
  1. 点击 “部署” 按钮!

样例已经部署在 https://hexo.21yunbox.com/.

Bip

Bip is a commercial hosting service which provides zero downtime deployment, a global CDN, SSL, unlimited bandwidth and more for static websites. Plans are available on a pay as you go, per domain basis.

Getting started is quick and easy, as Bip provides out the box support for Hexo. This guide assumes you already have a Bip domain and Bip CLI installed.

1: Initialise your project directory

$ bip init

Follow the prompts, where you’ll be asked which domain you’d like to deploy to. Bip will detect that you’re using Hexo, and set project settings like the source file directory automatically.

2: Deploy your website

$ hexo generate —deploy && bip deploy

After a few moments, your website will be deployed.

其他方法

Hexo 生成的所有文件都放在 public 文件夹中,您可以将它们复制到您喜欢的地方。