From 35a45d18c31a013739504ec4ff6fc4a813cb08bf Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 10:35:46 +0100 Subject: [PATCH 01/10] Add path-pattern input to preview-build.yml workflow --- .github/workflows/preview-build.yml | 25 +++++++++++++++++++++---- .github/workflows/preview-deploy.yml | 7 ++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index 3f2997e9f..ff7d2f8e7 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -13,6 +13,11 @@ 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 @@ -21,7 +26,15 @@ 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 @@ -30,11 +43,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 @@ -48,26 +63,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/ diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 899fa55d7..9a2204253 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -14,7 +14,7 @@ permissions: actions: read jobs: - docs-deploy: + deploy: runs-on: ubuntu-latest steps: - name: Download PR data @@ -30,9 +30,11 @@ 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}" - name: Create Deployment + if: steps.pull_request.outputs.any_changed == 'true' uses: actions/github-script@v7 id: deployment env: @@ -60,6 +62,7 @@ jobs: return deployment.data.id - name: Download docs + if: steps.pull_request.outputs.any_changed == 'true' env: GH_TOKEN: ${{ github.token }} run: | @@ -69,8 +72,10 @@ jobs: --dir html - uses: elastic/docs-builder/.github/actions/aws-auth@main + if: steps.pull_request.outputs.any_changed == 'true' - name: Upload to S3 + if: steps.pull_request.outputs.any_changed == 'true' env: PR_NUMBER: ${{ steps.pull_request.outputs.number }} run: | From b038a1fb545815497fb5ab32cf1e411d4b917abe Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 10:38:40 +0100 Subject: [PATCH 02/10] fix --- .github/workflows/preview-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index ff7d2f8e7..f2d583ccf 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -49,7 +49,7 @@ jobs: { "number": ${PR_NUMBER}, "ref": "${PR_REF}", - "any_changed": ${ANY_CHANGED}" + "any_changed": ${ANY_CHANGED} } EOF From 86bc7652152f9ee17d9d9ac5ed9fa0e11761ead6 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 10:46:56 +0100 Subject: [PATCH 03/10] Add read permissions for pull-requests --- .github/workflows/preview-build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index f2d583ccf..645f3c371 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -21,6 +21,7 @@ on: permissions: contents: read + pull-requests: read jobs: build: From 6e725ed543d546679cc3549291161ca145ba50b5 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 10:58:38 +0100 Subject: [PATCH 04/10] Only delete s3 objects if deployments are not empty --- .github/workflows/preview-cleanup.yml | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/preview-cleanup.yml b/.github/workflows/preview-cleanup.yml index d08c84a35..ccd157c11 100644 --- a/.github/workflows/preview-cleanup.yml +++ b/.github/workflows/preview-cleanup.yml @@ -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; @@ -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, @@ -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 From f8f88baee9af259971e91ac656e2f877fc02f9a2 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 11:15:12 +0100 Subject: [PATCH 05/10] Only deploy if the workflow_run conclusion is successful --- .github/workflows/preview-deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 9a2204253..6a0cc4553 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -15,6 +15,7 @@ permissions: jobs: deploy: + if: ${{ github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest steps: - name: Download PR data From ad6c73965716b574de4381b2b19b3f662912a561 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 13:41:48 +0100 Subject: [PATCH 06/10] Add concurrency to deploy job --- .github/workflows/preview-deploy.yml | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 6a0cc4553..c842919c0 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -14,9 +14,13 @@ permissions: actions: read jobs: - 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: @@ -33,14 +37,21 @@ jobs: 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 + runs-on: ubuntu-latest + concurrency: + group: ${{ github.workflow }}-${{ needs.pull-request-data.outputs.number }} + cancel-in-progress: true + steps: - name: Create Deployment - if: steps.pull_request.outputs.any_changed == 'true' + if: needs.pull-request-data.outputs.any_changed == 'true' 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: | @@ -63,7 +74,7 @@ jobs: return deployment.data.id - name: Download docs - if: steps.pull_request.outputs.any_changed == 'true' + if: needs.pull-request-data.outputs.any_changed == 'true' env: GH_TOKEN: ${{ github.token }} run: | @@ -73,12 +84,12 @@ jobs: --dir html - uses: elastic/docs-builder/.github/actions/aws-auth@main - if: steps.pull_request.outputs.any_changed == 'true' + if: needs.pull-request-data.outputs.any_changed == 'true' - name: Upload to S3 - if: steps.pull_request.outputs.any_changed == 'true' + if: needs.pull-request-data.outputs.any_changed == 'true' 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}/*" @@ -93,6 +104,6 @@ jobs: 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/${{ needs.pull-request-data.outputs.number}}`, log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, }) From bbd2fac04a2105f40a387107f0c186b22b10cf68 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 13:45:35 +0100 Subject: [PATCH 07/10] Refactor --- .github/workflows/preview-deploy.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index c842919c0..7089da484 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -40,13 +40,13 @@ jobs: 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 - if: needs.pull-request-data.outputs.any_changed == 'true' uses: actions/github-script@v7 id: deployment env: @@ -74,7 +74,6 @@ jobs: return deployment.data.id - name: Download docs - if: needs.pull-request-data.outputs.any_changed == 'true' env: GH_TOKEN: ${{ github.token }} run: | @@ -84,10 +83,8 @@ jobs: --dir html - uses: elastic/docs-builder/.github/actions/aws-auth@main - if: needs.pull-request-data.outputs.any_changed == 'true' - name: Upload to S3 - if: needs.pull-request-data.outputs.any_changed == 'true' env: PR_NUMBER: ${{ needs.pull-request-data.outputs.number }} run: | From 21db932f02094f4ac0eebf6985f027fac5badacb Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 13:51:28 +0100 Subject: [PATCH 08/10] Cleanup --- .github/workflows/preview-deploy.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 7089da484..fb129175c 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -94,6 +94,8 @@ jobs: - 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({ @@ -101,6 +103,6 @@ jobs: 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/${{ needs.pull-request-data.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}`, }) From bd8babd8bfc0639a7f207f5cba03209788bafe4a Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 13:54:24 +0100 Subject: [PATCH 09/10] Only compare changed files to last commit --- .github/workflows/preview-build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index 645f3c371..7de99b5cc 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -32,6 +32,7 @@ jobs: id: check-files uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6 with: + since_last_remote_commit: true files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }} - name: Checkout From bebe6a22bcb669e4a866d2092c85734346c496f5 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Wed, 5 Feb 2025 14:04:52 +0100 Subject: [PATCH 10/10] This doesn't work as expected --- .github/workflows/preview-build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index 7de99b5cc..645f3c371 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -32,7 +32,6 @@ jobs: id: check-files uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6 with: - since_last_remote_commit: true files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }} - name: Checkout