main.yml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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@v5
  21. with:
  22. persist-credentials: false
  23. - uses: pnpm/action-setup@v4
  24. with:
  25. run_install: false
  26. - uses: actions/setup-node@v5
  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@v5
  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. - name: Setup build folder
  43. run: |
  44. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/.git ]; then
  45. echo ".git not found"
  46. exit 1
  47. fi
  48. rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git"
  49. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/List ]; then
  50. echo "List not found"
  51. exit 1
  52. fi
  53. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  54. - name: Get current date
  55. id: date
  56. run: |
  57. echo "date=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
  58. echo "year=$(date +'%Y')" >> $GITHUB_OUTPUT
  59. echo "month=$(date +'%m')" >> $GITHUB_OUTPUT
  60. echo "day=$(date +'%d')" >> $GITHUB_OUTPUT
  61. echo "hour=$(date +'%H')" >> $GITHUB_OUTPUT
  62. echo "minute=$(date +'%M')" >> $GITHUB_OUTPUT
  63. echo "second=$(date +'%S')" >> $GITHUB_OUTPUT
  64. - name: Restore cache.db
  65. uses: actions/cache/restore@v4
  66. id: cache-db-restore
  67. with:
  68. path: |
  69. .cache
  70. 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 }}
  71. # If source files changed but packages didn't, rebuild from a prior cache.
  72. restore-keys: |
  73. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:
  74. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:
  75. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }}
  76. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-
  77. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-
  78. ${{ runner.os }}-v3-
  79. - run: pnpm install
  80. - run: pnpm run build
  81. env:
  82. PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }}
  83. - name: Pre-deploy check
  84. # If the public directory doesn't exist, the build should fail.
  85. # If the public directory is empty, the build should fail.
  86. run: |
  87. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then
  88. echo "public directory not found"
  89. exit 1
  90. fi
  91. if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then
  92. echo "public directory is empty"
  93. exit 1
  94. fi
  95. if [ ! -f .BUILD_FINISHED ]; then
  96. echo ".BUILD_FINISHED not found"
  97. exit 1
  98. fi
  99. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  100. - uses: actions/upload-artifact@v4
  101. with:
  102. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  103. path: ${{ steps.ramdisk.outputs.build_dir }}
  104. if-no-files-found: error
  105. retention-days: 1
  106. compression-level: 4
  107. include-hidden-files: false
  108. - name: Cache cache.db
  109. if: always()
  110. uses: actions/cache/save@v4
  111. with:
  112. path: |
  113. .cache
  114. 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 }}
  115. diff_deployment_on_pr:
  116. if: github.ref != 'refs/heads/master'
  117. needs:
  118. - build
  119. name: Diff output
  120. runs-on: ubuntu-slim
  121. steps:
  122. - uses: actions/download-artifact@v4
  123. with:
  124. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  125. path: public
  126. - name: Diff
  127. run: |
  128. git clone --filter=tree:0 --no-tags https://github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git >/dev/null 2>&1
  129. cd ./deploy-git
  130. git fetch origin master >/dev/null 2>&1
  131. rm -rf ./*
  132. cp -rf ../public/* ./
  133. git --no-pager diff --minimal
  134. deploy_to_cloudflare_pages:
  135. needs:
  136. - build
  137. name: Deploy to Cloudflare Pages
  138. if: github.ref == 'refs/heads/master'
  139. runs-on: ubuntu-slim
  140. # matrix is a tricky way to define a variable directly in the action yaml
  141. strategy:
  142. matrix:
  143. wranglerVersion: ['3.114.12']
  144. steps:
  145. - name: Get NPM cache directory path
  146. id: npm_cache_path
  147. shell: sh
  148. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  149. - uses: actions/cache@v4
  150. with:
  151. path: |
  152. ${{ steps.npm_cache_path.outputs.dir }}
  153. node_modules
  154. key: deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-${{ matrix.wranglerVersion }}
  155. restore-keys: |
  156. deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-
  157. - uses: actions/download-artifact@v4
  158. with:
  159. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  160. path: public
  161. - uses: cloudflare/wrangler-action@v3
  162. with:
  163. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  164. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  165. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  166. wranglerVersion: ${{ matrix.wranglerVersion }}
  167. deploy_to_github_gitlab:
  168. needs:
  169. - build
  170. name: Deploy to GitHub and GitLab
  171. if: github.ref == 'refs/heads/master'
  172. runs-on: ubuntu-slim
  173. steps:
  174. - uses: actions/download-artifact@v4
  175. with:
  176. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  177. path: public
  178. - name: Upload Dist to GitLab
  179. continue-on-error: true
  180. run: |
  181. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  182. cd ./deploy-git
  183. git config --global push.dgefault matching
  184. git config --global user.email "${GITLAB_EMAIL}"
  185. git config --global user.name "${GITLAB_USER}"
  186. rm -rf ./*
  187. cp -rf ../public/* ./
  188. git add --all .
  189. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  190. git push --quiet --force origin HEAD:master
  191. cd ..
  192. rm -rf ./deploy-git
  193. env:
  194. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  195. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  196. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  197. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  198. - name: Upload Dist to GitHub
  199. continue-on-error: true
  200. run: |
  201. gh repo unarchive SukkaLab/ruleset.skk.moe --yes
  202. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git
  203. cd ./deploy-git
  204. git config --global push.default matching
  205. git config --global user.email "${GH_EMAIL}"
  206. git config --global user.name "${GH_USER}"
  207. rm -rf ./*
  208. cp -rf ../public/* ./
  209. echo "ruleset.skk.moe" > CNAME
  210. git add --all .
  211. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  212. git push --quiet --force origin HEAD:master
  213. cd ..
  214. rm -rf ./deploy-git
  215. gh repo archive SukkaLab/ruleset.skk.moe --yes
  216. env:
  217. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  218. GH_USER: ${{ secrets.GIT_USER }}
  219. GH_TOKEN: ${{ secrets.GIT_TOKEN }}