Repository navigation
163 lines (155 loc) · 6.73 KB
/
Copy pathdeploy.yml
File metadata and controls
163 lines (155 loc) · 6.73 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
name: Deploy
# Packages website/ (worker.ts + static site/) and deploys its Worker:
# - Push (or dispatch) on dev deploys psychoscript-dev-site to https://dev.psychoscript.com/
# - Fast-forward (or dispatch) on production deploys psychoscript-production-site to https://psychoscript.com/
#
# Split at the credential boundary:
# - build checks out the repo with contents: read, no environment and no secrets,
# stages worker.ts and site/, and uploads them with a SHA256SUMS digest.
# - deploy runs in the branch's environment (dev or production), sparse-checks out
# only .github/release/, installs the pinned wrangler CLI, verifies the artifact
# digest, assumes github/psychoscript-<mode>, mints a short-lived IP-bound
# Cloudflare token, and runs .github/release/deploy-site.sh.
# - verify checks the live site without secrets.
on:
push:
branches: [dev, production]
paths:
- "website/**"
- ".github/workflows/deploy.yml"
- ".github/release/**"
workflow_dispatch:
permissions:
contents: read
concurrency:
group: site-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
if: github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/production'
runs-on: ubuntu-24.04
timeout-minutes: 10
outputs:
digest: ${{ steps.upload.outputs.digest }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
persist-credentials: false
- name: Stage site
run: |
rm -rf "$RUNNER_TEMP/site"
mkdir -p "$RUNNER_TEMP/site"
cp website/worker.ts "$RUNNER_TEMP/site/worker.ts"
cp -R website/site "$RUNNER_TEMP/site/site"
- id: upload
uses: ./.github/release/upload
with:
name: site-${{ github.ref_name == 'production' && 'production' || 'dev' }}
path: ${{ runner.temp }}/site
retention-days: 7
deploy:
needs: build
runs-on: ubuntu-24.04
timeout-minutes: 15
environment:
name: ${{ github.ref_name == 'production' && 'production' || 'dev' }}
url: ${{ github.ref_name == 'production' && 'https://psychoscript.com' || 'https://dev.psychoscript.com' }}
permissions:
contents: read
id-token: write
concurrency:
group: site-deploy-${{ github.ref_name == 'production' && 'production' || 'dev' }}
cancel-in-progress: false
env:
MODE: ${{ github.ref_name == 'production' && 'production' || 'dev' }}
steps:
- name: Require the branch for the mode
env:
REF: ${{ github.ref }}
run: |
case "$MODE:$REF" in
production:refs/heads/production | dev:refs/heads/dev) ;;
*) echo "::error::The $MODE site deploys only from its own branch, not $REF."; exit 1 ;;
esac
- name: Check out the release scripts
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
sparse-checkout: /.github/release/
sparse-checkout-cone-mode: false
persist-credentials: false
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 22
package-manager-cache: false
- name: Install the pinned wrangler CLI
working-directory: .github/release/wrangler
run: npm ci --ignore-scripts --no-audit --no-fund
- name: Download
uses: ./.github/release/download
with:
pattern: site-${{ env.MODE }}
digests: site-${{ env.MODE }}=${{ needs.build.outputs.digest }}
- id: aws
name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
with:
role-to-assume: arn:aws:iam::604439173067:role/github/psychoscript-${{ env.MODE }}
role-session-name: psychoscript-${{ github.run_id }}
role-duration-seconds: 900
aws-region: us-east-1
output-env-credentials: false
output-credentials: true
- name: Cloudflare token
id: cloudflare-token
env:
CLOUDFLARE_TOKEN_URL: ${{ vars.CLOUDFLARE_TOKEN_URL }}
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
AWS_ACCESS_KEY_ID: ${{ steps.aws.outputs.aws-access-key-id }}
AWS_SECRET_ACCESS_KEY: ${{ steps.aws.outputs.aws-secret-access-key }}
AWS_SESSION_TOKEN: ${{ steps.aws.outputs.aws-session-token }}
run: bash .github/release/cloudflare-token.sh
- name: Deploy
env:
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ steps.cloudflare-token.outputs.token }}
run: bash .github/release/deploy-site.sh "$MODE" "$RUNNER_TEMP/in/site-$MODE"
verify:
needs: deploy
runs-on: ubuntu-24.04
timeout-minutes: 10
env:
MODE: ${{ github.ref_name == 'production' && 'production' || 'dev' }}
steps:
- name: Check the live site
run: |
host=$([ "$MODE" = dev ] && echo dev.psychoscript.com || echo psychoscript.com)
base="https://$host"
status=""
for _ in 1 2 3 4 5 6 7 8 9 10 11 12; do
status=$(curl --silent --max-time 20 --output /dev/null --write-out '%{http_code}' "$base/" || true)
[ "$status" = 200 ] && break
sleep 10
done
echo "$base/ -> $status"
test "$status" = 200
for asset in liquid.js styles.css art/card.jpg robots.txt; do
code=$(curl --silent --max-time 20 --output /dev/null --write-out '%{http_code}' "$base/$asset")
echo "$asset -> $code"
[ "$code" = 200 ] || { echo "::error::Expected 200 from $base/$asset, got $code"; exit 1; }
done
if [ "$MODE" = dev ]; then
headers=$(curl --silent --show-error --max-time 20 --dump-header - --output /dev/null "$base/")
printf '%s\n' "$headers" | grep -iq '^x-robots-tag:.*noindex' || {
echo "::error::Expected X-Robots-Tag: noindex on $base/"
exit 1
}
else
headers=$(curl --silent --show-error --max-time 20 --dump-header - --output /dev/null "https://www.psychoscript.com/")
redir_status=$(printf '%s\n' "$headers" | awk 'NR==1 { print $2 }')
location=$(printf '%s\n' "$headers" | awk 'tolower($1) == "location:" { sub(/\r$/, "", $2); print $2; exit }')
echo "www redirect -> $redir_status $location"
[ "$redir_status" = 301 ] || { echo "::error::Expected 301 from www.psychoscript.com, got $redir_status"; exit 1; }
[ "$location" = "https://psychoscript.com/" ] || { echo "::error::Unexpected www redirect target: $location"; exit 1; }
fi