main.yml 6.8 KB

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