forked from strands-agents/harness-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
203 lines (174 loc) · 6.87 KB
/
Copy pathdocs-deploy-preview.yml
File metadata and controls
203 lines (174 loc) · 6.87 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
name: "Docs: Deploy Preview"
on:
pull_request_target:
types: [opened, reopened, synchronize]
paths:
- 'site/**'
- '.github/workflows/docs-*'
concurrency:
group: preview-cms-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
authorization-check:
name: Authorization Check
permissions: read-all
runs-on: ubuntu-latest
outputs:
approval-env: ${{ steps.auth.outputs.approval-env }}
steps:
- name: Check Authorization
id: auth
uses: strands-agents/devtools/authorization-check@main
with:
username: ${{ github.event.pull_request.user.login }}
allowed-roles: 'write,maintain,admin'
build-and-deploy:
name: Build and Deploy Preview
runs-on: ubuntu-latest
needs: authorization-check
environment: ${{ needs.authorization-check.outputs.approval-env }}
permissions:
contents: read
issues: write
pull-requests: write
id-token: write # For OIDC authentication
env:
# PR metadata
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
RUN_ID: ${{ github.run_id }}
# Secrets
AWS_DEPLOY_ROLE: ${{ secrets.STRANDS_DOCS_DEPLOY_ROLE }}
S3_BUCKET: ${{ secrets.STRANDS_DOCS_BUCKET }}
CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }}
CLOUDFRONT_DOMAIN: ${{ secrets.CLOUDFRONT_DOMAIN }}
# Derived paths (defined once, used everywhere)
BUILD_OUTPUT_DIR: site/dist
S3_PATH: pr-cms-${{ github.event.pull_request.number }}
PREVIEW_URL: https://${{ secrets.CLOUDFRONT_DOMAIN }}/pr-cms-${{ github.event.pull_request.number }}/docs/user-guide/quickstart/overview/
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
defaults:
run:
working-directory: site
steps:
- name: Checkout head commit
uses: actions/checkout@v7
with:
ref: ${{ env.PR_HEAD_SHA }}
persist-credentials: false
allow-unsafe-pr-checkout: true # opt back into fork-PR checkout, blocked by default in checkout@v7
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: '22'
- name: Install dependencies
run: npm ci
- name: Build CMS
env:
ASTRO_BASE_PATH: /${{ env.S3_PATH }}/
run: npm run cms:build
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ env.AWS_DEPLOY_ROLE }}
role-session-name: GitHubActions-Docs-${{ env.RUN_ID }}
aws-region: us-east-1
mask-aws-account-id: true
- name: Deploy to S3
working-directory: .
run: |
aws s3 sync ${{ env.BUILD_OUTPUT_DIR }}/ s3://${{ env.S3_BUCKET }}/${{ env.S3_PATH }}/ \
--cache-control "public, max-age=3600"
- name: Invalidate CloudFront cache for PR preview
working-directory: .
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ env.CLOUDFRONT_DISTRIBUTION_ID }} \
--invalidation-batch '{"Paths":{"Quantity":1,"Items":["/${{ env.S3_PATH }}/*"]},"CallerReference":"'$(date +%s)'"}'
- name: Output deployment info
run: |
echo "Deployment complete!"
echo "Deployed to: ${{ env.PREVIEW_URL }}"
echo "This is a preview deployment for PR #${{ env.PR_NUMBER }}"
- name: Comment on PR (success)
if: success()
uses: actions/github-script@v9
with:
script: |
const prNumber = context.payload.pull_request.number;
const baseUrl = 'https://${{ env.CLOUDFRONT_DOMAIN }}/${{ env.S3_PATH }}';
// Get changed files in the PR
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
// Build preview links for changed doc pages
const docFiles = files
.filter(f => f.filename.startsWith('site/src/content/docs/') && f.filename.endsWith('.mdx'))
.filter(f => f.status !== 'removed');
const previewLinks = docFiles.map(f => {
const urlPath = f.filename
.replace('site/src/content/docs/', '/docs/')
.replace(/\/index\.mdx$/, '/')
.replace(/\.mdx$/, '/');
const label = urlPath.replace(/^\/docs\//, '').replace(/\/$/, '');
return `- [${label}](${baseUrl}${urlPath})`;
});
const fallbackUrl = `${baseUrl}/docs/user-guide/quickstart/overview/`;
// Find existing bot comment
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Documentation Preview')
);
const lines = [
'## Documentation Preview Ready',
'',
'Your documentation preview has been successfully deployed!',
'',
];
if (previewLinks.length > 0) {
lines.push('**Changed pages:**', '', ...previewLinks);
} else {
lines.push(`**Preview URL**: ${fallbackUrl}`);
}
lines.push('', `_Updated at: ${new Date().toISOString()}_`);
const body = lines.join('\n');
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body
});
}
- name: Comment on PR (failure)
if: failure()
uses: actions/github-script@v9
with:
script: |
const prNumber = context.payload.pull_request.number;
const runUrl = '${{ env.RUN_URL }}';
const body = [
'## Documentation Preview Failed',
'',
`The documentation deployment encountered an error. Please check the [deployment logs](${runUrl}) for more details.`
].join('\n');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body
});