main.yml 6.7 KB

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