Hugo: Deploy SFTP

Article published the ; modified the
4 minute(s) to read

This article has 682 words.
RAW source of the article: MD

Description

To deploy a static website, made with Hugo, some solutions exist:

  • the easy is with the tool rsync .
  • automated deployment solutions.
  • I review a third for users SSH, under chroot contraints.

Into the simple context deployment, you need to create a file named deploy at the root of your Hugo project.

Documentation

Utilisation

rsync

To deploy a site with rsync is really simple. rsync is the tool to synchronize datas; it’s just do it!

Install the tool:

  • on Debian/*Buntu : # apt install rsync
  • on OpenBSD : # pkg_add rsync - FYI , since OpenBSD 6.5, it exists the tool openrsync; it’s subject to the same problem described below!

I presume you had an authentification SSH, with key is better.

File: deploy

1
2
3
4
5
6
7
8
9
#!/bin/sh

user=userid
host=servername
port=22

dir_dist=/remote_folder/

hugo && rsync -avz --delete -e "ssh -p $port" public/ ${user}@${host}:${dir_dist}

This example show the deployment with rsync using a SSH connection.

Info
Warning

SshFS

SshFS can be install with the package manager:

  • on Debian/*Buntu : # apt install sshfs
  • on OpenBSD : # pkg_add sshfs
Info

Once the connection is established, the remote folder is mounted locally where you desired.

Now, it’s time to use rsync as:
cd "${dir_local}" && rsync -av --delete --human-readable --progress --stats "." "${dir_mount}"

To unmount correctly:
$ doas umount "${dir_mount}"

With this in mind, see the file deploy under this SshFS+rsync context:

File: deploy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
#set -x
[ -n "$TERM" ] && clear

ROOT="$(dirname "$(readlink -f -- "$0")")"

id=userid
host=servername
port=22

### the directory where your web site files should go
## dir_dist: relative to chroot SSH
dir_dist="/www/"
dir_local="$ROOT/public/"
dir_mount="$HOME/servers/${id}/"

rsync_opts="--human-readable --progress --stats "

################################################################################
###
##
#   Fonctions
##
###
################################################################################

_mount() {
    [ ! -d "${dir_mount}" ] && mkdir -p "${dir_mount}"
    [ -d "${dir_mount}" ] && doas sshfs -C -p $port -o allow_other -o uid=$(id -u $USER) -o gid=$(id -g $USER) ${id}@${host}:${dir_dist} "${dir_mount}"
}

_rsync() {

    cd "${dir_local}" || exit
    rsync -av --delete $rsync_opts "." "${dir_mount}"

}

_umount() {
    doas umount "${dir_mount}"
}

################################################################################
###
##
#   Execution
##
###
################################################################################

hugo
status="$?"

if [ "${status}" -eq 0 ]; then
    if _mount; then
        _rsync
        _umount
    fi
fi

lftp

lftp is a “Swiss Army knife” for the network connection. It run the FTP(S), HTTP(S) protocols, on IPv4, IPv6; and too, bittorent and partial WebDAV. It can be mirroring documents and folders. And: SFTP!

Yes, you can mirror datas on SFTP!


To install by the package manager:

  • on Debian/*Buntu : # apt install lftp
  • on OpenBSD : # pkg_add lftp

File: deploy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/sh
#set -x
[ -n "$TERM" ] && clear

ROOT="$(dirname "$(readlink -f -- "$0")")"

id=userid
host=servername
port=22

### the directory where your web site files should go
## dir_dist: relative to chroot SSH
dir_dist="/www/"
dir_local="$ROOT/public/"

hugo && /usr/local/bin/lftp -e "mirror -e -R ${dir_local} ${dir_dist} ; quit" -p $port sftp://${id}@${host}

FIN

Voilà: This is the end!

Now, you known how to deploy your static website Hugo, with rsync/ssh, sshfs+rsync or lftp.