Repository navigation
220 lines (191 loc) · 7.83 KB
/
Copy pathpreview.yml
File metadata and controls
220 lines (191 loc) · 7.83 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
212
213
214
215
216
217
218
219
220
name: Preview
on:
pull_request:
branches:
- main
permissions:
contents: read
pull-requests: write
env:
PULUMI_VERSION: "3.197.0"
jobs:
preview:
name: Preview Changes
runs-on: ubuntu-latest
# Skip preview for fork PRs and Dependabot - they don't have access to Actions secrets
if: github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]'
# `production` only deploys from main, so it rejects pull request refs.
# `preview` accepts them and holds its own credentials. See
# README.md "Deployment, secrets and keys".
environment: preview
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
- name: Setup Pulumi
uses: pulumi/actions@v6
with:
pulumi-version: ${{ env.PULUMI_VERSION }}
- name: Cache Pulumi plugins
uses: actions/cache@v4
with:
path: ~/.pulumi/plugins
key: pulumi-plugins-${{ hashFiles('Pulumi.yaml') }}
restore-keys: |
pulumi-plugins-
- name: Install Pulumi packages
env:
GITHUB_TOKEN: ${{ github.token }}
run: pulumi install
- name: Install dependencies
run: npm ci
- name: Run validation
run: npm run check
# Deploy secrets live in `production`, which pull requests cannot read. With none of
# them in `preview`, the Pulumi steps are skipped. With only some, fail: the preview
# would show the resources of the missing ones as deletes.
- name: Check for deploy secrets
id: secrets
env:
CLOUDFLARE_ROLE_MANAGEMENT_TOKEN: ${{ secrets.CLOUDFLARE_ROLE_MANAGEMENT_TOKEN }}
DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
DISCORD_GUILD_ID: ${{ secrets.DISCORD_GUILD_ID }}
GCP_PROD_SERVICE_ACCOUNT_KEY: ${{ secrets.GCP_PROD_SERVICE_ACCOUNT_KEY }}
ORG_BILLING_EMAIL: ${{ secrets.ORG_BILLING_EMAIL }}
PULUMI_PROD_PASSPHRASE: ${{ secrets.PULUMI_PROD_PASSPHRASE }}
run: |
missing=""
count=0
for name in CLOUDFLARE_ROLE_MANAGEMENT_TOKEN DISCORD_BOT_TOKEN DISCORD_GUILD_ID \
GCP_PROD_SERVICE_ACCOUNT_KEY ORG_BILLING_EMAIL PULUMI_PROD_PASSPHRASE; do
if [ -z "${!name}" ]; then
missing="$missing $name"
else
count=$((count + 1))
fi
done
if [ "$count" -eq 0 ]; then
echo "::notice::No deploy secrets in the preview environment; skipping the Pulumi preview"
echo "available=false" >> "$GITHUB_OUTPUT"
elif [ -n "$missing" ]; then
echo "::error::Some deploy secrets are missing from the preview environment:$missing"
exit 1
else
echo "available=true" >> "$GITHUB_OUTPUT"
fi
- name: Authenticate to Google Cloud
if: steps.secrets.outputs.available == 'true'
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_PROD_SERVICE_ACCOUNT_KEY }}
# Only what a preview without refresh reads: the organization role list.
# `owner` is required; without it the token only covers this repository.
- name: Create GitHub App token
id: app-token
if: steps.secrets.outputs.available == 'true'
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.DEPLOY_APP_CLIENT_ID }}
private-key: ${{ secrets.DEPLOY_APP_PRIVATE_KEY }}
owner: modelcontextprotocol
permission-metadata: read
permission-organization-custom-org-roles: read
- name: Preview changes
id: preview
if: steps.secrets.outputs.available == 'true'
env:
PULUMI_PASSPHRASE: ${{ secrets.PULUMI_PROD_PASSPHRASE }}
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
ORG_BILLING_EMAIL: ${{ secrets.ORG_BILLING_EMAIL }}
DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
DISCORD_GUILD_ID: ${{ secrets.DISCORD_GUILD_ID }}
CLOUDFLARE_ROLE_MANAGEMENT_TOKEN: ${{ secrets.CLOUDFLARE_ROLE_MANAGEMENT_TOKEN }}
run: |
echo "$PULUMI_PASSPHRASE" > passphrase.prod.txt
pulumi login gs://mcp-access-prod-pulumi-state
# Build config flags for Discord if secrets are available
CONFIG_FLAGS=""
if [ -n "$DISCORD_GUILD_ID" ]; then
CONFIG_FLAGS="$CONFIG_FLAGS --config discord:guildId=$DISCORD_GUILD_ID"
fi
if [ -n "$DISCORD_BOT_TOKEN" ]; then
CONFIG_FLAGS="$CONFIG_FLAGS --config discord:botToken=$DISCORD_BOT_TOKEN"
fi
if [ -n "$ORG_BILLING_EMAIL" ]; then
CONFIG_FLAGS="$CONFIG_FLAGS --config githubBillingEmail=$ORG_BILLING_EMAIL"
fi
if [ -n "$CLOUDFLARE_ROLE_MANAGEMENT_TOKEN" ]; then
CONFIG_FLAGS="$CONFIG_FLAGS --config cloudflare:roleManagementToken=$CLOUDFLARE_ROLE_MANAGEMENT_TOKEN"
fi
# Run preview and capture output
set +e
PREVIEW_OUTPUT=$(PULUMI_CONFIG_PASSPHRASE_FILE=passphrase.prod.txt pulumi preview --stack prod --diff $CONFIG_FLAGS 2>&1)
PREVIEW_EXIT_CODE=$?
set -e
# Save output for comment
echo "exit_code=$PREVIEW_EXIT_CODE" >> $GITHUB_OUTPUT
# Write preview to file (handles multiline)
echo "$PREVIEW_OUTPUT" > preview_output.txt
# Also print to logs
echo "$PREVIEW_OUTPUT"
# Exit with preview exit code
exit $PREVIEW_EXIT_CODE
- name: Comment on PR
if: ${{ !cancelled() }}
uses: actions/github-script@v7
env:
PREVIEW_SKIPPED: ${{ steps.secrets.outputs.available == 'false' }}
with:
script: |
const fs = require('fs');
let output = '';
try {
output = fs.readFileSync('preview_output.txt', 'utf8');
} catch (e) {
output = 'Failed to read preview output';
}
// Truncate if too long for GitHub comment
const maxLength = 60000;
if (output.length > maxLength) {
output = output.substring(0, maxLength) + '\n\n... (truncated)';
}
// Say so on any comment an earlier run left, so nobody reviews an outdated plan
const skipped = process.env.PREVIEW_SKIPPED === 'true';
const body = skipped
? '## Pulumi Preview\n\nNo preview ran for this commit: the preview environment has no deploy secrets.'
: `## Pulumi Preview
<details>
<summary>Click to expand preview output</summary>
\`\`\`
${output}
\`\`\`
</details>
`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('## Pulumi Preview')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body
});
} else if (!skipped) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});
}