main.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: ${{ steps.npm_cache_path.outputs.dir }}
  98. key: ${{ runner.os }}-deploy-to-cloudflare-npm
  99. - uses: actions/download-artifact@v4
  100. with:
  101. name: build-artifact-${{ github.ref_name }}
  102. path: public
  103. - name: Deploy to Cloudflare Pages
  104. uses: cloudflare/wrangler-action@v3
  105. with:
  106. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  107. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  108. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  109. deploy_to_github_gitlab:
  110. needs:
  111. - build
  112. name: Deploy to GitHub and GitLab
  113. if: github.ref == 'refs/heads/master'
  114. runs-on: ubuntu-latest
  115. steps:
  116. - uses: actions/download-artifact@v4
  117. with:
  118. name: build-artifact-${{ github.ref_name }}
  119. path: public
  120. - name: Upload Dist to GitLab
  121. run: |
  122. git clone --filter=blob:none https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  123. cd ./deploy-git
  124. git config --global push.default matching
  125. git config --global user.email "${GITLAB_EMAIL}"
  126. git config --global user.name "${GITLAB_USER}"
  127. rm -rf ./*
  128. cp -rf ../public/* ./
  129. git add --all .
  130. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  131. git push --quiet --force origin HEAD:master
  132. cd ..
  133. rm -rf ./deploy-git
  134. env:
  135. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  136. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  137. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  138. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  139. - name: Upload Dist to GitHub
  140. uses: peaceiris/actions-gh-pages@v4
  141. continue-on-error: true
  142. with:
  143. personal_token: ${{ secrets.GIT_TOKEN }}
  144. user_name: ${{ secrets.GIT_USER }}
  145. user_email: ${{ secrets.GIT_EMAIL }}
  146. external_repository: SukkaLab/ruleset.skk.moe
  147. publish_branch: master
  148. publish_dir: public
  149. cname: ruleset.skk.moe