|
| 1 | +name: 'Documentation Preview' |
| 2 | +description: 'Builds and publishes documentation to preview environment' |
| 3 | + |
| 4 | +branding: |
| 5 | + icon: 'filter' |
| 6 | + color: 'red' |
| 7 | + |
| 8 | +inputs: |
| 9 | + strict: |
| 10 | + description: 'Whether to fail the build if there are any warnings' |
| 11 | + default: true |
| 12 | + required: false |
| 13 | + |
| 14 | +runs: |
| 15 | + using: "composite" |
| 16 | + steps: |
| 17 | + - name: Create Deployment |
| 18 | + uses: actions/github-script@v7 |
| 19 | + id: deployment |
| 20 | + with: |
| 21 | + result-encoding: string |
| 22 | + script: | |
| 23 | + const { owner, repo } = context.repo; |
| 24 | + const branch = context.payload.pull_request |
| 25 | + ? context.payload.pull_request.head.ref |
| 26 | + : context.ref.replace('refs/heads/', ''); |
| 27 | +
|
| 28 | + const repoResponse = await github.rest.repos.get({ |
| 29 | + owner, |
| 30 | + repo |
| 31 | + }) |
| 32 | + const defaultBranch = repoResponse.data.default_branch; |
| 33 | + const isDefaultBranch = branch === defaultBranch; |
| 34 | +
|
| 35 | + const environment = isDefaultBranch |
| 36 | + ? `docs-preview-${defaultBranch}` |
| 37 | + : `docs-preview-${context.issue.number}`; |
| 38 | +
|
| 39 | + const logUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 40 | +
|
| 41 | + const deployment = await github.rest.repos.createDeployment({ |
| 42 | + owner, |
| 43 | + repo, |
| 44 | + ref: branch, |
| 45 | + environment, |
| 46 | + auto_merge: false, |
| 47 | + required_contexts: [], |
| 48 | + }) |
| 49 | + await github.rest.repos.createDeploymentStatus({ |
| 50 | + deployment_id: deployment.data.id, |
| 51 | + owner, |
| 52 | + repo, |
| 53 | + state: "in_progress", |
| 54 | + log_url: logUrl, |
| 55 | + }) |
| 56 | + return deployment.data.id |
| 57 | +
|
| 58 | + - name: Generate Path Prefix |
| 59 | + id: path-prefix |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + if [[ -n "${{ github.event.pull_request }}" ]]; then |
| 63 | + echo "result=/${GITHUB_REPOSITORY}/pull/${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT |
| 64 | + else |
| 65 | + echo "result=/${GITHUB_REPOSITORY}/${{ github.ref_name }}" >> $GITHUB_OUTPUT |
| 66 | + fi |
| 67 | +
|
| 68 | + - name: Build public documentation |
| 69 | + uses: elastic/docs-builder@main |
| 70 | + continue-on-error: ${{ inputs.strict != 'true' }} |
| 71 | + with: |
| 72 | + prefix: ${{ steps.path-prefix.outputs.result }} |
| 73 | + strict: ${{ inputs.strict }} |
| 74 | + |
| 75 | + - name: Authenticate |
| 76 | + uses: elastic/docs-builder/.github/actions/aws-auth@main |
| 77 | + |
| 78 | + - name: Upload artifact |
| 79 | + shell: bash |
| 80 | + run: | |
| 81 | + aws s3 sync .artifacts/docs/html s3://elastic-docs-v3-website-preview${{steps.path-prefix.outputs.result}} --delete --quiet |
| 82 | + aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "${{steps.path-prefix.outputs.result}}/*" |
| 83 | +
|
| 84 | + - name: Update deployment status |
| 85 | + uses: actions/github-script@v7 |
| 86 | + if: always() |
| 87 | + with: |
| 88 | + script: | |
| 89 | + await github.rest.repos.createDeploymentStatus({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + deployment_id: ${{ steps.deployment.outputs.result }}, |
| 93 | + state: '${{ job.status == 'success' && 'success' || 'failure' }}', |
| 94 | + environment_url: `https://docs-v3-preview.elastic.dev${{steps.path-prefix.outputs.result}}`, |
| 95 | + log_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}', |
| 96 | + description: "Deployment completed", |
| 97 | + }) |
0 commit comments