|
| 1 | +name: preview-deploy |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: ~ |
| 5 | + workflow_run: |
| 6 | + workflows: [preview-build] |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: none |
| 12 | + id-token: write |
| 13 | + deployments: write |
| 14 | + actions: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + docs-deploy: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Download docs |
| 21 | + env: |
| 22 | + GH_TOKEN: ${{ github.token }} |
| 23 | + run: | |
| 24 | + gh run download ${{ github.event.workflow_run.id }} --name docs --repo "${GITHUB_REPOSITORY}" |
| 25 | + - name: Get PR data |
| 26 | + id: pull_request |
| 27 | + run: | |
| 28 | + echo "number=$(cat pull_request_number.txt)" >> "${GITHUB_OUTPUT}" |
| 29 | + echo "ref=$(cat pull_request_ref.txt)" >> "${GITHUB_OUTPUT}" |
| 30 | +
|
| 31 | + - name: Create Deployment |
| 32 | + uses: actions/github-script@v7 |
| 33 | + id: deployment |
| 34 | + env: |
| 35 | + PR_NUMBER: ${{ steps.pull_request.outputs.number }} |
| 36 | + PR_REF: ${{ steps.pull_request.outputs.ref }} |
| 37 | + with: |
| 38 | + result-encoding: string |
| 39 | + script: | |
| 40 | + const { owner, repo } = context.repo; |
| 41 | + const deployment = await github.rest.repos.createDeployment({ |
| 42 | + owner, |
| 43 | + repo, |
| 44 | + ref: process.env.PR_REF, |
| 45 | + environment: `docs-preview-${process.env.PR_NUMBER}`, |
| 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: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, |
| 55 | + }) |
| 56 | + return deployment.data.id |
| 57 | +
|
| 58 | + - uses: elastic/docs-builder/.github/actions/aws-auth@main |
| 59 | + |
| 60 | + - name: Upload to S3 |
| 61 | + env: |
| 62 | + PR_NUMBER: ${{ steps.pull_request.outputs.number }} |
| 63 | + run: | |
| 64 | + aws s3 sync . "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete --exclude "pull_request_number.txt" |
| 65 | + aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*" |
| 66 | +
|
| 67 | + - name: Update deployment status |
| 68 | + uses: actions/github-script@v7 |
| 69 | + if: always() && steps.deployment.outputs.result |
| 70 | + with: |
| 71 | + script: | |
| 72 | + await github.rest.repos.createDeploymentStatus({ |
| 73 | + owner: context.repo.owner, |
| 74 | + repo: context.repo.repo, |
| 75 | + deployment_id: ${{ steps.deployment.outputs.result }}, |
| 76 | + state: "${{ job.status == 'success' && 'success' || 'failure' }}", |
| 77 | + environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${{ steps.pull_request.outputs.number}}`, |
| 78 | + log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, |
| 79 | + }) |
0 commit comments