diff --git a/.github/workflows/hugo.yml b/.github/workflows/hugo.yml index f46ac94..b7ee0d8 100644 --- a/.github/workflows/hugo.yml +++ b/.github/workflows/hugo.yml @@ -1,77 +1,148 @@ -# Sample workflow for building and deploying a Hugo site to GitHub Pages -name: Deploy Hugo site to Pages +name: Build and deploy on: - # Runs on pushes targeting the default branch push: - branches: ["main"] - - # Allows you to run this workflow manually from the Actions tab + branches: + - main workflow_dispatch: -# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages permissions: contents: read pages: write id-token: write -# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. -# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. concurrency: - group: "pages" + group: pages cancel-in-progress: false -# Default to bash defaults: run: shell: bash jobs: - # Build job build: runs-on: ubuntu-latest env: + # Define tool versions + DART_SASS_VERSION: 1.101.0 + GO_VERSION: 1.26.4 HUGO_VERSION: 0.163.1 + NODE_VERSION: 24.16.0 + + # Set the build time zone + TZ: Asia/Shanghai steps: - - name: Install Hugo CLI - run: | - wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ - && sudo dpkg -i ${{ runner.temp }}/hugo.deb - - name: Install Dart Sass - run: sudo snap install dart-sass - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v6 with: submodules: recursive + fetch-depth: 0 + - name: Setup Pages id: pages - uses: actions/configure-pages@v5 - - name: Install Node.js dependencies - run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true" - - name: Build with Hugo - env: - HUGO_CACHEDIR: ${{ runner.temp }}/hugo_cache - HUGO_ENVIRONMENT: production + uses: actions/configure-pages@v6 + + - name: Create a local tools directory run: | - hugo \ + mkdir -p "${HOME}/.local" + + - name: Install Go + if: hashFiles('go.mod') != '' + uses: actions/setup-go@v6 + with: + go-version: ${{ env.GO_VERSION }} + cache: false + + - name: Install Node.js + if: hashFiles('package-lock.json') != '' + uses: actions/setup-node@v6 + with: + node-version: ${{ env.NODE_VERSION }} + + - name: Install Dart Sass + run: | + echo "Installing Dart Sass ${DART_SASS_VERSION}..." + curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" + tar -C "${HOME}/.local" -xf "${{ runner.temp }}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" + echo "${HOME}/.local/dart-sass" >> "${GITHUB_PATH}" + + - name: Install Hugo + run: | + echo "Installing Hugo ${HUGO_VERSION}..." + curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz" + mkdir "${HOME}/.local/hugo" + tar -C "${HOME}/.local/hugo" -xf "${{ runner.temp }}/hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz" + echo "${HOME}/.local/hugo" >> "${GITHUB_PATH}" + + - name: Log tool versions + run: | + echo "Logging tool versions..." + command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed" + command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed" + command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed" + command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed" + + - name: Configure Git + run: | + echo "Configuring Git..." + git config --global core.quotepath false + + - name: Fetch full Git history + run: | + if [[ $(git rev-parse --is-shallow-repository) == true ]]; then + echo "Fetching full Git history..." + git fetch --unshallow + fi + + - name: Initialize Git submodules + run: | + if [[ -f .gitmodules ]]; then + echo "Initializing Git submodules..." + git submodule update --init --recursive + fi + + - name: Install Node.js dependencies + run: | + if [[ -f package-lock.json ]]; then + echo "Installing Node.js dependencies..." + npm ci + fi + + - name: Cache restore + id: cache-restore + uses: actions/cache/restore@v5 + with: + path: ${{ runner.temp }}/hugo_cache + key: hugo-${{ github.run_id }} + restore-keys: hugo- + + - name: Build + run: | + echo "Building the project..." + hugo build \ --gc \ --minify \ - --cleanDestinationDir \ - --destination ./public \ - --baseURL "${{ steps.pages.outputs.base_url }}/" - - name: Upload artifact - uses: actions/upload-pages-artifact@v3 + --baseURL "${{ steps.pages.outputs.base_url }}/" \ + --cacheDir "${{ runner.temp }}/hugo_cache" + + - name: Cache save + uses: actions/cache/save@v5 with: + path: ${{ runner.temp }}/hugo_cache + key: ${{ steps.cache-restore.outputs.cache-primary-key }} + + - name: Upload artifact + uses: actions/upload-pages-artifact@v5 + with: + include-hidden-files: false path: ./public - # Deployment job deploy: + runs-on: ubuntu-latest + needs: build environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest - needs: build steps: - name: Deploy to GitHub Pages - id: deployment uses: actions/deploy-pages@v5