From 4b7e1c48d77fb209847ef00cf136fee39c7b7125 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 4 Feb 2025 11:21:38 +0100 Subject: [PATCH] Optimize preview workflows (#407) --- .github/workflows/preview-build.yml | 47 ++++++++++++++++++++-------- .github/workflows/preview-deploy.yml | 22 ++++++++++--- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index 538a49f4..3f2997e9 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -6,13 +6,13 @@ on: inputs: strict: description: 'Treat warnings as errors' - type: boolean - default: true + type: string + default: 'true' continue-on-error: description: 'Do not fail to publish if build fails' - type: boolean + type: string required: false - default: true + default: 'true' permissions: contents: read @@ -23,6 +23,29 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Store PR data + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + PR_REF: ${{ github.event.pull_request.head.sha }} + run: | + cat << EOF > pull_request.json + { + "number": ${PR_NUMBER}, + "ref": "${PR_REF}" + } + EOF + + - name: Upload PR data + uses: actions/upload-artifact@v4 + with: + name: pull-request-data + path: pull_request.json + if-no-files-found: error + retention-days: 1 + compression-level: 1 - name: Bootstrap Action Workspace if: github.repository == 'elastic/docs-builder' @@ -40,18 +63,16 @@ jobs: - name: Build documentation if: github.repository != 'elastic/docs-builder' uses: elastic/docs-builder@main - continue-on-error: true + continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }} with: prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}" - strict: true - - name: Add pull request number to build - env: - PR_NUMBER: ${{ github.event.pull_request.number }} - PR_REF: ${{ github.event.pull_request.head.sha }} - run: | - echo "${PR_NUMBER}" >> .artifacts/docs/html/pull_request_number.txt - echo "${PR_REF}" >> .artifacts/docs/html/pull_request_ref.txt + strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }} - uses: actions/upload-artifact@v4 with: name: docs path: .artifacts/docs/html/ + if-no-files-found: error + retention-days: 1 + # The lower the compression-level, the faster the artifact will be uploaded. + # But the size of the artifact will be larger. + compression-level: 1 diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 78c7e99f..dc4e969a 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -17,16 +17,20 @@ jobs: docs-deploy: runs-on: ubuntu-latest steps: - - name: Download docs + - name: Download PR data env: GH_TOKEN: ${{ github.token }} run: | - gh run download ${{ github.event.workflow_run.id }} --name docs --repo "${GITHUB_REPOSITORY}" + gh run download ${{ github.event.workflow_run.id }} \ + --repo "${GITHUB_REPOSITORY}" \ + --name pull-request-data - name: Get PR data id: pull_request run: | - echo "number=$(cat pull_request_number.txt)" >> "${GITHUB_OUTPUT}" - echo "ref=$(cat pull_request_ref.txt)" >> "${GITHUB_OUTPUT}" + { + echo "number=$(jq -r '.number' pull_request.json)" + echo "ref=$(jq -r '.ref' pull_request.json)" + } >> "${GITHUB_OUTPUT}" - name: Create Deployment uses: actions/github-script@v7 @@ -54,6 +58,14 @@ jobs: log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, }) return deployment.data.id + + - name: Download docs + env: + GH_TOKEN: ${{ github.token }} + run: | + gh run download ${{ github.event.workflow_run.id }} \ + --repo "${GITHUB_REPOSITORY}" \ + --name docs - uses: elastic/docs-builder/.github/actions/aws-auth@main @@ -61,7 +73,7 @@ jobs: env: PR_NUMBER: ${{ steps.pull_request.outputs.number }} run: | - aws s3 sync . "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete --exclude "pull_request_number.txt" + aws s3 sync . "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete --exclude "pull_request.json" aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*" - name: Update deployment status