Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] Smoke test new site from Hugo-module Docsy as well #1795

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ on:
workflow_dispatch:

jobs:
new-site-from-npm:
new-site:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
docsy-src: [NPM, HUGO_MODULE]
env:
BASE_REPO: ${{ github.repository }}
BRANCH: ${{ github.head_ref }}
Expand All @@ -37,11 +38,11 @@ jobs:
if: github.event_name != 'pull_request'
run: |
mkdir tmp && cd tmp && set -x
../tools/make-site.sh -p $BASE_REPO -v $SHA
../tools/make-site.sh -s ${{ matrix.docsy-src }} -r $BASE_REPO -v $SHA
shell: bash
- name: Make site from PR
if: github.event_name == 'pull_request'
run: |
mkdir tmp && cd tmp && set -x
../tools/make-site.sh -p $PR_REPO -v $BRANCH
../tools/make-site.sh -s ${{ matrix.docsy-src }} -r $PR_REPO -v $BRANCH
shell: bash
229 changes: 165 additions & 64 deletions tools/make-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,40 @@
set -eo pipefail

DEPS="autoprefixer postcss-cli"
DOCSY_REPO_DEFAULT="google/docsy"
DOCSY_REPO=$DOCSY_REPO_DEFAULT
DOCSY_VERS=""
DOCSY_SRC="NPM"
FORCE_DELETE=false
: ${HUGO:=npx hugo}
NPM_PKG_VERS=
NPM_PKG="google/docsy"
SITE_NAME="test-site"
THEMESDIR=node_modules
THEMESDIR="node_modules"
VERBOSE=1
OUTPUT_REDIRECT="" # Use along with VERBOSE

function _usage() {
echo
echo "Usage: `basename $0` [options]"
echo
echo " Creates a Docsy-themed site under SITE_NAME using the Hugo new command."
echo " Docsy is fetched as an NPM module from GitHub, unless the -l flag is used."
echo
echo " -f Force delete SITE_NAME if it exists before recreating it"
echo " -h Output this usage info"
echo " -l PATH Use Docsy at PATH rather than as an NPM package"
echo " -n SITE_NAME Name of directory to create for the Hugo generated site. Default: $SITE_NAME"
echo " -p NPM_PKG GitHub repo to fetch Docsy from as an NPM module"
echo " Format: GITHUB_USER/DOCSY_REPO. Default: $NPM_PKG"
echo " -v VERS Docsy NPM package version. Default: ''."
echo " Examples: semver:0.8.0, some-branch-name"
echo
cat <<EOS
Usage: `basename $0` [options]
Creates a Docsy-themed site under SITE_NAME using the Hugo new command.
Docsy is fetched as an NPM package from $DOCSY_REPO in GitHub,
unless the -l or -d HUGO_MOD flags are used.
-f Force delete SITE_NAME if it exists before recreating it
-h Output this usage info
-l PATH Use local Docsy from PATH. Default: '$THEMESDIR'
-n SITE_NAME Name of directory to create for the Hugo generated site.
Default: '$SITE_NAME'
-q Run a bit more quietly.
-r REPO GitHub org+repo to fetch Docsy from.
Format: GITHUB_USER/DOCSY_REPO. Default: $DOCSY_REPO_DEFAULT
-s MOD_OR_PKG Docsy source: from a Hugo module or NPM package named '$DOCSY_REPO', where
MOD_OR_PKG is NPM or HUGO_MODULE (HUGO for short). Default: $DOCSY_SRC
-v VERS Docsy Hugo module or NPM package version. Default: '$DOCSY_VERS'.
Examples for Hugo modules: v1.1.1, some-branch-name
Examples for NPM: semver:1.1.1, some-branch-name
EOS
}

function usage() {
Expand All @@ -34,58 +45,148 @@ function usage() {
exit $status
}

# Process command line arguments
while getopts "fhl:n:p:v:" opt; do
case $opt in
f)
FORCE_DELETE=true
;;
h)
usage
;;
l)
NPM_PKG=""
THEMESDIR="$OPTARG"
;;
n)
SITE_NAME="$OPTARG"
;;
p)
NPM_PKG="$OPTARG"
;;
v)
NPM_PKG_VERS="#$OPTARG"
;;
esac
done

# Create project directory, checking if it exists first
if [ -e "$SITE_NAME" ]; then
if [ "$FORCE_DELETE" = true ]; then
echo "[INFO] Directory '$SITE_NAME' already exists. Deleting it as requested (-f)."
(set -x; rm -rf "$SITE_NAME")
function process_CLI_args() {
while getopts ":fhl:n:qr:s:v:" opt; do
case $opt in
f)
FORCE_DELETE=true
;;
h)
usage
;;
l)
DOCSY_SRC="LOCAL"
THEMESDIR="$OPTARG"
;;
n)
SITE_NAME="$OPTARG"
;;
q)
VERBOSE=""
OUTPUT_REDIRECT="> /dev/null 2>&1"
;;
r)
DOCSY_REPO="$OPTARG"
;;
s)
DOCSY_SRC=$(echo "$OPTARG" | tr '[:lower:]' '[:upper:]')
if [[ $DOCSY_SRC != "NPM" && $DOCSY_SRC != HUGO* ]]; then
echo "ERROR: invalid argument to -s flag: $DOCSY_SRC"
usage 1;
fi
;;
v)
DOCSY_VERS="$OPTARG"
;;
\?)
echo "ERROR: unrecognized flag: -$OPTARG"
usage 1;
;;
esac
done

shift $((OPTIND-1))
if [ "$#" -gt 0 ]; then
echo "ERROR: extra argument(s): $*" >&2
usage 1;
fi
}

# Create site directory, checking if it exists first
function create_site_directory() {
if [ -e "$SITE_NAME" ]; then
if [ "$FORCE_DELETE" = true ]; then
echo "[INFO] Directory '$SITE_NAME' already exists. Deleting it as requested (-f)."
([[ $VERBOSE ]] && set -x; rm -rf "$SITE_NAME")
else
echo "[ERROR] Directory '$SITE_NAME' already exists. Remove it or use -f to force delete."
exit 1
fi
fi
}

function _npm_install() {
npm init -y > /dev/null
npm install --omit dev --save $DEPS
}

function set_up_and_cd_into_site() {
$HUGO new site --format yaml --quiet "$SITE_NAME"
cd "$SITE_NAME"
eval _npm_install $OUTPUT_REDIRECT

if [[ "$DOCSY_SRC" == HUGO* ]]; then
_set_up_site_using_hugo_modules
else
echo "[ERROR] Directory '$SITE_NAME' already exists. Remove it or use -f to force delete."
exit 1
echo "theme: docsy" >> hugo.yaml
echo "themesDir: $THEMESDIR" >> hugo.yaml
fi
fi
}

DOCSY_NPM_PKG=$NPM_PKG$NPM_PKG_VERS
function _set_up_site_using_hugo_modules() {
local user_name=$(whoami)
# : ${user_name:=$USER}
# : ${user_name:="me"}

set -x
HUGO_MOD_WITH_VERS=$DOCSY_REPO
if [[ -n $DOCSY_VERS ]]; then
HUGO_MOD_WITH_VERS+="@$DOCSY_VERS"
fi

echo "[INFO] Getting Docsy as Hugo module $HUGO_MOD_WITH_VERS"

# Setup site
$HUGO new site --format yaml --quiet "$SITE_NAME"
cd "$SITE_NAME"
npm init -y > /dev/null
npm install --save-dev $DOCSY_NPM_PKG $DEPS
eval "$HUGO mod init github.com/$user_name/$SITE_NAME" $OUTPUT_REDIRECT

if [[ "$DOCSY_REPO" == "$DOCSY_REPO_DEFAULT" ]]; then
eval "$HUGO mod get github.com/$HUGO_MOD_WITH_VERS" $OUTPUT_REDIRECT
else
echo "[INFO] Fetch Docsy GitHub repo '$DOCSY_REPO' @ '$DOCSY_VERS'"
mkdir tmp
BRANCH_SPEC=""
if [[ -n $DOCSY_VERS ]]; then
BRANCH_SPEC="-b $DOCSY_VERS"
fi
git clone --depth=1 https://github.com/$DOCSY_REPO $BRANCH_SPEC tmp/docsy
(cd tmp/docsy && git log -1)

echo "theme: docsy" >> hugo.yaml
echo "themesDir: $THEMESDIR" >> hugo.yaml
echo "replace github.com/$DOCSY_REPO_DEFAULT => ./tmp/docsy" >> go.mod
eval "$HUGO mod get github.com/$DOCSY_REPO_DEFAULT" $OUTPUT_REDIRECT
fi

echo "module: {proxy: direct, hugoVersion: {extended: true}, imports: [{path: github.com/$DOCSY_REPO_DEFAULT, disable: false}]}" >> hugo.yaml
}

# Generate site
$HUGO
function main() {
process_CLI_args "$@"
create_site_directory

set +x
if [[ "$DOCSY_SRC" == "NPM" ]]; then
NPM_PKG=$DOCSY_REPO
if [[ -n $DOCSY_VERS ]]; then
NPM_PKG+="#$DOCSY_VERS"
fi
echo "[INFO] Getting Docsy as NPM package '$NPM_PKG'"
DEPS+=" $NPM_PKG"
elif [[ "$DOCSY_SRC" == "LOCAL" ]]; then
echo "[INFO] Getting Docsy through a local directory '$THEMESDIR"
fi

[[ $VERBOSE ]] && set -x
set_up_and_cd_into_site
eval $HUGO $OUTPUT_REDIRECT # Generate site
[[ $VERBOSE ]] && set +x
cd ..

echo "[INFO] '$SITE_NAME' successfully created, set up, and built."

if [[ $VERBOSE ]]; then
echo "[INFO] Here are the site files:"
echo
set -x
ls -l "$SITE_NAME"
echo
ls -l "$SITE_NAME/public"
fi
}

echo "[INFO] $SITE_NAME successfully created, set up, and built."
main "$@"