Skip to content

Commit cf4e3df

Browse files
authored
Add path-pattern input to preview-build.yml workflow (#422)
* Add path-pattern input to preview-build.yml workflow * fix * Add read permissions for pull-requests * Only delete s3 objects if deployments are not empty * Only deploy if the workflow_run conclusion is successful * Add concurrency to deploy job * Refactor * Cleanup * Only compare changed files to last commit * This doesn't work as expected
1 parent 7ec2d91 commit cf4e3df

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

.github/workflows/preview-build.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@ on:
1313
type: string
1414
required: false
1515
default: 'true'
16+
path-pattern:
17+
description: 'Path pattern to filter files. Only if changed files match the pattern, the workflow will continue.'
18+
type: string
19+
default: '**'
20+
required: false
1621

1722
permissions:
1823
contents: read
24+
pull-requests: read
1925

2026
jobs:
2127
build:
2228
runs-on: ubuntu-latest
2329
steps:
30+
31+
- name: Get changed files
32+
id: check-files
33+
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
34+
with:
35+
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
36+
2437
- name: Checkout
38+
if: steps.check-files.outputs.any_changed == 'true'
2539
uses: actions/checkout@v4
2640
with:
2741
persist-credentials: false
@@ -30,11 +44,13 @@ jobs:
3044
env:
3145
PR_NUMBER: ${{ github.event.pull_request.number }}
3246
PR_REF: ${{ github.event.pull_request.head.sha }}
47+
ANY_CHANGED: ${{ steps.check-files.outputs.any_changed }}
3348
run: |
3449
cat << EOF > pull_request.json
3550
{
3651
"number": ${PR_NUMBER},
37-
"ref": "${PR_REF}"
52+
"ref": "${PR_REF}",
53+
"any_changed": ${ANY_CHANGED}
3854
}
3955
EOF
4056
@@ -48,26 +64,28 @@ jobs:
4864
compression-level: 1
4965

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

5470
# we run our artifact directly please use the prebuild
5571
# elastic/docs-builder@main GitHub Action for all other repositories!
5672
- name: Build documentation
57-
if: github.repository == 'elastic/docs-builder'
73+
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
5874
env:
5975
PR_NUMBER: ${{ github.event.pull_request.number }}
6076
run: |
6177
dotnet run --project src/docs-builder -- --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
6278
6379
- name: Build documentation
64-
if: github.repository != 'elastic/docs-builder'
80+
if: github.repository != 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
6581
uses: elastic/docs-builder@main
6682
continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
6783
with:
6884
prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
6985
strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
86+
7087
- uses: actions/upload-artifact@v4
88+
if: steps.check-files.outputs.any_changed == 'true'
7189
with:
7290
name: docs
7391
path: .artifacts/docs/html/

.github/workflows/preview-cleanup.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@ jobs:
1414
destroy:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: elastic/docs-builder/.github/actions/aws-auth@main
18-
- name: Delete s3 objects
19-
env:
20-
PR_NUMBER: ${{ github.event.pull_request.number }}
21-
run: |
22-
aws s3 rm "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --recursive
23-
2417
- name: Delete GitHub environment
2518
uses: actions/github-script@v7
19+
id: delete-deployment
2620
with:
2721
script: |
2822
const { owner, repo } = context.repo;
@@ -31,6 +25,7 @@ jobs:
3125
repo,
3226
environment: `docs-preview-${context.issue.number}`
3327
});
28+
core.setOutput('is-empty', deployments.data.length === 0)
3429
for (const deployment of deployments.data) {
3530
await github.rest.repos.createDeploymentStatus({
3631
owner,
@@ -45,3 +40,13 @@ jobs:
4540
deployment_id: deployment.id
4641
});
4742
}
43+
44+
- uses: elastic/docs-builder/.github/actions/aws-auth@main
45+
if: steps.delete-deployment.outputs.is-empty == 'false'
46+
47+
- name: Delete s3 objects
48+
if: steps.delete-deployment.outputs.is-empty == 'false'
49+
env:
50+
PR_NUMBER: ${{ github.event.pull_request.number }}
51+
run: |
52+
aws s3 rm "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --recursive

.github/workflows/preview-deploy.yml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ permissions:
1414
actions: read
1515

1616
jobs:
17-
docs-deploy:
17+
pull-request-data:
18+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1819
runs-on: ubuntu-latest
20+
outputs:
21+
number: ${{ steps.pull_request.outputs.number }}
22+
ref: ${{ steps.pull_request.outputs.ref }}
23+
any_changed: ${{ steps.pull_request.outputs.any_changed }}
1924
steps:
2025
- name: Download PR data
2126
env:
@@ -30,14 +35,23 @@ jobs:
3035
{
3136
echo "number=$(jq -r '.number' pull_request.json)"
3237
echo "ref=$(jq -r '.ref' pull_request.json)"
38+
echo "any_changed=$(jq -r '.any_changed' pull_request.json)"
3339
} >> "${GITHUB_OUTPUT}"
34-
40+
41+
deploy:
42+
needs: pull-request-data
43+
if: needs.pull-request-data.outputs.any_changed == 'true'
44+
runs-on: ubuntu-latest
45+
concurrency:
46+
group: ${{ github.workflow }}-${{ needs.pull-request-data.outputs.number }}
47+
cancel-in-progress: true
48+
steps:
3549
- name: Create Deployment
3650
uses: actions/github-script@v7
3751
id: deployment
3852
env:
39-
PR_NUMBER: ${{ steps.pull_request.outputs.number }}
40-
PR_REF: ${{ steps.pull_request.outputs.ref }}
53+
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
54+
PR_REF: ${{ needs.pull-request-data.outputs.ref }}
4155
with:
4256
result-encoding: string
4357
script: |
@@ -72,21 +86,23 @@ jobs:
7286

7387
- name: Upload to S3
7488
env:
75-
PR_NUMBER: ${{ steps.pull_request.outputs.number }}
89+
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
7690
run: |
7791
aws s3 sync ./html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
7892
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
7993
8094
- name: Update deployment status
8195
uses: actions/github-script@v7
8296
if: always() && steps.deployment.outputs.result
97+
env:
98+
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
8399
with:
84100
script: |
85101
await github.rest.repos.createDeploymentStatus({
86102
owner: context.repo.owner,
87103
repo: context.repo.repo,
88104
deployment_id: ${{ steps.deployment.outputs.result }},
89105
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
90-
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${{ steps.pull_request.outputs.number}}`,
106+
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${process.env.PR_NUMBER}`,
91107
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
92108
})

0 commit comments

Comments
 (0)