Skip to content

preview-deploy

preview-deploy #95

name: preview-deploy
on:
workflow_call: ~
workflow_run:
workflows: [preview-build]
types:
- completed
permissions:
contents: none
id-token: write
deployments: write
actions: read
jobs:
pull-request-data:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
outputs:
number: ${{ steps.pull_request.outputs.number }}
ref: ${{ steps.pull_request.outputs.ref }}
any_changed: ${{ steps.pull_request.outputs.any_changed }}
steps:
- name: Download PR data
env:
GH_TOKEN: ${{ github.token }}
run: |
gh run download ${{ github.event.workflow_run.id }} \
--repo "${GITHUB_REPOSITORY}" \
--name pull-request-data
- name: Get PR data
id: pull_request
run: |
{
echo "number=$(jq -r '.number' pull_request.json)"
echo "ref=$(jq -r '.ref' pull_request.json)"
echo "any_changed=$(jq -r '.any_changed' pull_request.json)"
} >> "${GITHUB_OUTPUT}"
deploy:
needs: pull-request-data
if: needs.pull-request-data.outputs.any_changed == 'true'
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ needs.pull-request-data.outputs.number }}
cancel-in-progress: true
steps:
- name: Create Deployment
uses: actions/github-script@v7
id: deployment
env:
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
PR_REF: ${{ needs.pull-request-data.outputs.ref }}
with:
result-encoding: string
script: |
const { owner, repo } = context.repo;
const deployment = await github.rest.repos.createDeployment({
owner,
repo,
ref: process.env.PR_REF,
environment: `docs-preview-${process.env.PR_NUMBER}`,
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.pull-request-data.outputs.number }}
run: |
aws s3 sync ./html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
- name: Update deployment status
uses: actions/github-script@v7
if: always() && steps.deployment.outputs.result
env:
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
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/${context.repo.owner}/${context.repo.repo}/pull/${process.env.PR_NUMBER}`,
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
})