#!/usr/bin/env bash
# @see https://git.io/viyyo
# template for localhost:4567/* -> gh-pages deployment
# gh-pages will host the webApp risk calculator for Fawaz's paper

set -eo pipefail

[[ "$TRACE" ]] && set -x

start_viewer_sinatra() {
  mkdir -p /tmp/viewer-sinatra
  cd /tmp/viewer-sinatra
  # TODO: Make `master` configurable
  curl -fsSL https://github.com/everypolitician/viewer-sinatra/archive/master.tar.gz | tar -z -x -f - --strip 1
  unset BUNDLE_GEMFILE
  bundle install
  bundle exec ruby app.rb &
  while ! nc -z localhost 4567; do sleep 1; done
}

build_viewer_static() {
  cd /tmp
  wget -nv -m localhost:4567/status/all_countries.html || (echo "wget exited with non-zero exit code: $?" >&2 && exit 1)
}

deploy_viewer_static() {
  cd /tmp
  git clone --depth=1 https://github.com/everypolitician/viewer-static.git
  cd viewer-static
  git checkout gh-pages
  cp -R /tmp/localhost:4567/* .
  git add .
  git -c "user.name=everypoliticianbot" -c "user.email=everypoliticianbot@users.noreply.github.com" commit -m "Automated commit" || true
  git push origin gh-pages
}

main() {
  if [[ "$TRAVIS_PULL_REQUEST" == false && "$TRAVIS_BRANCH" == master ]]; then
    if [[ -z $(git diff --name-only "$TRAVIS_COMMIT_RANGE" -- countries.json) ]]; then
      echo "No changes to countries.json detected, skipping deploy."
      exit
    fi
    start_viewer_sinatra
    build_viewer_static
    deploy_viewer_static
  fi
}

main

The deploy_viewer_static() function is a shell script that automates the deployment of a static website to the gh-pages branch of a GitHub repository. Here’s a breakdown of what each line is doing:

  1. cd /tmp: Changes the current working directory to the /tmp directory, which is often used for storing temporary files on Unix-like operating systems.

  2. git clone --depth=1 https://github.com/everypolitician/viewer-static.git: Clones the GitHub repository at https://github.com/everypolitician/viewer-static.git into the /tmp directory. The --depth=1 flag indicates that only the latest commit is fetched, making the clone operation faster.

  3. cd viewer-static: Changes the current working directory to the newly cloned viewer-static directory.

  4. git checkout gh-pages: Switches to the gh-pages branch of the repository. This branch is often used for hosting static websites on GitHub Pages.

  5. cp -R /tmp/localhost:4567/* .: Copies all files from the /tmp/localhost:4567/ directory to the current (viewer-static) directory. The -R flag indicates that the operation should be recursive, copying all subdirectories and their contents.

  6. git add .: Stages all the new and modified files in the current directory for a git commit.

  7. git -c "user.name=everypoliticianbot" -c "user.email=everypoliticianbot@users.noreply.github.com" commit -m "Automated commit" || true: Commits the staged changes to the repository with the commit message “Automated commit”. The commit is made under the username everypoliticianbot and email everypoliticianbot@users.noreply.github.com. The || true part ensures that even if the commit fails, the script continues execution without error.

  8. git push origin gh-pages: Pushes the committed changes to the gh-pages branch of the remote GitHub repository, thereby updating the GitHub Pages website.

Overall, the script automates the process of updating a static website hosted on GitHub Pages by pulling the latest version of the website from a repository, copying new files into it, and then pushing the changes back to the repository. This is a common pattern for automating the deployment of static websites and should be adopted for this project.