chriswarbo-net: a2e7195fb52b9271fd758f8f45a3a9d5cb45b9b9
1: #!/usr/bin/env bash
2: set -e
3: set -o pipefail
4: shopt -s nullglob
5:
6: if command -v ts > /dev/null && ts | grep queued | grep render > /dev/null
7: then
8: echo "Another render is queued, deferring to that"
9: exit 0
10: fi
11:
12: # We use a lot of recursion, which can overflow the stack, so try increasing it
13: ulimit -s 100000 || true
14:
15: function build() {
16: echo "Building '$1'" 1>&2
17: nix-build --show-trace -A "$1" static/nix
18: }
19:
20: function check() {
21: ./static/checkSource 1>&2 || exit 1
22: build wholeSite
23: }
24:
25: function push() {
26: BUCKET="${BUCKET:-www.chriswarbo.net}"
27:
28: # Default to building and testing the site; override by passing a path
29: # explicitly (e.g. the result of './render build')
30: if [[ "$#" -eq 1 ]]
31: then
32: SITE="$1"
33: else
34: SITE=$(check)
35: fi
36:
37: # Sanity check
38: echo "Using '$SITE' as site root" 1>&2
39: for EXPECTED in index.html projects blog blog.html js css
40: do
41: [[ -e "$SITE/$EXPECTED" ]] || {
42: echo "Given site '$SITE' does not contain '$EXPECTED', aborting"
43: exit 1
44: } 1>&2
45: done
46:
47: local args
48: # These speed up syncing with S3 (fewer roundtrips, etc.)
49: args=(--fast-list --no-update-modtime --checksum)
50:
51: # Exclude the /git folder from the sync: without this, sync will try to
52: # delete all of the /git/foo folders that exist in S3.
53: args+=(--filter '- /git/')
54:
55: # Our Nix result is full of symlinks, which must be dereferenced
56: args+=(-L)
57:
58: echo "Syncing '$SITE' with '$BUCKET'" 1>&2
59: RCLONE_S3_REGION=eu-west-1 RCLONE_S3_PROVIDER=AWS RCLONE_S3_ENV_AUTH=true \
60: with-aws-creds \
61: rclone sync "${args[@]}" "$SITE" ":s3:$BUCKET"
62:
63: # Temporary workaround (as of 2023-07-03...) to redirect chriswarbo.net/git
64: # requests to github.com/warbo. We want /git/foo.git to be redirected, but
65: # not /git/foo (the human-readable HTML interfaces): to achieve that effect,
66: # we just redirect all 404s, so existing objects (like /git/foo) will be
67: # served, but non-existing objects (like /git/foo.git, among others) will be
68: # redirected.
69: # NOTE: We don't use S3's object-based redirects (which use metadata), since
70: # those emit 301 PERMANENT redirects, and we want 302 TEMPORARY redirects.
71: CONFIG='{
72: "IndexDocument": {
73: "Suffix": "index.html"
74: },
75: "RoutingRules": [
76: {
77: "Condition": {
78: "KeyPrefixEquals": "git/",
79: "HttpErrorCodeReturnedEquals": "404"
80: },
81: "Redirect": {
82: "HostName": "github.com",
83: "ReplaceKeyPrefixWith": "warbo/"
84: }
85: }
86: ]
87: }'
88:
89: echo "Redirecting 404s in /git to github.com/warbo" 1>&2
90: with-aws-creds aws s3api put-bucket-website \
91: --bucket "$BUCKET" \
92: --website-configuration "$CONFIG"
93: }
94:
95: printf "Waiting for rendering lock..." 1>&2
96: (
97: flock -x 200
98: echo "lock aquired" 1>&2
99:
100: case "$1" in
101: page)
102: shift
103: build "$@"
104: ;;
105:
106: build)
107: build untestedSite
108: ;;
109:
110: test)
111: check
112: ;;
113:
114: push)
115: shift
116: push "$@"
117: ;;
118:
119: *)
120: echo "
121: Usage: $0 <build|test|push|page>
122:
123: build: Builds the HTML, CSS, etc. of the site. Outputs a directory containing
124: the result.
125:
126: test: Builds the site and runs tests on the result. Outputs a directory
127: containing the result.
128:
129: push: Push a given site directory to chriswarbo.net. If no argument is given,
130: defaults to using the output of 'test'.
131:
132: page: Build a given attribute of the site, e.g. a page or directory. Requires an
133: argument, which is passed to nix-build's -A option.
134: " 1>&2
135: exit 1
136: ;;
137: esac
138: ) 200>/tmp/blog.lock
Generated by git2html.