main.yml 5.4 KB

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