-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add preview workflow #279
Merged
+205
−5
Merged
Add preview workflow #279
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d479691
Create action to authenticate with AWS using the generated role name
reakaleek 5126893
Create preview reusable workflow
reakaleek fa2093d
Trigger reusable workflow in PR workflow
reakaleek c0a2f5b
Add cleanup workflow
reakaleek eba1e99
Upload binary instead of built documentation
reakaleek 3debe83
Remove empty newline
reakaleek cf69668
fix
reakaleek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: AWS Auth | ||
|
||
description: | | ||
This is an opinionated action to authenticate with AWS. | ||
It will generate a role ARN based on the repository name and the AWS account ID. | ||
|
||
inputs: | ||
aws_account_id: | ||
description: 'The AWS account ID to generate the role ARN for' | ||
required: true | ||
default: '197730964718' # elastic-web | ||
aws_region: | ||
description: 'The AWS region to use' | ||
required: false | ||
default: 'us-east-1' | ||
aws_role_name_prefix: | ||
description: 'The prefix for the role name' | ||
required: false | ||
default: 'elastic-docs-v3-preview-' | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Generate AWS Role ARN | ||
id: role_arn | ||
shell: python | ||
env: | ||
AWS_ACCOUNT_ID: ${{ inputs.aws_account_id }} | ||
ROLE_NAME_PREFIX: ${{ inputs.aws_role_name_prefix }} | ||
run: | | ||
import hashlib | ||
import os | ||
prefix = os.environ["ROLE_NAME_PREFIX"] | ||
m = hashlib.sha256() | ||
m.update(os.environ["GITHUB_REPOSITORY"].encode('utf-8')) | ||
hash = m.hexdigest()[:64-len(prefix)] | ||
with open(os.environ["GITHUB_OUTPUT"], "a") as f: | ||
f.write(f"result=arn:aws:iam::{os.environ["AWS_ACCOUNT_ID"]}:role/{prefix}{hash}") | ||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2 | ||
with: | ||
role-to-assume: ${{ steps.role_arn.outputs.result }} | ||
aws-region: ${{ inputs.aws_region }} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
name: preview-cleanup | ||
|
||
on: | ||
pull_request_target: | ||
types: [closed] | ||
|
||
permissions: | ||
deployments: write | ||
id-token: write | ||
|
||
jobs: | ||
cleanup: | ||
runs-on: ubuntu-latest | ||
environment: preview-${{ github.event.pull_request.number }} | ||
steps: | ||
- uses: ./.github/actions/aws-auth | ||
- name: Delete s3 objects | ||
env: | ||
PR_NUMBER: ${{ github.event.pull_request.number }} | ||
run: | | ||
aws s3 rm "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --recursive | ||
|
||
- name: Delete GitHub environment | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const { owner, repo } = context.repo; | ||
const deployments = await github.rest.repos.listDeployments({ | ||
owner, | ||
repo, | ||
environment: `preview-${context.issue.number}` | ||
}); | ||
for (const deployment of deployments.data) { | ||
await github.rest.repos.createDeploymentStatus({ | ||
owner, | ||
repo, | ||
deployment_id: deployment.id, | ||
state: 'inactive', | ||
description: 'Marking deployment as inactive' | ||
}); | ||
await github.rest.repos.deleteDeployment({ | ||
owner, | ||
repo, | ||
deployment_id: deployment.id | ||
}); | ||
} | ||
|
||
octokit.rest.repos.deleteAnEnvironment({ | ||
owner, | ||
repo, | ||
environment_name: `preview-${context.issue.number}`, | ||
}); | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
name: preview | ||
|
||
on: | ||
workflow_call: ~ | ||
|
||
permissions: | ||
id-token: write | ||
pull-requests: write | ||
deployments: write | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Create Deployment | ||
uses: actions/github-script@v7 | ||
id: deployment | ||
with: | ||
result-encoding: string | ||
script: | | ||
const { owner, repo } = context.repo; | ||
const deployment = await github.rest.repos.createDeployment({ | ||
issue_number: context.issue.number, | ||
owner, | ||
repo, | ||
ref: context.payload.pull_request.head.ref, | ||
environment: `preview-${context.issue.number}`, | ||
bmorelli25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: `Preview deployment for PR ${context.issue.number}`, | ||
auto_merge: false, | ||
required_contexts: [], | ||
}) | ||
await github.rest.repos.createDeploymentStatus({ | ||
deployment_id: deployment.data.id, | ||
owner, | ||
repo, | ||
state: "in_progress", | ||
description: "Deployment created", | ||
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`, | ||
}) | ||
return deployment.data.id | ||
|
||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: docs-builder-binary | ||
|
||
# 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: | | ||
chmod +x ./docs-builder | ||
./docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" | ||
|
||
- uses: ./.github/actions/aws-auth | ||
Mpdreamz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- 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://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${context.issue.number}`, | ||
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.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", | ||
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`, | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary for this PR but we might want to protect this to only run if doc changes are part of the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention for this preview was not to create a preview of the docs only, but actually also a preview of changes within the docs-builder codebase.