Skip to content
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 path-pattern input to preview-build.yml workflow #422

Merged
merged 10 commits into from
Feb 5, 2025
26 changes: 22 additions & 4 deletions .github/workflows/preview-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ on:
type: string
required: false
default: 'true'
path-pattern:
description: 'Path pattern to filter files. Only if changed files match the pattern, the workflow will continue.'
type: string
default: '**'
required: false

permissions:
contents: read
pull-requests: read

jobs:
build:
runs-on: ubuntu-latest
steps:

- name: Get changed files
id: check-files
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
with:
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}

- name: Checkout
if: steps.check-files.outputs.any_changed == 'true'
uses: actions/checkout@v4
with:
persist-credentials: false
Expand All @@ -30,11 +44,13 @@ jobs:
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_REF: ${{ github.event.pull_request.head.sha }}
ANY_CHANGED: ${{ steps.check-files.outputs.any_changed }}
run: |
cat << EOF > pull_request.json
{
"number": ${PR_NUMBER},
"ref": "${PR_REF}"
"ref": "${PR_REF}",
"any_changed": ${ANY_CHANGED}
}
EOF

Expand All @@ -48,26 +64,28 @@ jobs:
compression-level: 1

- name: Bootstrap Action Workspace
if: github.repository == 'elastic/docs-builder'
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
uses: ./.github/actions/bootstrap

# we run our artifact directly please use the prebuild
# elastic/docs-builder@main GitHub Action for all other repositories!
- name: Build documentation
if: github.repository == 'elastic/docs-builder'
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
dotnet run --project src/docs-builder -- --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"

- name: Build documentation
if: github.repository != 'elastic/docs-builder'
if: github.repository != 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
uses: elastic/docs-builder@main
continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
with:
prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}

- uses: actions/upload-artifact@v4
if: steps.check-files.outputs.any_changed == 'true'
with:
name: docs
path: .artifacts/docs/html/
Expand Down
19 changes: 12 additions & 7 deletions .github/workflows/preview-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ jobs:
destroy:
runs-on: ubuntu-latest
steps:
- uses: elastic/docs-builder/.github/actions/aws-auth@main
- 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
id: delete-deployment
with:
script: |
const { owner, repo } = context.repo;
Expand All @@ -31,6 +25,7 @@ jobs:
repo,
environment: `docs-preview-${context.issue.number}`
});
core.setOutput('is-empty', deployments.data.length === 0)
for (const deployment of deployments.data) {
await github.rest.repos.createDeploymentStatus({
owner,
Expand All @@ -45,3 +40,13 @@ jobs:
deployment_id: deployment.id
});
}

- uses: elastic/docs-builder/.github/actions/aws-auth@main
if: steps.delete-deployment.outputs.is-empty == 'false'

- name: Delete s3 objects
if: steps.delete-deployment.outputs.is-empty == 'false'
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
aws s3 rm "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --recursive
28 changes: 22 additions & 6 deletions .github/workflows/preview-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ permissions:
actions: read

jobs:
docs-deploy:
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:
Expand All @@ -30,14 +35,23 @@ jobs:
{
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: ${{ steps.pull_request.outputs.number }}
PR_REF: ${{ steps.pull_request.outputs.ref }}
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
PR_REF: ${{ needs.pull-request-data.outputs.ref }}
with:
result-encoding: string
script: |
Expand Down Expand Up @@ -72,21 +86,23 @@ jobs:

- name: Upload to S3
env:
PR_NUMBER: ${{ steps.pull_request.outputs.number }}
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/${{ steps.pull_request.outputs.number}}`,
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}`,
})