chore: prepare to deploy to gh-pages
authorGreg Hurrell <greg@hurrell.net>
Sun, 13 Jun 2021 09:14:06 +0000 (11:14 +0200)
committerGreg Hurrell <greg@hurrell.net>
Sun, 13 Jun 2021 09:14:06 +0000 (11:14 +0200)
Get rid of the old deploy.sh script and copy the publish script from my
cv repo. Next commit will contain the necessary edits to get it actually
working.

deploy.sh [deleted file]
publish.sh [new file with mode: 0755]

diff --git a/deploy.sh b/deploy.sh
deleted file mode 100755 (executable)
index 56e7c94..0000000
--- a/deploy.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-set -e
-
-declare -r HOST=hex.wincent.com
-declare -r BASE=/var/www/hex.wincent.com/public_html
-
-# Upload files to staging area.
-ssh $HOST mkdir -p hextrapolate
-scp -r build/* $HOST:hextrapolate
-
-# Mirror files over to final position.
-ssh -t $HOST sudo rsync -va hextrapolate/ $BASE/
diff --git a/publish.sh b/publish.sh
new file mode 100755 (executable)
index 0000000..faf1c02
--- /dev/null
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+# Get a temporary index file to work with.
+INDEX="$(mktemp -d)/index"
+
+for FILE in $(find images public -type f -and -not -name '.*'); do
+  # Add file contents to object database.
+  BLOB_ID=$(git hash-object -w -- "$FILE")
+
+  # Add file to index.
+  MODE=100644
+  NAME=$(basename "$FILE")
+  env GIT_INDEX_FILE="$INDEX" git update-index \
+    --add \
+    --cacheinfo "${MODE},${BLOB_ID},${NAME}"
+done
+
+# Special file: "CNAME" for GitHub pages custom domain.
+# See: https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site/managing-a-custom-domain-for-your-github-pages-site#configuring-a-subdomain
+BLOB_ID=$(/bin/echo -n greg.hurrell.net | git hash-object -w --stdin)
+MODE=100644
+NAME=CNAME
+env GIT_INDEX_FILE="$INDEX" git update-index \
+  --add \
+  --cacheinfo "${MODE},${BLOB_ID},${NAME}"
+
+# Create tree object from index.
+TREE_ID=$(env GIT_INDEX_FILE="$INDEX" git write-tree)
+
+# Make the commit.
+DATE=$(date)
+PARENT=$(git rev-parse refs/heads/gh-pages)
+SOURCE=$(git describe --always --dirty)
+COMMIT=$(builtin echo "Build $DATE\n\nFrom $SOURCE." | git commit-tree "$TREE_ID" -p "$PARENT" -F -)
+
+git update-ref refs/heads/gh-pages "$COMMIT"
+
+echo "Created commit: $(git rev-parse --short "$COMMIT")\n"
+
+git log --raw -1 refs/heads/gh-pages
+
+if $(git diff --quiet "$COMMIT" "$PARENT"); then
+  BOLD=$(tput bold)
+  RED=$(tput setaf 1)
+  RESET=$(tput sgr0)
+  echo "\n${BOLD}${RED}WARNING: CREATED EMPTY COMMIT${RESET}\n"
+  echo "If this was in error, reset using:"
+  echo "  git update-ref refs/heads/gh-pages $(git rev-parse --short "$PARENT")"
+fi