main.yml 8.0 KB

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