Skip to content

Instantly share code, notes, and snippets.

@jukrb0x
Last active April 30, 2020 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jukrb0x/95d0adb547d6d29dc7a216913463373b to your computer and use it in GitHub Desktop.
Save jukrb0x/95d0adb547d6d29dc7a216913463373b to your computer and use it in GitHub Desktop.
Bash script for updating Hugo Blog | Hugo Generator & Packaging + Uploading

我是谁

whoami

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

English | 中文文档

开始之前

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

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

安装 sshpass

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

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

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

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

你会在这里找到 sshpass.rb

快速开始

下载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的文件
  • 上传压缩包
  • 解压缩并删除压缩包

脚本不会执行:

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

安全性和便捷性

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

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

自定义特性

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

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

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

What is Publi.sh

whoami

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

English | 中文文档

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.

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

Quick Start

Download publi.sh

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.

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 .

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.

#!/bin/sh
# you need to pre-install SSHPASS on your own computer, if not, check this out: https://gist.github.com/arunoda/7790979
# My default username is root, you may change it to yourself.
# ====== VAR ======
hugoRoot='/Users/xxxxxx/hugo/blog/'
publicFolder='/Users/xxxxxx/hugo/blog/public'
zipName='Archive.zip'
zipLoc=$publicFolder/$zipName
hostname='xxx.xxx.xxx.xxx'
upTo='/www/wwwroot/webxxx'
# ====== VAR ======
# -----------------
echo "========== WELCOME TO HOGWARTS EXPRESS =========="
echo " ___ ___ __
/ | \ ____ ______ _ _______ ________/ |_ ______
/ ~ \/ _ \ / ___\ \/ \/ /\__ \\_ __ \ __\/ ___/
\ Y ( <_> ) /_/ > / / __ \| | \/| | \___ \
\___|_ / \____/\___ / \/\_/ (____ /__| |__| /____ >
\/ /_____/ \/ \/
___________
\_ _____/__ ________________ ____ ______ ______
| __)_\ \/ /\____ \_ __ \_/ __ \ / ___// ___/
| \> < | |_> > | \/\ ___/ \___ \ \___ \
/_______ /__/\_ \| __/|__| \___ >____ >____ >
\/ \/|__| \/ \/ \/ "
echo "Please enter your SSH password:"
stty -echo
read sshpw
stty echo
cd $hugoRoot
hugo
cd $publicFolder
zip -r $zipName *
stty -echo
stty echo
# scp $zipLoc root@$hostname:$upTo
# manual pass
sshpass -p $sshpw scp $zipLoc root@$hostname:$upTo
# auto pass
# ssh root@$hostname
# manual pass
sshpass -p $sshpw ssh -o StrictHostKeyChecking=no root@$hostname "cd $upTo;unzip -o $zipName;rm $zipName;rmdir __MACOSX"
# auto pass
# cd $upTo
# unzip -o $zipName
# rm $zipName
# rmdir __MACOSX
# logout
cd $publicFolder
rm $zipName
echo '====== Website has been updated ======'
echo '====== Press any key to exit ======='
read byebye
require 'formula'
class Sshpass < Formula
url 'http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz'
homepage 'http://sourceforge.net/projects/sshpass'
sha256 'c6324fcee608b99a58f9870157dfa754837f8c48be3df0f5e2f3accf145dee60'
def install
system "./configure", "--disable-debug", "--disable-dependency-tracking",
"--prefix=#{prefix}"
system "make install"
end
def test
system "sshpass"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment