main.yml 6.6 KB

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