-
Notifications
You must be signed in to change notification settings - Fork 6
194 lines (188 loc) · 7.06 KB
/
codex-preview.yml
File metadata and controls
194 lines (188 loc) · 7.06 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
name: Codex Preview
on:
workflow_call:
inputs:
codex-env:
description: The environment to build the codex for
required: false
default: "internal"
type: string
docs-builder-version:
description: Version of docs-builder to use (e.g. edge, 1.2.3)
required: false
default: "edge"
type: string
path-pattern:
description: The path pattern to check for changes
required: false
default: '**'
type: string
path-pattern-ignore:
description: The path pattern to ignore changes
required: false
default: ''
type: string
concurrency:
group: ${{github.workflow}}-${{ github.ref }}
permissions:
id-token: write
deployments: write
contents: read
pull-requests: write
jobs:
check:
permissions:
pull-requests: read
contents: read
runs-on: ubuntu-slim
outputs:
any_modified: ${{ steps.check-files.outputs.any_modified }}
steps:
- name: Checkout
# Checkout is needed to get changed files when the event is not a pull request
if: contains(fromJSON('["push", "merge_group", "workflow_dispatch"]'), github.event_name)
uses: actions/checkout@v6
- name: Check changes
id: check-files
uses: tj-actions/changed-files@v47.0.6
with:
files: ${{ inputs.path-pattern }}
files_ignore: |
${{ inputs.path-pattern-ignore }}
.github/**
README.md
build:
needs: check
outputs:
path_prefix: ${{ steps.codex-build.outputs.path-prefix }}
permissions:
contents: read
id-token: write
runs-on: ${{ needs.check.outputs.any_modified == 'true' && 'ubuntu-latest' || 'ubuntu-slim' }}
steps:
- name: Checkout code
if: needs.check.outputs.any_modified == 'true'
# We would need to checkout the PR branch if the event to support pull_request_target.
# Skip this for now because it's not clear if we need to or should support it.
# However, the rest of the workflow is designed to support pull_request_target.
uses: actions/checkout@v6
- uses: elastic/docs-actions/codex/build@v1
if: needs.check.outputs.any_modified == 'true'
id: codex-build
with:
docs-builder-version: ${{ inputs.docs-builder-version }}
- name: Warn about fork PR
if: >
startsWith(github.event_name, 'pull_request')
&& github.event.pull_request.head.repo.full_name != github.repository
&& needs.check.outputs.any_modified == 'true'
run: |
echo "::warning::Preview deployments are only available for PRs from the same repository, not from forks." \
"You can either change the event to pull_request_target or push the branch to the upstream repository instead."
- name: Skip build
if: needs.check.outputs.any_modified == 'false'
run: |
echo "::warning::No changes detected, skipping build."
deploy:
if: >
needs.check.outputs.any_modified == 'true'
&& contains(fromJSON('["pull_request", "pull_request_target"]'), github.event_name)
&& (github.event_name == 'pull_request_target' || github.event.pull_request.head.repo.full_name == github.repository)
permissions:
id-token: write
deployments: write
needs:
- check
- build
runs-on: ubuntu-latest
steps:
- name: Create Deployment
if: contains(fromJSON('["pull_request", "pull_request_target", "workflow_dispatch"]'), github.event_name)
uses: actions/github-script@v9
id: deployment
with:
result-encoding: string
script: |
const { owner, repo } = context.repo;
const prNumber = process.env.PR_NUMBER;
const environment = 'codex-preview';
const task = prNumber ? `codex-preview-${prNumber}` : undefined;
const deployment = await github.rest.repos.createDeployment({
owner,
repo,
environment,
task,
ref: process.env.REF,
auto_merge: false,
transient_environment: true,
required_contexts: [],
})
await github.rest.repos.createDeploymentStatus({
deployment_id: deployment.data.id,
owner,
repo,
state: "in_progress",
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
})
return deployment.data.id
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
REF: ${{ startsWith(github.event_name, 'pull_request') && github.event.pull_request.head.sha || github.ref_name }}
- name: Download artifact
uses: actions/download-artifact@v8
with:
name: docs
path: docs
- name: Reject symlinks in build output
run: |
if find docs -type l | grep -q .; then
echo "::error::Symlinks found in build output — refusing to deploy"
find docs -type l -ls
exit 1
fi
- uses: elastic/docs-actions/aws/auth@v1
with:
aws_role_name_prefix: codex-eng-preview-
- name: AWS sync docs to S3
id: s3-upload
run: |
aws s3 sync docs "s3://elastic-codex-website-internal${PATH_PREFIX}" --delete --no-follow-symlinks
aws cloudfront create-invalidation \
--distribution-id E1BSBYLPE05D2R \
--paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
env:
PATH_PREFIX: ${{ needs.build.outputs.path_prefix }}
AWS_RETRY_MODE: standard
AWS_MAX_ATTEMPTS: 3
- name: Update deployment status
uses: actions/github-script@v9
if: always() && steps.deployment.outputs.result
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
PATH_PREFIX: ${{ needs.build.outputs.path_prefix }}
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.deployment.outputs.result }},
state: "${{ steps.s3-upload.outcome == 'success' && 'success' || 'failure' }}",
environment_url: `https://codex.elastic.dev${process.env.PATH_PREFIX}`,
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
})
update-link-index:
needs:
- build
- check
permissions:
id-token: write
if: >
needs.check.outputs.any_modified == 'true'
&& github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
&& contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name)
&& github.event.repository.fork == false
runs-on: ubuntu-slim
steps:
- uses: elastic/docs-actions/codex/update-link-index@v1
with:
codex-env: ${{ inputs.codex-env }}