霍格沃茨特快列车
Mkapakka's Hut
脚本一键更新网站|Hugo渲染+上传到服务器

这篇文章用该脚本上传。

突发奇想:有空把这个脚本变得更有交互性

每次更新都要用ftp/webftp不然就在terminal敲一堆命令,累死👴了,写了个脚本一键操作。

Github Gist 脚本文件

0. 我是谁

whoami

这是一个bash脚本用于自动生成Hugo的Public文件并自动上传到服务器。

English | 中文文档

1. 开始之前

你需要安装好SSH,SCP这类东西。本文档不会教你如何安装,请自行脑补Google

没有ssh和scp你为啥会看这篇文档呢(逃)

1.1. 安装 sshpass

这一步是自动化输入密码步骤,安装sshpass的方法可以看这里

因为我在用macOS,所以简单提一下:

Homebrew默认禁止了安装sshpass,但是可以通过第三方的包来安装

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

你会在这里找到 sshpass.rb

2. 快速开始

下载publi.sh 脚本文件

修改一些变量:

hugoRoot='/Users/xxxxxx/hugo/blog/' # 你的本地hugo网站根目录
publicFolder='/Users/xxxxxx/hugo/blog/public' # 你的hugo public目录地址
zipName='Archive.zip' # 压缩文件名称
zipLoc=$publicFolder/$zipName # 不用管这个
hostname='xxx.xxx.xxx.xxx' # 服务器地址
upTo='/www/wwwroot/webxxx' # 服务器上网站根目录

我的ssh登录账号默认为root,如果你的不是root请自行修改。

这个脚本会自动执行hugo命令来生成public目录,所以不用提前执行一遍。

我用了几个简单的方法来更新网站,仅仅是上传并覆盖,这里有几点你需要知道。

脚本会执行:

  • 生成public
  • 压缩public的文件
  • 上传压缩包
  • 解压缩并删除压缩包

脚本不会执行:

  • 检查文件是否重复,直接继续文件覆盖
  • 不会删除已存在的文件

3. 安全性和便捷性

出于安全原因,不建议将ssh密码直接放置在脚本中,所以我设定为每次执行时都输入一遍ssh密码。

当然你可以在脚本的变量部分新建变量sshpw并且赋值为你的ssh密码,并且注释掉第26到29行。

4. 自定义特性

我发现我的系统不知道咋滴会把__MACOSX放到压缩文件里,但是在我的压缩包查看器中找不到。

所以我新增了一个删除文件夹的命令在42行rmdir __MACOSX,但是这个命令好像有误,因为当文件夹不是空的时候它会出错,可以换成rm -rf __MACOSX,但是不是什么大事我就没删除它了。

你可以直接删除这个命令。


English Documentation:

0. What is Publi.sh

whoami

This is a bash script for uploading hugo public folder to server automatically.

English | 中文文档

1. Before deploying

You need to pre-install some tools on your own computer

Please make sure you have installed some basic ones like SSH, SCP which will not be mentioned related installation and usage.

1.1. Install sshpass

sshpass will make you feel relaxed when inputting password required, that we gonna impose in the script.

You can install by yourself or following this tutorial

Since I am using macOS, here’s the simple way to install on it.

homebrew has forbade installing the sshpass by default, whereas we can install the unofficial brew package for it.

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

You can find sshpass.rb here

2. Quick Start

Download publi.sh Script

Modify variables to your own:

hugoRoot='/Users/xxxxxx/hugo/blog/' # your local hugo website root
publicFolder='/Users/xxxxxx/hugo/blog/public' # your hugo website PUBLIC root
zipName='Archive.zip' # the zip name
zipLoc=$publicFolder/$zipName # No need to change
hostname='xxx.xxx.xxx.xxx' # server address
upTo='/www/wwwroot/webxxx' # the root of the website on server

The ssh login is root by default, you may need to change it by yourself.

When you run the script the hugo will generate the public files. There’s no need to hugo again.

I take the simple way to update the website which means the script only upload files without checking the files may needed be removed.

Generally, the script do:

  • Generate Hugo public files
  • Compress the public files in one file
  • Upload the zip package
  • Decompress the zip and Delete it

and not do:

  • Check files iteration, it just covers the same file.
  • Do nothing with files exists.

3. Security & Convenience

For security I set ssh password shall be entered every execution.

Of course, you can set a variable named sshpw at the front of script, and comment out line 26 ~ 29 .

4. My own specifics

I found somehow it generated __MACOSX in the zip file. Hence I remove it after uploadding by line 42 rmdir __MACOSX

Perhaps it’s not right command because failure occurs when the folder is not empty, using rm -rf __MACOSX is better.

But it’s not a really issuse so I ignored even as you can just remove this command.


Last modified on 2020-04-19