preview-deploy #162
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: preview-deploy | |
| on: | |
| workflow_call: ~ | |
| workflow_run: | |
| workflows: [preview-build] | |
| types: | |
| - completed | |
| permissions: | |
| contents: none | |
| id-token: write | |
| deployments: write | |
| actions: read | |
| jobs: | |
| deployment-metadata: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| outputs: | |
| pr_number: ${{ steps.metadata.outputs.pr_number }} | |
| ref: ${{ steps.metadata.outputs.ref }} | |
| should_deploy: ${{ steps.metadata.outputs.should_deploy }} | |
| path_prefix: ${{ steps.metadata.outputs.path_prefix }} | |
| steps: | |
| - name: Download deployment metadata | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| gh run download ${{ github.event.workflow_run.id }} \ | |
| --repo "${GITHUB_REPOSITORY}" \ | |
| --name deployment_metadata | |
| - name: Get deployment metadata | |
| id: metadata | |
| run: | | |
| { | |
| echo "pr_number=$(jq -r '.pr_number' deployment_metadata.json)" | |
| echo "ref=$(jq -r '.ref' deployment_metadata.json)" | |
| echo "path_prefix=$(jq -r '.path_prefix' deployment_metadata.json)" | |
| echo "should_deploy=$(jq -r '.should_deploy' deployment_metadata.json)" | |
| } >> "${GITHUB_OUTPUT}" | |
| deploy: | |
| needs: deployment-metadata | |
| if: needs.deployment-metadata.outputs.should_deploy == 'true' | |
| runs-on: ubuntu-latest | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ needs.deployment-metadata.outputs.pr_number }} | |
| cancel-in-progress: true | |
| steps: | |
| - name: Create Deployment | |
| uses: actions/github-script@v7 | |
| id: deployment | |
| env: | |
| PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }} | |
| REF: ${{ needs.deployment-metadata.outputs.ref }} | |
| with: | |
| result-encoding: string | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const prNumber = process.env.PR_NUMBER; | |
| const environment = prNumber ? `docs-preview-${prNumber}` : 'docs-preview'; | |
| const deployment = await github.rest.repos.createDeployment({ | |
| owner, | |
| repo, | |
| environment, | |
| ref: process.env.REF, | |
| auto_merge: false, | |
| 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 | |
| - name: Download docs | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| gh run download ${{ github.event.workflow_run.id }} \ | |
| --repo "${GITHUB_REPOSITORY}" \ | |
| --name docs \ | |
| --dir html | |
| - uses: elastic/docs-builder/.github/actions/aws-auth@main | |
| - name: Upload to S3 | |
| env: | |
| PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }} | |
| PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }} | |
| run: | | |
| aws s3 sync ./html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete | |
| aws cloudfront create-invalidation \ | |
| --distribution-id EKT7LT5PM8RKS \ | |
| --paths "${PATH_PREFIX}" "/${PATH_PREFIX}/*" | |
| - name: Update deployment status | |
| uses: actions/github-script@v7 | |
| if: always() && steps.deployment.outputs.result | |
| env: | |
| PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }} | |
| PATH_PREFIX: ${{ needs.deployment-metadata.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: "${{ job.status == 'success' && 'success' || 'failure' }}", | |
| environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`, | |
| log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, | |
| }) |