Repository navigation
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·211 lines (179 loc) · 10.4 KB
/
Copy pathsync.sh
File metadata and controls
executable file
·211 lines (179 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
set -o errexit -o pipefail
# This script takes the built Hugo site and:
# - creates a new S3 bucket named according to whether the action is a push or pull_request.
# - creates a list of all Hugo-generated client-side ("meta-refresh") redirects that
# - we'll use to produce proper 301s later.
# - pushes the content of the website into the new S3 bucket.
# - tests the built website, first for broken links, then with Cypress to ensure pages
# - render and behave properly.
# - emits a metadata file containing information about the commit and bucket, which
# - Pulumi will use to process its update.
# - writes a record to AWS Parameter Store relating the generated bucket to the commit
# - responsible for producing it.
# - Posts a PR comment back to GitHub, if applicable.
source ./scripts/ci/common.sh
# The docroot of the built website.
build_dir="public"
# The text file we'll write as an output result.
metadata_file="$(origin_bucket_metadata_filepath)"
# Verify we have at least 1000 index.html files in total across the site.
if [ ! "$(find $build_dir -type f | grep index.html | wc -l)" -ge 1000 ]; then
echo "Page-count check failed. Exiting."
exit 1
fi
# Move the sitemap from root to the registry directory.
mv "${build_dir}/sitemap.xml" "${build_dir}/registry/sitemap.xml"
# For previews, name the destination bucket with the PR number, to reduce the number of
# buckets we create and to facilitate shorter sync times.
destination_bucket=$(echo "$(origin_bucket_prefix)-$(build_identifier)" | tr '_' '-')
destination_bucket_uri="s3://${destination_bucket}"
# Make the bucket. If this fails, there are two explanations, given the way we're naming
# our buckets: either a previous run failed at some point after creating the bucket, in
# which case we should simply proceed (to repopulate it), or the bucket was somehow
# created in another account, in which case subsequent operations on the bucket will also
# fail, causing this script to exit nonzero. In either case, it's okay to continue.
aws s3 mb $destination_bucket_uri --region "$(aws_region)" || true
aws s3api put-public-access-block --bucket $destination_bucket --public-access-block-configuration BlockPublicAcls=false
aws s3api put-bucket-ownership-controls --bucket $destination_bucket --ownership-controls="Rules=[{ObjectOwnership=ObjectWriter}]"
aws s3api put-bucket-acl --bucket $destination_bucket --acl bucket-owner-full-control --acl public-read
aws s3api put-bucket-tagging --bucket $destination_bucket --tagging "TagSet=[{$(aws_owner_tag)}]" --region "$(aws_region)"
# Make the bucket an S3 website.
aws s3 website $destination_bucket_uri --index-document index.html --error-document 404.html --region "$(aws_region)"
# Sync the local build directory to the bucket using s5cmd for massively parallel uploads.
# s5cmd uses hundreds of concurrent goroutines vs aws cli's ~10-16 concurrent requests,
# resulting in 10-50x faster uploads for large file counts.
# The --delete flag removes destination objects not present locally, keeping the bucket clean
# for PR preview buckets that get reused across commits.
log "Synchronizing to $destination_bucket_uri..."
s5cmd --log error sync --delete --acl public-read \
"$build_dir/" "$destination_bucket_uri/"
log "Sync complete."
# Sync LLM docs separately. These are generated outside the Hugo build tree to avoid
# Hugo processing static files. They're uploaded directly to the same URL paths
# they'd occupy if they were in the Hugo static directory.
#
# LLM docs bundles are uploaded gzip-compressed with Content-Encoding: gzip so they
# pass through both CloudFront layers (registry CDN + www.pulumi.com CDN) without
# relying on CloudFront's automatic compression -- which is disabled on /registry/*
# because the outer CDN forwards Accept-Encoding via AllViewerExceptHostHeader, and
# wouldn't help anyway for bundles over CloudFront's 10 MB auto-compress ceiling
# (the aws bundle is >100 MB uncompressed). Go's http.Client decompresses
# Content-Encoding: gzip transparently, so the pulumi docs CLI requires no changes.
#
# The llm-docs-out/ tree contains only llm-docs.json files, so it is safe to apply
# --content-encoding/--content-type globally to every object in the sync.
llm_docs_dir="llm-docs-out"
if [[ -d "$llm_docs_dir" ]]; then
log "Pre-gzipping LLM docs..."
find "$llm_docs_dir" -type f -name 'llm-docs.json' -print0 \
| xargs -0 -P"$(nproc)" -I{} sh -c 'gzip -9 < "$1" > "$1.gz.tmp" && mv "$1.gz.tmp" "$1"' _ {}
log "Synchronizing LLM docs to $destination_bucket_uri..."
s5cmd --log error sync --acl public-read \
--content-encoding gzip \
--content-type application/json \
"$llm_docs_dir/" "$destination_bucket_uri/"
log "LLM docs sync complete."
fi
s3_website_url="http://${destination_bucket}.s3-website.$(aws_region).amazonaws.com"
echo "$s3_website_url"
# Set the content-type of latest-version explicitly. (Otherwise, it'll be set as binary/octet-stream.)
aws s3 cp "$build_dir/latest-version" "${destination_bucket_uri}/latest-version" \
--content-type "text/plain" --acl public-read --region "$(aws_region)" --metadata-directive REPLACE
# Smoke test LLM docs compression at the S3 website layer (before either CloudFront
# layer has a chance to mask a regression). Uses the random package because it has a
# small, reliably-present llm-docs.json bundle. Catches the regression where we
# accidentally upload llm-docs.json files without Content-Encoding: gzip, which would
# otherwise only surface after deploy when the CLI starts paying full wire cost again.
#
# Uses curl --dump-header to validate the body (gunzip -t) and capture the headers in
# a single round trip, rather than issuing separate HEAD and GET requests.
if [[ -d "$llm_docs_dir" && -f "$llm_docs_dir/registry/packages/random/api-docs/llm-docs.json" ]]; then
log "Smoke-testing LLM docs compression..."
llm_docs_test_url="${s3_website_url}/registry/packages/random/api-docs/llm-docs.json"
llm_docs_headers_file=$(mktemp)
if ! curl -fsS --dump-header "$llm_docs_headers_file" "$llm_docs_test_url" | gunzip -t; then
echo "ERROR: $llm_docs_test_url body did not decompress as gzip (or request failed)." >&2
rm -f "$llm_docs_headers_file"
exit 1
fi
llm_docs_headers=$(cat "$llm_docs_headers_file")
rm -f "$llm_docs_headers_file"
if ! grep -qi '^content-encoding: gzip' <<< "$llm_docs_headers"; then
echo "ERROR: $llm_docs_test_url is missing 'Content-Encoding: gzip'. Response headers:" >&2
echo "$llm_docs_headers" >&2
exit 1
fi
log "LLM docs compression smoke test passed."
else
log "Skipping LLM docs compression smoke test: $llm_docs_dir/registry/packages/random/api-docs/llm-docs.json not found."
fi
# Smoke test the deployed website.
log "Running browser tests on $s3_website_url..."
./scripts/run-browser-tests.sh "$s3_website_url" "site.cy.js,structured-data.cy.js"
# At this point, we have a bucket that's suitable for deployment. As a result of this run,
# we leave a file in the project root indicating the name of the bucket that was generated
# and the associated commit SHA, and then we upload that file into the bucket as well, for
# reference. The Pulumi program will expect this file to exist, and will use the bucket
# name to set the CloudFront origin on the next Pulumi run.
#
# Why use a local file and not `pulumi config`, or some other persistence store? Because
# we need ensure that every CI job deploys only what it was responsible for building.
# Coupled with the locking we get from the Pulumi Cloud, using a local file is a safe
# way to ensure we're deploying what we just finished building and testing.
log "Writing result metadata."
metadata='{
"timestamp": %s,
"commit": "%s",
"bucket": "%s",
"url": "%s"
}'
printf "$metadata" "$(current_time_in_ms)" "$(git_sha)" "$destination_bucket" "$s3_website_url" > "$metadata_file"
# Copy the file to the destination bucket, for future reference.
aws s3 cp "$metadata_file" "${destination_bucket_uri}/registry/metadata.json" --region "$(aws_region)" --acl public-read
# Persist an association between the current commit and the bucket we just deployed to.
set_bucket_for_commit "$(git_sha)" "$destination_bucket" "$(aws_region)"
# The marker that makes the preview comment pinned: every build finds the comment carrying it
# and rewrites that one, instead of leaving a fresh comment per commit.
PREVIEW_COMMENT_MARKER="<!-- registry-preview-link -->"
# Posts (or updates) the pinned preview comment on the PR.
post_preview_comment() {
if [[ -z "${GITHUB_TOKEN:-}" ]]; then
log "No GitHub token available; skipping the preview comment."
return 0
fi
local event pr_comment_api_url repo_api_url pr_number
if [[ -n "${PREVIEW_PR:-}" ]]; then
pr_number="$PREVIEW_PR"
repo_api_url="${GITHUB_API_URL:-https://api.github.com}/repos/${GITHUB_REPOSITORY}"
pr_comment_api_url="${repo_api_url}/issues/${pr_number}/comments"
elif [[ -n "${GITHUB_EVENT_PATH:-}" && -f "${GITHUB_EVENT_PATH}" ]]; then
event="$(cat "$GITHUB_EVENT_PATH")"
pr_comment_api_url="$(echo "$event" | jq -r '.pull_request._links.comments.href // empty')"
repo_api_url="$(echo "$event" | jq -r '.pull_request.base.repo.url // empty')"
pr_number="$(echo "$event" | jq -r '.number // empty')"
fi
if [[ -z "$pr_comment_api_url" || -z "$repo_api_url" || -z "$pr_number" ]]; then
log "No pull request to comment on; skipping the preview comment."
return 0
fi
local body
body="${PREVIEW_COMMENT_MARKER}
Your site preview for commit $(git_sha_short) is ready! :tada:
${preview_url}$(changed_pages_section "$repo_api_url" "$pr_number" "$build_dir" "$s3_website_url")"
upsert_github_pr_comment \
"$PREVIEW_COMMENT_MARKER" \
"$body" \
"$pr_comment_api_url" \
"$repo_api_url"
}
# Finally, for previews, point the PR at the resulting bucket URL. Everything below is
# reporting, not deployment, so it must never fail the build -- hence the `|| log`.
if [[ "$1" == "preview" ]]; then
preview_url="${s3_website_url}/registry/"
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
echo "Site preview for commit $(git_sha_short): ${preview_url}" >> "$GITHUB_STEP_SUMMARY" || true
fi
post_preview_comment || log "Failed to post the preview comment; continuing."
fi
log "Done! The bucket website is now built and available at ${s3_website_url}."