diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index 3f2997e9f..645f3c371 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -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 @@ -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 @@ -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/ 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 diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 899fa55d7..fb129175c 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -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: @@ -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: | @@ -72,7 +86,7 @@ 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}/*" @@ -80,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({ @@ -87,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/${{ 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}`, })