main.yml 8.6 KB

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