report-size.yml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. name: Report size
  2. on:
  3. workflow_run:
  4. workflows: ["Read size"]
  5. types:
  6. - completed
  7. # This workflow needs to be run with "pull-requests: write" permissions to
  8. # be able to comment on the pull request. We can't checkout the PR code
  9. # in this workflow.
  10. # Reference:
  11. # https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
  12. permissions:
  13. pull-requests: write
  14. jobs:
  15. report-size:
  16. name: Comment on PR
  17. runs-on: ubuntu-latest
  18. if: github.event.workflow_run.event == 'pull_request' &&
  19. github.event.workflow_run.conclusion == 'success'
  20. steps:
  21. - name: Log GitHub context
  22. env:
  23. GITHUB_CONTEXT: ${{ toJson(github) }}
  24. run: echo "$GITHUB_CONTEXT"
  25. # Using actions/download-artifact doesn't work here
  26. # https://github.com/actions/download-artifact/issues/60
  27. - name: Download artifact
  28. uses: actions/github-script@v6
  29. id: download-artifact
  30. with:
  31. result-encoding: string
  32. script: |
  33. const fs = require('fs/promises');
  34. const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
  35. owner: context.repo.owner,
  36. repo: context.repo.repo,
  37. run_id: context.payload.workflow_run.id,
  38. });
  39. const matchArtifact = artifacts.data.artifacts.find((artifact) => artifact.name === 'sizes');
  40. const download = await github.rest.actions.downloadArtifact({
  41. owner: context.repo.owner,
  42. repo: context.repo.repo,
  43. artifact_id: matchArtifact.id,
  44. archive_format: 'zip',
  45. });
  46. await fs.writeFile('sizes.zip', Buffer.from(download.data));
  47. await exec.exec('unzip sizes.zip');
  48. const json = await fs.readFile('sizes.json', 'utf8');
  49. return json;
  50. # This runs on the base branch of the PR, meaning "dev"
  51. - name: Git checkout
  52. uses: actions/checkout@v3
  53. - name: Install Node
  54. uses: actions/setup-node@v3
  55. with:
  56. node-version: 18
  57. cache: 'npm'
  58. - name: Install dependencies
  59. run: npm ci
  60. - name: Build
  61. run: npm run build-module
  62. - name: === Test tree-shaking ===
  63. run: npm run test-treeshake
  64. - name: Read sizes
  65. id: read-size
  66. run: |
  67. FILESIZE_BASE=$(stat --format=%s test/treeshake/three.module.min.js)
  68. TREESHAKEN_BASE=$(stat --format=%s test/treeshake/index.bundle.min.js)
  69. echo "FILESIZE_BASE=$FILESIZE_BASE" >> $GITHUB_OUTPUT
  70. echo "TREESHAKEN_BASE=$TREESHAKEN_BASE" >> $GITHUB_OUTPUT
  71. - name: Format sizes
  72. id: format
  73. # It's important these are passed as env variables.
  74. # https://securitylab.github.com/research/github-actions-untrusted-input/
  75. env:
  76. FILESIZE: ${{ fromJSON(steps.download-artifact.outputs.result).filesize }}
  77. FILESIZE_GZIP: ${{ fromJSON(steps.download-artifact.outputs.result).gzip }}
  78. FILESIZE_BASE: ${{ steps.read-size.outputs.FILESIZE_BASE }}
  79. TREESHAKEN: ${{ fromJSON(steps.download-artifact.outputs.result).treeshaken }}
  80. TREESHAKEN_GZIP: ${{ fromJSON(steps.download-artifact.outputs.result).treeshakenGzip }}
  81. TREESHAKEN_BASE: ${{ steps.read-size.outputs.TREESHAKEN_BASE }}
  82. run: |
  83. FILESIZE_FORM=$(node ./test/treeshake/utils/format-size.js "$FILESIZE")
  84. FILESIZE_GZIP_FORM=$(node ./test/treeshake/utils/format-size.js "$FILESIZE_GZIP")
  85. FILESIZE_DIFF=$(node ./test/treeshake/utils/format-diff.js "$FILESIZE" "$FILESIZE_BASE")
  86. TREESHAKEN_FORM=$(node ./test/treeshake/utils/format-size.js "$TREESHAKEN")
  87. TREESHAKEN_GZIP_FORM=$(node ./test/treeshake/utils/format-size.js "$TREESHAKEN_GZIP")
  88. TREESHAKEN_DIFF=$(node ./test/treeshake/utils/format-diff.js "$TREESHAKEN" "$TREESHAKEN_BASE")
  89. echo "FILESIZE=$FILESIZE_FORM" >> $GITHUB_OUTPUT
  90. echo "FILESIZE_GZIP=$FILESIZE_GZIP_FORM" >> $GITHUB_OUTPUT
  91. echo "FILESIZE_DIFF=$FILESIZE_DIFF" >> $GITHUB_OUTPUT
  92. echo "TREESHAKEN=$TREESHAKEN_FORM" >> $GITHUB_OUTPUT
  93. echo "TREESHAKEN_GZIP=$TREESHAKEN_GZIP_FORM" >> $GITHUB_OUTPUT
  94. echo "TREESHAKEN_DIFF=$TREESHAKEN_DIFF" >> $GITHUB_OUTPUT
  95. - name: Find existing comment
  96. uses: peter-evans/find-comment@v2
  97. id: find-comment
  98. with:
  99. issue-number: ${{ fromJSON(steps.download-artifact.outputs.result).pr }}
  100. comment-author: 'github-actions[bot]'
  101. body-includes: Bundle size
  102. - name: Comment on PR
  103. uses: peter-evans/create-or-update-comment@v2
  104. with:
  105. issue-number: ${{ fromJSON(steps.download-artifact.outputs.result).pr }}
  106. comment-id: ${{ steps.find-comment.outputs.comment-id }}
  107. edit-mode: replace
  108. body: |
  109. ### 📦 Bundle size
  110. | Filesize | Gzipped | Diff from `${{ github.ref_name }}` |
  111. |----------|---------|------|
  112. | ${{ steps.format.outputs.FILESIZE }} | ${{ steps.format.outputs.FILESIZE_GZIP }} | ${{ steps.format.outputs.FILESIZE_DIFF }} |
  113. ### 🌳 Bundle size after tree-shaking
  114. _Includes a renderer, camera, and empty scene._
  115. | Filesize | Gzipped | Diff from `${{ github.ref_name }}` |
  116. |----------|---------|------|
  117. | ${{ steps.format.outputs.TREESHAKEN }} | ${{ steps.format.outputs.TREESHAKEN_GZIP }} | ${{ steps.format.outputs.TREESHAKEN_DIFF }} |
粤ICP备19079148号