Hugo, The world’s fastest framework for building websites. Caddy, the HTTP/2 web server with automatic HTTPS. Github webhook, which is allows you to build or set up GitHub Apps which subscribe to certain events on GitHub.com. With the three parts, we can build a static website and automatize deployment process. In the post, we can learn how to automated deployment static website.

After developed your favorite static website with hugo, I suggest you remove third theme git submodule folder (.git folder in themes/{theme name} and .gitmodules in static website root path).

  • Install caddy and hugo for server.

    ssh your_server
    sudo apt-get update
    sudo apt-get upgrade
    curl https://getcaddy.com | sudo bash -s personal http.git,http.hugo
    sudo apt-get install hugo
    
  • Create caddyfile

    mkdir ~/caddy_log
    cat ~/Caddyfile
    trustkeeper.io  {
      tls [email protected]
      gzip
      root /home/deploy/www/trustkeeper.io/public
      log /home/deploy/caddy_log/access.log
      errors /home/deploy/caddy_log/errors.log
    
      # caddy 的 http.git 插件
      git {
          repo [email protected]:wenweih/trustkeeper.io.git
          path /home/deploy/www/
          clone_args --recursive
          pull_args --recurse-submodules
          key /home/deploy/.ssh/id_rsa
          #then git submodule update --init --recursive
          hook /webhook {webhook secret fill in github}
          then hugo --destination=/home/deploy/www/trustkeeper.io/public
          hook_type github
      }
    }
    
  • Bind your server ip with domain, I strongly recommend cloudflare, which provides DNS Resolver service.

  • Github repo settings
    • You should add server /home/deploy/.ssh/id_rsa.pub content to the repo as the deploy keys in Github webpage, you can find out in repo page settings > Deploy keys and click Add deploy key button to fill out the form.
    • Meanwhile in settings > webhooks page, click Add webhook button.
  • Back to hosted static website server. and run:

    [email protected]:~$ caddy -conf=/etc/caddy/Caddyfile
    Activating privacy features... done.
    Cloning into '/home/deploy/www'...
    remote: Enumerating objects: 258, done.
    remote: Counting objects: 100% (258/258), done.
    remote: Compressing objects: 100% (168/168), done.
    remote: Total 258 (delta 80), reused 240 (delta 62), pack-reused 0
    Receiving objects: 100% (258/258), 4.43 MiB | 1.17 MiB/s, done.
    Resolving deltas: 100% (80/80), done.
    Checking connectivity... done.
    2018/11/11 21:02:56 ssh://[email protected]:wenweih/trustkeeper.io.git pulled.
    0 draft content
    0 future content
    3 pages created
    0 non-page files copied
    0 paginator pages created
    0 tags created
    0 categories created
    in 80 ms
    2018/11/11 21:02:56 Command 'hugo --destination=/home/deploy/www/trustkeeper.io/public' successful.
    http://trustkeeper.io
    https://trustkeeper.io
    

    If your want to run the service background, go with nohup command:

    nohup caddy -conf=/etc/caddy/Caddyfile >/dev/null 2>&1 &

Finally, each git push for the repo, your website would be automated update.

https://trustkeeper.io here we go!