Skip to content

Add preview workflow #30

Add preview workflow

Add preview workflow #30

Workflow file for this run

name: preview
on:
pull_request: ~
permissions:
contents: read
packages: read
id-token: write
pull-requests: write
deployments: write
env:
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Create Deployment
uses: actions/github-script@v7
id: deployment
with:
result-encoding: string
script: |
const response = await github.rest.repos.createDeployment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
ref: "${{ github.event.pull_request.head.ref }}",
environment: `preview-${context.issue.number}`,
description: `Preview deployment for PR ${context.issue.number}`,
auto_merge: false,
})
await github.rest.repos.createDeploymentStatus({
deployment_id: response.data.id,
owner: context.repo.owner,
repo: context.repo.repo,
state: "in_progress",
description: "Deployment created",
})
return response.data.id
# - name: Delete a comment if exists
# uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
# with:
# comment-tag: docs-builder-preview
# mode: delete
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
id: bootstrap
uses: ./.github/actions/bootstrap
- name: Publish AOT
run: ./build.sh publishbinaries
# we run our artifact directly please use the prebuild
# elastic/docs-builder@main GitHub Action for all other repositories!
- name: Build documentation
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: .artifacts/publish/docs-builder/release/docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
- name: Generate ARNs
id: generate_arns
shell: python
env:
AWS_ACCOUNT_ID: 197730964718
run: |
import hashlib
import os
prefix = "elastic-docs-v3-preview-"
aws_account_id = os.environ["AWS_ACCOUNT_ID"]
m = hashlib.sha256()
m.update(os.environ["GITHUB_REPOSITORY"].encode('utf-8'))
hash = m.hexdigest()[:64-len(prefix)]
name = f"{prefix}{hash}"
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
print(f"role_arn=arn:aws:iam::{aws_account_id}:role/{name}", file=f)
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: ${{ steps.generate_arns.outputs.role_arn }}
aws-region: us-east-1
- name: Upload to S3
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
aws s3 sync .artifacts/docs/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: steps.deployment.outputs.result
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.deployment.outputs.result }},
state: "success",
description: "Deployment completed",
environment_url: `https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number}}`,
})
- name: Update Deployment Status on Failure
if: failure() && steps.deployment.outputs.result
uses: actions/github-script@v7
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.deployment.outputs.result }},
state: "failure",
description: "Deployment failed",
})
# - name: Comment PR with execution number
# uses: thollander/actions-comment-pull-request@v3
# with:
# message: |
# https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number }}
# comment-tag: docs-builder-preview