main.yml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. name: Build
  2. on:
  3. push:
  4. branches:
  5. - master
  6. schedule:
  7. - cron: "0 12 * * *"
  8. concurrency:
  9. group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  10. cancel-in-progress: true
  11. jobs:
  12. build:
  13. name: Build
  14. runs-on: ubuntu-24.04-arm
  15. steps:
  16. - run: df -h
  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. ref_name }}
  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. deploy_to_cloudflare_pages:
  117. needs:
  118. - build
  119. name: Deploy to Cloudflare Pages
  120. if: github.ref == 'refs/heads/master'
  121. runs-on: ubuntu-24.04-arm
  122. steps:
  123. - name: Get NPM cache directory path
  124. id: npm_cache_path
  125. shell: sh
  126. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  127. - name: Cache NPM cache
  128. uses: actions/cache@v4
  129. with:
  130. path: |
  131. ${{ steps.npm_cache_path.outputs.dir }}
  132. node_modules
  133. key: ${{ runner.os }}-${{ runner.arch }}-deploy-to-cloudflare-npm
  134. - uses: actions/download-artifact@v4
  135. with:
  136. name: build-artifact-${{ github.ref_name }}
  137. path: public
  138. - name: Deploy to Cloudflare Pages
  139. uses: cloudflare/wrangler-action@v3
  140. with:
  141. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  142. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  143. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  144. wranglerVersion: '3.81.0'
  145. deploy_to_github_gitlab:
  146. needs:
  147. - build
  148. name: Deploy to GitHub and GitLab
  149. if: github.ref == 'refs/heads/master'
  150. runs-on: ubuntu-24.04-arm
  151. steps:
  152. - uses: actions/download-artifact@v4
  153. with:
  154. name: build-artifact-${{ github.ref_name }}
  155. path: public
  156. - name: Upload Dist to GitLab
  157. continue-on-error: true
  158. run: |
  159. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  160. cd ./deploy-git
  161. git config --global push.default matching
  162. git config --global user.email "${GITLAB_EMAIL}"
  163. git config --global user.name "${GITLAB_USER}"
  164. rm -rf ./*
  165. cp -rf ../public/* ./
  166. git add --all .
  167. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  168. git push --quiet --force origin HEAD:master
  169. cd ..
  170. rm -rf ./deploy-git
  171. env:
  172. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  173. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  174. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  175. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  176. - name: Upload Dist to GitHub
  177. continue-on-error: true
  178. run: |
  179. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git
  180. cd ./deploy-git
  181. git config --global push.default matching
  182. git config --global user.email "${GH_EMAIL}"
  183. git config --global user.name "${GH_USER}"
  184. rm -rf ./*
  185. cp -rf ../public/* ./
  186. echo "ruleset.skk.moe" > CNAME
  187. git add --all .
  188. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  189. git push --quiet --force origin HEAD:master
  190. cd ..
  191. rm -rf ./deploy-git
  192. env:
  193. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  194. GH_USER: ${{ secrets.GIT_USER }}
  195. GH_TOKEN: ${{ secrets.GIT_TOKEN }}