main.yml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. name: Build
  2. on:
  3. push:
  4. branches:
  5. - master
  6. pull_request:
  7. schedule:
  8. - cron: "0 12 * * *"
  9. concurrency:
  10. group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  11. cancel-in-progress: true
  12. jobs:
  13. build:
  14. name: Build
  15. runs-on: ubuntu-24.04-arm
  16. steps:
  17. # - name: Tune GitHub-hosted runner network
  18. # # https://github.com/actions/runner-images/issues/1187
  19. # uses: smorimoto/tune-github-hosted-runner-network@v1
  20. - uses: actions/checkout@v4
  21. with:
  22. persist-credentials: false
  23. - uses: pnpm/action-setup@v4
  24. with:
  25. run_install: false
  26. - uses: actions/setup-node@v4
  27. with:
  28. node-version-file: ".node-version"
  29. cache: "pnpm"
  30. - name: Create RAM Disk for building
  31. id: ramdisk
  32. run: |
  33. BUILD_DIR=$(mktemp -d -p /dev/shm/ -t sukka-surge-public.XXXXXXXXXX)
  34. echo "Build dir created at $BUILD_DIR"
  35. echo "build_dir=$BUILD_DIR" >> $GITHUB_OUTPUT
  36. - name: Download Previous Build
  37. uses: actions/checkout@v4
  38. with:
  39. repository: SukkaLab/ruleset.skk.moe
  40. persist-credentials: false
  41. path: previous-build-${{ github.run_id }}-${{ github.run_number }}
  42. - name: Setup build folder
  43. run: |
  44. mv previous-build-${{ github.run_id }}-${{ github.run_number }}/{.,}* ${{ steps.ramdisk.outputs.build_dir }}/
  45. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/.git ]; then
  46. echo ".git not found"
  47. exit 1
  48. fi
  49. rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git"
  50. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/List ]; then
  51. echo "List not found"
  52. exit 1
  53. fi
  54. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  55. - name: Get current date
  56. id: date
  57. run: |
  58. echo "date=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
  59. echo "year=$(date +'%Y')" >> $GITHUB_OUTPUT
  60. echo "month=$(date +'%m')" >> $GITHUB_OUTPUT
  61. echo "day=$(date +'%d')" >> $GITHUB_OUTPUT
  62. echo "hour=$(date +'%H')" >> $GITHUB_OUTPUT
  63. echo "minute=$(date +'%M')" >> $GITHUB_OUTPUT
  64. echo "second=$(date +'%S')" >> $GITHUB_OUTPUT
  65. - name: Restore cache.db
  66. uses: actions/cache/restore@v4
  67. id: cache-db-restore
  68. with:
  69. path: |
  70. .cache
  71. key: ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:${{ steps.date.outputs.second }}
  72. # If source files changed but packages didn't, rebuild from a prior cache.
  73. restore-keys: |
  74. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:
  75. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:
  76. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }}
  77. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-
  78. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-
  79. ${{ runner.os }}-v3-
  80. - run: pnpm install
  81. - run: pnpm run build
  82. env:
  83. PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }}
  84. - name: Pre-deploy check
  85. # If the public directory doesn't exist, the build should fail.
  86. # If the public directory is empty, the build should fail.
  87. run: |
  88. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then
  89. echo "public directory not found"
  90. exit 1
  91. fi
  92. if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then
  93. echo "public directory is empty"
  94. exit 1
  95. fi
  96. if [ ! -f .BUILD_FINISHED ]; then
  97. echo ".BUILD_FINISHED not found"
  98. exit 1
  99. fi
  100. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  101. - uses: actions/upload-artifact@v4
  102. with:
  103. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  104. path: ${{ steps.ramdisk.outputs.build_dir }}
  105. if-no-files-found: error
  106. retention-days: 1
  107. compression-level: 4
  108. include-hidden-files: false
  109. - name: Cache cache.db
  110. if: always()
  111. uses: actions/cache/save@v4
  112. with:
  113. path: |
  114. .cache
  115. key: ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:${{ steps.date.outputs.second }}
  116. diff_deployment_on_pr:
  117. if: github.ref != 'refs/heads/master'
  118. needs:
  119. - build
  120. name: Diff output
  121. runs-on: ubuntu-24.04-arm
  122. steps:
  123. - uses: actions/download-artifact@v4
  124. with:
  125. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  126. path: public
  127. - name: Diff
  128. run: |
  129. git clone --filter=tree:0 --no-tags https://github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git >/dev/null 2>&1
  130. cd ./deploy-git
  131. git fetch origin master >/dev/null 2>&1
  132. rm -rf ./*
  133. cp -rf ../public/* ./
  134. git --no-pager diff --minimal
  135. deploy_to_cloudflare_pages:
  136. needs:
  137. - build
  138. name: Deploy to Cloudflare Pages
  139. if: github.ref == 'refs/heads/master'
  140. runs-on: ubuntu-24.04-arm
  141. steps:
  142. - name: Get NPM cache directory path
  143. id: npm_cache_path
  144. shell: sh
  145. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  146. - name: Cache NPM cache
  147. uses: actions/cache@v4
  148. with:
  149. path: |
  150. ${{ steps.npm_cache_path.outputs.dir }}
  151. node_modules
  152. key: ${{ runner.os }}-${{ runner.arch }}-deploy-to-cloudflare-npm
  153. - uses: actions/download-artifact@v4
  154. with:
  155. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  156. path: public
  157. - name: Deploy to Cloudflare Pages
  158. uses: cloudflare/wrangler-action@v3
  159. with:
  160. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  161. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  162. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  163. wranglerVersion: '3.90.0'
  164. deploy_to_github_gitlab:
  165. needs:
  166. - build
  167. name: Deploy to GitHub and GitLab
  168. if: github.ref == 'refs/heads/master'
  169. runs-on: ubuntu-24.04-arm
  170. steps:
  171. - uses: actions/download-artifact@v4
  172. with:
  173. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  174. path: public
  175. - name: Upload Dist to GitLab
  176. continue-on-error: true
  177. run: |
  178. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  179. cd ./deploy-git
  180. git config --global push.dgefault matching
  181. git config --global user.email "${GITLAB_EMAIL}"
  182. git config --global user.name "${GITLAB_USER}"
  183. rm -rf ./*
  184. cp -rf ../public/* ./
  185. git add --all .
  186. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  187. git push --quiet --force origin HEAD:master
  188. cd ..
  189. rm -rf ./deploy-git
  190. env:
  191. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  192. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  193. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  194. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  195. - name: Upload Dist to GitHub
  196. continue-on-error: true
  197. run: |
  198. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git
  199. cd ./deploy-git
  200. git config --global push.default matching
  201. git config --global user.email "${GH_EMAIL}"
  202. git config --global user.name "${GH_USER}"
  203. rm -rf ./*
  204. cp -rf ../public/* ./
  205. echo "ruleset.skk.moe" > CNAME
  206. git add --all .
  207. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  208. git push --quiet --force origin HEAD:master
  209. cd ..
  210. rm -rf ./deploy-git
  211. env:
  212. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  213. GH_USER: ${{ secrets.GIT_USER }}
  214. GH_TOKEN: ${{ secrets.GIT_TOKEN }}