|
| 1 | +name: Publish PR |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ['Render PR'] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +jobs: |
| 9 | + publish: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + if: > |
| 12 | + ${{ |
| 13 | + !github.event.repository.fork && |
| 14 | + github.event.workflow_run.event == 'pull_request' && |
| 15 | + github.event.workflow_run.conclusion == 'success' |
| 16 | + }} |
| 17 | + steps: |
| 18 | + - name: print event info |
| 19 | + uses: actions/github-script@v7 |
| 20 | + with: |
| 21 | + script: 'console.log(${{ toJson(github.event) }});' |
| 22 | + - name: download zipball |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + const { owner, repo } = context.repo; |
| 27 | + const { total_count, artifacts } = await github.actions.listWorkflowRunArtifacts({ |
| 28 | + owner, |
| 29 | + repo, |
| 30 | + run_id: ${{ github.event.workflow_run.id }}, |
| 31 | + name: 'result', |
| 32 | + }); |
| 33 | + if (total_count !== 1) { |
| 34 | + const summary = artifacts?.map(artifact => { |
| 35 | + const { name, size_in_bytes, url } = artifact; |
| 36 | + return { name, size_in_bytes, url }; |
| 37 | + }); |
| 38 | + const repr = value => JSON.stringify(value); |
| 39 | + throw Error(`Expected 1 artifact, got ${total_count} ${repr(summary ?? [])}`); |
| 40 | + } |
| 41 | + const artifactData = { |
| 42 | + owner, |
| 43 | + repo, |
| 44 | + artifact_id: artifacts[0].id, |
| 45 | + archive_format: 'zip', |
| 46 | + }; |
| 47 | + const download = await github.actions.downloadArtifact(artifactData); |
| 48 | + const fs = require('fs'); |
| 49 | + fs.writeFileSync('${{ github.workspace }}/result.zip', Buffer.from(download.data)); |
| 50 | + - name: provide result directory |
| 51 | + run: rmdir -rf result && mkdir -p result |
| 52 | + - run: unzip -o result.zip -d result |
| 53 | + - run: ls result |
| 54 | + - name: extract PR number |
| 55 | + id: extract-pr-number |
| 56 | + run: | |
| 57 | + cd result |
| 58 | + awk -vok=1 \ |
| 59 | + '{ print; if(!match($0, /^[1-9][0-9]*$/)) ok=0; } END { exit !(NR==1 && ok); }' \ |
| 60 | + pr-number.txt |
| 61 | + echo "number=$(cat pr-number.txt)" >> $GITHUB_OUTPUT |
| 62 | + rm pr-number.txt |
| 63 | + - name: publish to gh-pages |
| 64 | + |
| 65 | + with: |
| 66 | + branch: gh-pages |
| 67 | + folder: result |
| 68 | + target-folder: pr/${{ steps.extract-pr-number.outputs.number }} |
| 69 | + - name: determine gh-pages url |
| 70 | + id: get-pages-url |
| 71 | + run: | |
| 72 | + gh_pages_url="https://$(printf '%s' "$GITHUB_REPOSITORY" \ |
| 73 | + | sed 's#/#.github.io/#; s#^tc39.github.io/#tc39.es/#')" |
| 74 | + echo "url=$gh_pages_url" >> $GITHUB_OUTPUT |
| 75 | + - name: provide comment |
| 76 | + uses: phulsechinmay/[email protected] |
| 77 | + with: |
| 78 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + message: | |
| 80 | + The rendered spec for this PR is available at ${{ steps.get-pages-url.outputs.url }}. |
0 commit comments