main.yml 6.4 KB

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