|
| 1 | +name: Docs deploy |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ["Docs build"] |
| 5 | + types: |
| 6 | + - completed |
| 7 | + |
| 8 | +jobs: |
| 9 | + checks: |
| 10 | + name: Docs Deploy Checks |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + parameters: ${{ steps.parameters.outputs.result }} |
| 14 | + artifact: ${{ steps.get-artifact.outputs.result }} |
| 15 | + steps: |
| 16 | + - if: ${{ github.event.workflow_run.conclusion != 'success' }} |
| 17 | + run: echo 'The triggering workflow did not succeed' && exit 1 |
| 18 | + - name: Get artifact |
| 19 | + id: get-artifact |
| 20 | + uses: actions/github-script@v7 |
| 21 | + with: |
| 22 | + script: | |
| 23 | + // List all artifacts for the workflow run |
| 24 | + const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + run_id: context.payload.workflow_run.id, |
| 28 | + }); |
| 29 | +
|
| 30 | + // Find the artifact named "docs-build-output" |
| 31 | + const matchArtifact = artifacts.find(artifact => artifact.name === "docs-build-output"); |
| 32 | +
|
| 33 | + if (!matchArtifact) { |
| 34 | + console.log("No artifact found with the name docs-build-output, build job was skipped"); |
| 35 | + return { found: false }; |
| 36 | + } |
| 37 | +
|
| 38 | + return { found: true, id: matchArtifact.id }; |
| 39 | +
|
| 40 | + - name: Determine deploy parameters |
| 41 | + id: parameters |
| 42 | + uses: actions/github-script@v7 |
| 43 | + with: |
| 44 | + script: | |
| 45 | + // Extract event type and fork status |
| 46 | + const eventType = context.payload.workflow_run.event; |
| 47 | + const isFork = context.payload.workflow_run.repository.fork; |
| 48 | +
|
| 49 | + let parameters; |
| 50 | + console.log({eventType, isFork}); |
| 51 | +
|
| 52 | + // Handle push events |
| 53 | + if (eventType == "push") { |
| 54 | + const branch = context.payload.workflow_run.head_branch; |
| 55 | + console.log({branch}); |
| 56 | + const shouldDeploy = !isFork && branch == "master"; |
| 57 | + parameters = { |
| 58 | + event: "branch", |
| 59 | + name: "master", |
| 60 | + shouldDeploy |
| 61 | + }; |
| 62 | + // Handle pull request events |
| 63 | + } else if (eventType == "pull_request") { |
| 64 | + let pull_number = context.payload.workflow_run.pull_requests[0]?.number; |
| 65 | + if (!pull_number) { |
| 66 | + const response = await github.rest.search.issuesAndPullRequests({ |
| 67 | + q: `repo:${context.repo.owner}/${context.repo.repo} is:pr sha:${context.payload.workflow_run.head_sha}`, |
| 68 | + per_page: 1, |
| 69 | + }); |
| 70 | + const items = response.data.items; |
| 71 | + if (items.length < 1) { |
| 72 | + throw new Error("No pull request found for the commit"); |
| 73 | + } |
| 74 | + pull_number = items[0].number; |
| 75 | + } |
| 76 | + const { data: pr } = await github.rest.pulls.get({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + pull_number |
| 80 | + }); |
| 81 | + console.log({ pull_number }); |
| 82 | + parameters = { |
| 83 | + event: "pr", |
| 84 | + name: `pr-${pull_number}`, |
| 85 | + pr_number: pull_number, |
| 86 | + shouldDeploy: true |
| 87 | + }; |
| 88 | + // Handle release events |
| 89 | + } else if (eventType == "release") { |
| 90 | + parameters = { |
| 91 | + event: "release", |
| 92 | + name: context.payload.workflow_run.head_branch, |
| 93 | + shouldDeploy: !isFork |
| 94 | + }; |
| 95 | + } |
| 96 | +
|
| 97 | + console.log(parameters); |
| 98 | + return parameters; |
| 99 | +
|
| 100 | + deploy: |
| 101 | + name: Docs Deploy |
| 102 | + runs-on: ubuntu-latest |
| 103 | + needs: checks |
| 104 | + if: ${{ fromJson(needs.checks.outputs.artifact).found && fromJson(needs.checks.outputs.parameters).shouldDeploy }} |
| 105 | + steps: |
| 106 | + - name: Checkout code |
| 107 | + uses: actions/checkout@v4 |
| 108 | + |
| 109 | + - name: Load parameters |
| 110 | + id: parameters |
| 111 | + uses: actions/github-script@v7 |
| 112 | + with: |
| 113 | + script: | |
| 114 | + const json = `${{ needs.checks.outputs.parameters }}`; |
| 115 | + const parameters = JSON.parse(json); |
| 116 | + core.setOutput("event", parameters.event); |
| 117 | + core.setOutput("name", parameters.name); |
| 118 | + core.setOutput("shouldDeploy", parameters.shouldDeploy); |
| 119 | +
|
| 120 | + - run: | |
| 121 | + echo "Starting docs deployment for ${{ steps.parameters.outputs.event }} ${{ steps.parameters.outputs.name }}" |
| 122 | +
|
| 123 | + - name: Download artifact |
| 124 | + uses: actions/github-script@v7 |
| 125 | + with: |
| 126 | + script: | |
| 127 | + let artifact = ${{ needs.checks.outputs.artifact }}; |
| 128 | + let download = await github.rest.actions.downloadArtifact({ |
| 129 | + owner: context.repo.owner, |
| 130 | + repo: context.repo.repo, |
| 131 | + artifact_id: artifact.id, |
| 132 | + archive_format: 'zip', |
| 133 | + }); |
| 134 | + let fs = require('fs'); |
| 135 | + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/docs-build-output.zip`, Buffer.from(download.data)); |
| 136 | +
|
| 137 | + - name: Unzip artifact |
| 138 | + run: unzip "${{ github.workspace }}/docs-build-output.zip" -d "${{ github.workspace }}/docs/build" |
| 139 | + |
| 140 | + - name: Publish to Cloudflare Pages |
| 141 | + uses: cloudflare/pages-action@v1 |
| 142 | + with: |
| 143 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 144 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 145 | + projectName: ${{ fromJson(steps.clean.outputs.output).pages_project_name.value }} |
| 146 | + workingDirectory: "docs" |
| 147 | + directory: "build" |
| 148 | + branch: ${{ steps.parameters.outputs.name }} |
| 149 | + wranglerVersion: "3" |
0 commit comments