main.yml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. name: Build
  2. on:
  3. push:
  4. branches:
  5. - master
  6. pull_request:
  7. schedule:
  8. - cron: '0 5 * * *' # Runs at 05:00 UTC
  9. - cron: '0 17 * * *' # Runs at 1:00 UTC
  10. concurrency:
  11. group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  12. cancel-in-progress: true
  13. jobs:
  14. build:
  15. name: Build
  16. runs-on: ubuntu-24.04-arm
  17. steps:
  18. - uses: smorimoto/tune-github-hosted-runner-network@v1
  19. # https://github.com/actions/runner-images/issues/1187
  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: Grab Building Folder
  31. id: ramdisk
  32. run: |
  33. echo "build_dir=previous-build-${{ github.run_id }}-${{ github.run_number }}" >> $GITHUB_OUTPUT
  34. - name: Download Previous Build
  35. uses: actions/checkout@v4
  36. with:
  37. repository: SukkaLab/ruleset.skk.moe
  38. persist-credentials: false
  39. filter: "tree:0" # we don't care about git history here
  40. fetch-tags: false
  41. path: ${{ steps.ramdisk.outputs.build_dir }}
  42. token: ${{ secrets.GIT_TOKEN }}
  43. - name: Setup build folder
  44. run: |
  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
  141. # matrix is a tricky way to define a variable directly in the action yaml
  142. strategy:
  143. matrix:
  144. wranglerVersion: ['3.114.12']
  145. steps:
  146. - name: Get NPM cache directory path
  147. id: npm_cache_path
  148. shell: sh
  149. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  150. - uses: actions/cache@v4
  151. with:
  152. path: |
  153. ${{ steps.npm_cache_path.outputs.dir }}
  154. node_modules
  155. key: deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-${{ matrix.wranglerVersion }}
  156. restore-keys: |
  157. deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-
  158. - uses: actions/download-artifact@v4
  159. with:
  160. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  161. path: public
  162. - uses: cloudflare/wrangler-action@v3
  163. with:
  164. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  165. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  166. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  167. wranglerVersion: ${{ matrix.wranglerVersion }}
  168. deploy_to_github_gitlab:
  169. needs:
  170. - build
  171. name: Deploy to GitHub and GitLab
  172. if: github.ref == 'refs/heads/master'
  173. runs-on: ubuntu-24.04
  174. steps:
  175. - uses: actions/download-artifact@v4
  176. with:
  177. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  178. path: public
  179. - name: Upload Dist to GitLab
  180. continue-on-error: true
  181. run: |
  182. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  183. cd ./deploy-git
  184. git config --global push.dgefault matching
  185. git config --global user.email "${GITLAB_EMAIL}"
  186. git config --global user.name "${GITLAB_USER}"
  187. rm -rf ./*
  188. cp -rf ../public/* ./
  189. git add --all .
  190. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  191. git push --quiet --force origin HEAD:master
  192. cd ..
  193. rm -rf ./deploy-git
  194. env:
  195. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  196. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  197. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  198. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  199. - name: Upload Dist to GitHub
  200. continue-on-error: true
  201. run: |
  202. gh repo unarchive SukkaLab/ruleset.skk.moe --yes
  203. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git
  204. cd ./deploy-git
  205. git config --global push.default matching
  206. git config --global user.email "${GH_EMAIL}"
  207. git config --global user.name "${GH_USER}"
  208. rm -rf ./*
  209. cp -rf ../public/* ./
  210. echo "ruleset.skk.moe" > CNAME
  211. git add --all .
  212. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  213. git push --quiet --force origin HEAD:master
  214. cd ..
  215. rm -rf ./deploy-git
  216. gh repo archive SukkaLab/ruleset.skk.moe --yes
  217. env:
  218. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  219. GH_USER: ${{ secrets.GIT_USER }}
  220. GH_TOKEN: ${{ secrets.GIT_TOKEN }}