|
| 1 | +name: Codex Preview |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + codex-env: |
| 7 | + description: The environment to build the codex for |
| 8 | + required: false |
| 9 | + default: "engineering" |
| 10 | + type: string |
| 11 | + path-pattern: |
| 12 | + description: The path pattern to check for changes |
| 13 | + required: false |
| 14 | + default: '**' |
| 15 | + type: string |
| 16 | + path-pattern-ignore: |
| 17 | + description: The path pattern to ignore changes |
| 18 | + required: false |
| 19 | + default: '' |
| 20 | + type: string |
| 21 | + |
| 22 | +permissions: |
| 23 | + id-token: write |
| 24 | + deployments: write |
| 25 | + contents: read |
| 26 | + pull-requests: write |
| 27 | + |
| 28 | +jobs: |
| 29 | + check: |
| 30 | + permissions: |
| 31 | + pull-requests: read |
| 32 | + contents: read |
| 33 | + runs-on: ubuntu-slim |
| 34 | + outputs: |
| 35 | + any_modified: ${{ steps.check-files.outputs.any_modified }} |
| 36 | + steps: |
| 37 | + - name: Checkout |
| 38 | + # Checkout is needed to get changed files when the event is not a pull request |
| 39 | + if: contains(fromJSON('["push", "merge_group", "workflow_dispatch"]'), github.event_name) |
| 40 | + uses: actions/checkout@v6 |
| 41 | + - name: Check changes |
| 42 | + id: check-files |
| 43 | + uses: tj-actions/changed-files@v47 |
| 44 | + with: |
| 45 | + files: ${{ inputs.path-pattern }} |
| 46 | + files_ignore: | |
| 47 | + ${{ inputs.path-pattern-ignore }} |
| 48 | + .github/** |
| 49 | + README.md |
| 50 | + build: |
| 51 | + needs: check |
| 52 | + if: needs.check.outputs.any_modified == 'true' |
| 53 | + outputs: |
| 54 | + path_prefix: ${{ steps.generate-path-prefix.outputs.result }} |
| 55 | + permissions: |
| 56 | + contents: read |
| 57 | + runs-on: ubuntu-latest |
| 58 | + steps: |
| 59 | + - name: Checkout code |
| 60 | + # We would need to checkout the PR branch if the event to support pull_request_target. |
| 61 | + # Skip this for now because it's not clear if we need to or should support it. |
| 62 | + # However, the rest of the workflow is designed to support pull_request_target. |
| 63 | + uses: actions/checkout@v6 |
| 64 | + - name: Setup docs-builder |
| 65 | + uses: elastic/docs-actions/docs-builder/setup@main |
| 66 | + with: |
| 67 | + version: edge |
| 68 | + - name: Generate env.PATH_PREFIX |
| 69 | + id: generate-path-prefix |
| 70 | + env: |
| 71 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 72 | + GITHUB_REF_NAME: ${{ github.ref_name }} |
| 73 | + run: | |
| 74 | + case "${GITHUB_EVENT_NAME}" in |
| 75 | + "merge_group" | "pull_request" | "pull_request_target") |
| 76 | + path_prefix="/_preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" |
| 77 | + ;; |
| 78 | + "push" | "workflow_dispatch") |
| 79 | + path_prefix="/_preview/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" |
| 80 | + ;; |
| 81 | + *) |
| 82 | + echo "Unsupported event: '${GITHUB_EVENT_NAME}'"; |
| 83 | + exit 1; |
| 84 | + ;; |
| 85 | + esac |
| 86 | + echo "PATH_PREFIX=${path_prefix}" >> $GITHUB_ENV |
| 87 | + echo "result=${path_prefix}" >> $GITHUB_OUTPUT |
| 88 | + - name: Build |
| 89 | + run: docs-builder --output ./.artifacts/docs/html --path-prefix ${{ env.PATH_PREFIX }} |
| 90 | + env: |
| 91 | + PATH_PREFIX: ${{ env.PATH_PREFIX }} |
| 92 | + - name: Upload docs |
| 93 | + uses: actions/upload-artifact@v6 |
| 94 | + with: |
| 95 | + name: docs |
| 96 | + path: .artifacts/docs/html |
| 97 | + retention-days: 1 |
| 98 | + - name: Upload artifact |
| 99 | + uses: actions/upload-artifact@v6 |
| 100 | + with: |
| 101 | + name: links |
| 102 | + path: .artifacts/docs/html/links.json |
| 103 | + retention-days: 1 |
| 104 | + - name: Warn about fork PR |
| 105 | + if: > |
| 106 | + startsWith(github.event_name, 'pull_request') |
| 107 | + && github.event.pull_request.head.repo.full_name != github.repository |
| 108 | + run: | |
| 109 | + echo "::warning::Preview deployments are only available for PRs from the same repository, not from forks." \ |
| 110 | + "You can either change the event to pull_request_target or push the branch to the upstream repository instead." |
| 111 | +
|
| 112 | + deploy: |
| 113 | + if: > |
| 114 | + contains(fromJSON('["pull_request", "pull_request_target"]'), github.event_name) |
| 115 | + && (github.event_name == 'pull_request_target' || github.event.pull_request.head.repo.full_name == github.repository) |
| 116 | + permissions: |
| 117 | + id-token: write |
| 118 | + deployments: write |
| 119 | + needs: build |
| 120 | + runs-on: ubuntu-latest |
| 121 | + steps: |
| 122 | + - name: Create Deployment |
| 123 | + if: contains(fromJSON('["pull_request", "pull_request_target", "workflow_dispatch"]'), github.event_name) |
| 124 | + uses: actions/github-script@v8 |
| 125 | + id: deployment |
| 126 | + with: |
| 127 | + result-encoding: string |
| 128 | + script: | |
| 129 | + const { owner, repo } = context.repo; |
| 130 | + const prNumber = process.env.PR_NUMBER; |
| 131 | + const environment = 'codex-preview'; |
| 132 | + const task = prNumber ? `codex-preview-${prNumber}` : undefined; |
| 133 | + const deployment = await github.rest.repos.createDeployment({ |
| 134 | + owner, |
| 135 | + repo, |
| 136 | + environment, |
| 137 | + task, |
| 138 | + ref: process.env.REF, |
| 139 | + auto_merge: false, |
| 140 | + transient_environment: true, |
| 141 | + required_contexts: [], |
| 142 | + }) |
| 143 | + await github.rest.repos.createDeploymentStatus({ |
| 144 | + deployment_id: deployment.data.id, |
| 145 | + owner, |
| 146 | + repo, |
| 147 | + state: "in_progress", |
| 148 | + log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, |
| 149 | + }) |
| 150 | + return deployment.data.id |
| 151 | + env: |
| 152 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 153 | + REF: ${{ startsWith(github.event_name, 'pull_request') && github.event.pull_request.head.sha || github.ref_name }} |
| 154 | + - name: Download artifact |
| 155 | + uses: actions/download-artifact@v6 |
| 156 | + with: |
| 157 | + name: docs |
| 158 | + path: docs |
| 159 | + - uses: elastic/docs-actions/aws/auth@main |
| 160 | + with: |
| 161 | + aws_role_name_prefix: codex-eng-preview- |
| 162 | + - name: AWS sync docs to S3 |
| 163 | + id: s3-upload |
| 164 | + run: | |
| 165 | + aws s3 sync docs "s3://elastic-codex-website-engineering${PATH_PREFIX}" --delete --no-follow-symlinks |
| 166 | + aws cloudfront create-invalidation \ |
| 167 | + --distribution-id E1BSBYLPE05D2R \ |
| 168 | + --paths "${PATH_PREFIX}" "${PATH_PREFIX}/*" |
| 169 | + env: |
| 170 | + PATH_PREFIX: ${{ needs.build.outputs.path_prefix }} |
| 171 | + AWS_RETRY_MODE: standard |
| 172 | + AWS_MAX_ATTEMPTS: 3 |
| 173 | + - name: Update deployment status |
| 174 | + uses: actions/github-script@v8 |
| 175 | + if: always() && steps.deployment.outputs.result |
| 176 | + env: |
| 177 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 178 | + PATH_PREFIX: ${{ needs.build.outputs.path_prefix }} |
| 179 | + with: |
| 180 | + script: | |
| 181 | + await github.rest.repos.createDeploymentStatus({ |
| 182 | + owner: context.repo.owner, |
| 183 | + repo: context.repo.repo, |
| 184 | + deployment_id: ${{ steps.deployment.outputs.result }}, |
| 185 | + state: "${{ steps.s3-upload.outcome == 'success' && 'success' || 'failure' }}", |
| 186 | + environment_url: `https://codex.elastic.dev${process.env.PATH_PREFIX}`, |
| 187 | + log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, |
| 188 | + }) |
| 189 | +
|
| 190 | + update-link-index: |
| 191 | + concurrency: |
| 192 | + group: codex-upload-link-index |
| 193 | + needs: build |
| 194 | + permissions: |
| 195 | + id-token: write |
| 196 | + if: > |
| 197 | + github.ref == 'refs/heads/main' |
| 198 | + && contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) |
| 199 | + && github.event.repository.fork == false |
| 200 | + runs-on: ubuntu-slim |
| 201 | + steps: |
| 202 | + - uses: elastic/docs-actions/codex/update-link-index@main |
| 203 | + with: |
| 204 | + codex-env: ${{ inputs.codex-env }} |
0 commit comments