main.yml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: Checkout
  17. uses: actions/checkout@v4
  18. with:
  19. persist-credentials: false
  20. - uses: pnpm/action-setup@v4
  21. name: Install pnpm
  22. with:
  23. run_install: false
  24. - name: Use Node.js
  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. - name: Install dependencies
  55. run: pnpm install
  56. - run: pnpm run 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 public ]; then
  62. echo "public directory not found"
  63. exit 1
  64. fi
  65. if [ ! "$(ls -A public)" ]; then
  66. echo "public directory is empty"
  67. exit 1
  68. fi
  69. - uses: actions/upload-artifact@v4
  70. with:
  71. name: build-artifact-${{ github. ref_name }}
  72. path: public
  73. if-no-files-found: error
  74. retention-days: 1
  75. compression-level: 4
  76. include-hidden-files: false
  77. - name: Cache cache.db
  78. if: always()
  79. uses: actions/cache/save@v4
  80. with:
  81. path: |
  82. .cache
  83. 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 }}
  84. deploy_to_cloudflare_pages:
  85. needs:
  86. - build
  87. name: Deploy to Cloudflare Pages
  88. if: github.ref == 'refs/heads/master'
  89. runs-on: ubuntu-latest
  90. steps:
  91. - uses: actions/download-artifact@v4
  92. with:
  93. name: build-artifact-${{ github. ref_name }}
  94. path: public
  95. - name: Deploy to Cloudflare Pages
  96. uses: cloudflare/wrangler-action@v3
  97. with:
  98. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  99. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  100. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  101. deploy_to_github_gitlab:
  102. needs:
  103. - build
  104. name: Deploy to GitHub and GitLab
  105. if: github.ref == 'refs/heads/master'
  106. runs-on: ubuntu-latest
  107. steps:
  108. - uses: actions/download-artifact@v4
  109. with:
  110. name: build-artifact-${{ github.ref_name }}
  111. path: public
  112. - name: Upload Dist to GitLab
  113. run: |
  114. git clone --filter=blob:none https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  115. cd ./deploy-git
  116. git config --global push.default matching
  117. git config --global user.email "${GITLAB_EMAIL}"
  118. git config --global user.name "${GITLAB_USER}"
  119. rm -rf ./*
  120. cp -rf ../public/* ./
  121. git add --all .
  122. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  123. git push --quiet --force origin HEAD:master
  124. cd ..
  125. rm -rf ./deploy-git
  126. env:
  127. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  128. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  129. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  130. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  131. - name: Upload Dist to GitHub
  132. uses: peaceiris/actions-gh-pages@v4
  133. continue-on-error: true
  134. with:
  135. personal_token: ${{ secrets.GIT_TOKEN }}
  136. user_name: ${{ secrets.GIT_USER }}
  137. user_email: ${{ secrets.GIT_EMAIL }}
  138. external_repository: SukkaLab/ruleset.skk.moe
  139. publish_branch: master
  140. publish_dir: public
  141. cname: ruleset.skk.moe