Build #4743
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build | |
| on: | |
| push: | |
| branches: | |
| - master | |
| pull_request: | |
| schedule: | |
| - cron: '0 5 * * *' # Runs at 05:00 UTC | |
| - cron: '0 17 * * *' # Runs at 1:00 UTC | |
| workflow_dispatch: | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| build: | |
| name: Build | |
| runs-on: ubuntu-24.04-arm | |
| steps: | |
| - uses: smorimoto/tune-github-hosted-runner-network@v1 | |
| # https://github.com/actions/runner-images/issues/1187 | |
| - uses: actions/checkout@v6 | |
| with: | |
| persist-credentials: false | |
| - uses: pnpm/action-setup@v4 | |
| with: | |
| run_install: false | |
| - uses: actions/setup-node@v6 | |
| with: | |
| node-version-file: ".node-version" | |
| cache: "pnpm" | |
| - name: Grab Building Folder | |
| id: ramdisk | |
| run: | | |
| echo "build_dir=previous-build-${{ github.run_id }}-${{ github.run_number }}" >> $GITHUB_OUTPUT | |
| - name: Download Previous Build | |
| uses: actions/checkout@v6 | |
| with: | |
| repository: SukkaLab/ruleset.skk.moe | |
| persist-credentials: false | |
| filter: "tree:0" # we don't care about git history here | |
| fetch-tags: false | |
| path: ${{ steps.ramdisk.outputs.build_dir }} | |
| - name: Setup build folder | |
| run: | | |
| if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/.git ]; then | |
| echo ".git not found" | |
| exit 1 | |
| fi | |
| rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git" | |
| if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/List ]; then | |
| echo "List not found" | |
| exit 1 | |
| fi | |
| echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}" | |
| - name: Get current date | |
| id: date | |
| run: | | |
| echo "date=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT | |
| echo "year=$(date +'%Y')" >> $GITHUB_OUTPUT | |
| echo "month=$(date +'%m')" >> $GITHUB_OUTPUT | |
| echo "day=$(date +'%d')" >> $GITHUB_OUTPUT | |
| echo "hour=$(date +'%H')" >> $GITHUB_OUTPUT | |
| echo "minute=$(date +'%M')" >> $GITHUB_OUTPUT | |
| echo "second=$(date +'%S')" >> $GITHUB_OUTPUT | |
| - name: Restore cache.db | |
| uses: actions/cache/restore@v5 | |
| id: cache-db-restore | |
| with: | |
| path: | | |
| .cache | |
| 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 }} | |
| # If source files changed but packages didn't, rebuild from a prior cache. | |
| restore-keys: | | |
| ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}: | |
| ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}: | |
| ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} | |
| ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}- | |
| ${{ runner.os }}-v3-${{ steps.date.outputs.year }}- | |
| ${{ runner.os }}-v3- | |
| - run: pnpm install | |
| - run: pnpm run build | |
| env: | |
| PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }} | |
| - name: Pre-deploy check | |
| # If the public directory doesn't exist, the build should fail. | |
| # If the public directory is empty, the build should fail. | |
| run: | | |
| if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then | |
| echo "public directory not found" | |
| exit 1 | |
| fi | |
| if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then | |
| echo "public directory is empty" | |
| exit 1 | |
| fi | |
| if [ ! -f .BUILD_FINISHED ]; then | |
| echo ".BUILD_FINISHED not found" | |
| exit 1 | |
| fi | |
| echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}" | |
| - uses: actions/upload-artifact@v6 | |
| with: | |
| name: build-artifact-${{ github.sha }}-${{ github.run_number }} | |
| path: ${{ steps.ramdisk.outputs.build_dir }} | |
| if-no-files-found: error | |
| retention-days: 1 | |
| compression-level: 4 | |
| include-hidden-files: false | |
| - name: Cache cache.db | |
| if: always() | |
| uses: actions/cache/save@v5 | |
| with: | |
| path: | | |
| .cache | |
| 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 }} | |
| diff_deployment_on_pr: | |
| if: github.ref != 'refs/heads/master' | |
| needs: | |
| - build | |
| name: Diff output | |
| runs-on: ubuntu-slim | |
| steps: | |
| - uses: actions/download-artifact@v7 | |
| with: | |
| name: build-artifact-${{ github.sha }}-${{ github.run_number }} | |
| path: public | |
| - name: Diff | |
| run: | | |
| git clone --filter=tree:0 --no-tags https://github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git >/dev/null 2>&1 | |
| cd ./deploy-git | |
| git fetch origin master >/dev/null 2>&1 | |
| rm -rf ./* | |
| cp -rf ../public/* ./ | |
| git --no-pager diff --minimal | |
| deploy_to_cloudflare_pages: | |
| needs: | |
| - build | |
| name: Deploy to Cloudflare Pages | |
| if: github.ref == 'refs/heads/master' | |
| runs-on: ubuntu-slim | |
| # matrix is a tricky way to define a variable directly in the action yaml | |
| strategy: | |
| matrix: | |
| wranglerVersion: ['3.114.12'] | |
| steps: | |
| - name: Get NPM cache directory path | |
| id: npm_cache_path | |
| shell: sh | |
| run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT | |
| - uses: actions/cache@v5 | |
| with: | |
| path: | | |
| ${{ steps.npm_cache_path.outputs.dir }} | |
| node_modules | |
| key: deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-${{ matrix.wranglerVersion }} | |
| restore-keys: | | |
| deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler- | |
| - uses: actions/download-artifact@v7 | |
| with: | |
| name: build-artifact-${{ github.sha }}-${{ github.run_number }} | |
| path: public | |
| - uses: cloudflare/wrangler-action@v3 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
| command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main | |
| wranglerVersion: ${{ matrix.wranglerVersion }} | |
| deploy_to_github_gitlab: | |
| needs: | |
| - build | |
| name: Deploy to GitHub and GitLab | |
| if: github.ref == 'refs/heads/master' | |
| runs-on: ubuntu-slim | |
| steps: | |
| - uses: actions/download-artifact@v7 | |
| with: | |
| name: build-artifact-${{ github.sha }}-${{ github.run_number }} | |
| path: public | |
| - name: Upload Dist to GitLab | |
| continue-on-error: true | |
| run: | | |
| git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git | |
| cd ./deploy-git | |
| git config --global push.dgefault matching | |
| git config --global user.email "${GITLAB_EMAIL}" | |
| git config --global user.name "${GITLAB_USER}" | |
| rm -rf ./* | |
| cp -rf ../public/* ./ | |
| git add --all . | |
| git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}" | |
| git push --quiet --force origin HEAD:master | |
| cd .. | |
| rm -rf ./deploy-git | |
| env: | |
| GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }} | |
| GITLAB_USER: ${{ secrets.GITLAB_USER }} | |
| GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }} | |
| GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }} | |
| - name: Upload Dist to GitHub | |
| continue-on-error: true | |
| run: | | |
| gh repo unarchive SukkaLab/ruleset.skk.moe --yes | |
| git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git | |
| cd ./deploy-git | |
| git config --global push.default matching | |
| git config --global user.email "${GH_EMAIL}" | |
| git config --global user.name "${GH_USER}" | |
| rm -rf ./* | |
| cp -rf ../public/* ./ | |
| echo "ruleset-mirror.skk.moe" > CNAME | |
| git add --all . | |
| git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}" | |
| git push --quiet --force origin HEAD:master | |
| cd .. | |
| rm -rf ./deploy-git | |
| # gh repo archive SukkaLab/ruleset.skk.moe --yes | |
| env: | |
| GH_EMAIL: ${{ secrets.GIT_EMAIL }} | |
| GH_USER: ${{ secrets.GIT_USER }} | |
| GH_TOKEN: ${{ secrets.GIT_TOKEN }} | |
| remove_artifacts: | |
| needs: | |
| - deploy_to_cloudflare_pages | |
| - deploy_to_github_gitlab | |
| name: Remove Artifacts after Deployment | |
| runs-on: ubuntu-slim | |
| steps: | |
| - uses: geekyeggo/delete-artifact@v5 | |
| with: | |
| name: build-artifact-${{ github.sha }}-${{ github.run_number }} |