diff --git a/.github/workflows/build-and-deploy-site.yml b/.github/workflows/build-and-deploy-site.yml new file mode 100644 index 0000000..6970e59 --- /dev/null +++ b/.github/workflows/build-and-deploy-site.yml @@ -0,0 +1,58 @@ +name: Build and Deploy Site + +on: + push: + branches: [master] + workflow_dispatch: + +permissions: + contents: write + +defaults: + run: + shell: bash + +jobs: + build-and-deploy-site: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Setup Hugo Extended + uses: peaceiris/actions-hugo@v3 + with: + hugo-version: '0.158.0' + extended: true + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: '20' + cache: 'npm' + + - name: Setup site dependencies + run: make setup + + - name: Build site + run: make build-production BASE_URL="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/" + + - name: Prepare Pages output + run: touch public/.nojekyll + + - name: Deploy site to GitHub Pages + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_branch: gh-pages + publish_dir: ./public + keep_files: true diff --git a/.github/workflows/build-and-preview-site.yml b/.github/workflows/build-and-preview-site.yml new file mode 100644 index 0000000..78d2d59 --- /dev/null +++ b/.github/workflows/build-and-preview-site.yml @@ -0,0 +1,100 @@ +name: Build and Preview Site + +on: + pull_request_target: + branches: [master] + types: [opened, synchronize, reopened, closed] + +permissions: + contents: write + pull-requests: write + +concurrency: + group: preview-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: true + +defaults: + run: + shell: bash + +jobs: + build-and-deploy-preview: + runs-on: ubuntu-latest + + steps: + - name: Checkout PR code + if: github.event.action != 'closed' + uses: actions/checkout@v6 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + fetch-depth: 0 + + - name: Checkout for cleanup + if: github.event.action == 'closed' + uses: actions/checkout@v6 + with: + ref: gh-pages + fetch-depth: 0 + + - name: Setup Go + if: github.event.action != 'closed' + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Setup Hugo Extended + if: github.event.action != 'closed' + uses: peaceiris/actions-hugo@v3 + with: + hugo-version: '0.158.0' + extended: true + + - name: Setup Node + if: github.event.action != 'closed' + uses: actions/setup-node@v4 + with: + node-version: '20' + cache: 'npm' + + - name: Setup site dependencies + if: github.event.action != 'closed' + run: make setup + + - name: Build PR preview + if: github.event.action != 'closed' + run: | + make build-preview BASE_URL="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/" + touch public/.nojekyll + + - name: Deploy PR preview + if: github.event.action != 'closed' + uses: rossjrw/pr-preview-action@v1.6.3 + with: + source-dir: ./public + preview-branch: gh-pages + umbrella-dir: pr-preview + action: auto + comment: false + + - name: Comment PR with Preview URL + if: github.event.action != 'closed' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: pr-preview + message: | + 🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}** + + 🌐 **Preview URL**: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/ + + _This preview will be updated automatically when you push new commits to this PR._ + + - name: Cleanup PR preview on close + if: github.event.action == 'closed' + uses: rossjrw/pr-preview-action@v1.6.3 + with: + preview-branch: gh-pages + umbrella-dir: pr-preview + action: remove diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml deleted file mode 100644 index e2f1fd3..0000000 --- a/.github/workflows/gh-pages.yml +++ /dev/null @@ -1,72 +0,0 @@ -name: Build and Deploy to GitHub Pages - -on: - push: - branches: - - master - paths-ignore: - - ".github/**" - pull_request: - branches: - - master - workflow_dispatch: - -permissions: - contents: read - pages: write - id-token: write - -concurrency: - group: "pages" - cancel-in-progress: false - -jobs: - build: - name: Build Hugo site - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v5 - with: - fetch-depth: 0 - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version-file: go.mod - cache: true - - - name: Setup Hugo Extended - uses: peaceiris/actions-hugo@v3 - with: - hugo-version: "0.158.0" - extended: true - - - name: Install Node.js dependencies - run: npm ci - - - name: Build Hugo site - run: | - hugo --minify \ - --baseURL "https://${{ github.repository_owner }}.github.io/academy-example/" - - - name: Upload Pages artifact - if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' - uses: actions/upload-pages-artifact@v3 - with: - path: ./public - - deploy: - name: Deploy to GitHub Pages - needs: build - if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' - runs-on: ubuntu-latest - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - - steps: - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v4 diff --git a/Makefile b/Makefile index 697a47f..54a163c 100644 --- a/Makefile +++ b/Makefile @@ -18,18 +18,32 @@ include .github/build/Makefile.show-help.mk #---------------------------------------------------------------------------- # Academy # --------------------------------------------------------------------------- -.PHONY: setup build site clean check-go theme-update +.PHONY: setup build build-production build-preview site clean check-go theme-update + +BASE_URL ?= ## ------------------------------------------------------------ ----LOCAL_BUILDS: Show help for available targets ## Local: Install site dependencies setup: - npm i + @if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then \ + npm ci; \ + else \ + npm i; \ + fi ## Local: Build site for local consumption build: - hugo build + BASE_URL="$(BASE_URL)" npm run build + +## CI: Build production site output +build-production: + BASE_URL="$(BASE_URL)" npm run build:production + +## CI: Build preview site output and mark it non-indexable +build-preview: + BASE_URL="$(BASE_URL)" npm run build:preview ## Local: Build and run site locally with draft and future content enabled. site: check-go diff --git a/package.json b/package.json index 0f42c1c..d2a0243 100644 --- a/package.json +++ b/package.json @@ -10,15 +10,14 @@ "bugs": "https://github.com/google/docsy-example/issues", "spelling": "cSpell:ignore docsy hugo htmltest precheck postbuild rtlcss -", "scripts": { - "_build": "npm run _hugo-dev --", "_check:links": "echo IMPLEMENTATION PENDING for check-links; echo", "_hugo": "hugo --cleanDestinationDir", "_hugo-dev": "npm run _hugo -- -e dev -DFE", "_local": "npx cross-env HUGO_MODULE_WORKSPACE=docsy.work", "_serve": "npm run _hugo-dev -- --minify serve --renderToMemory", - "build:preview": "npm run _hugo-dev -- --minify --baseURL \"${DEPLOY_PRIME_URL:-/}\"", - "build:production": "npm run _hugo -- --minify", - "build": "npm run _build -- ", + "build": "sh -c 'hugo build --cleanDestinationDir ${BASE_URL:+--baseURL \"$BASE_URL\"}'", + "build:preview": "cross-env HUGO_ENVIRONMENT=production HUGO_ENV=production HUGO_PREVIEW=true sh -c 'hugo build -D --gc --buildFuture --cleanDestinationDir --minify ${BASE_URL:+--baseURL \"$BASE_URL\"}'", + "build:production": "cross-env HUGO_ENVIRONMENT=production HUGO_ENV=production sh -c 'hugo build -D --gc --buildFuture --cleanDestinationDir --minify ${BASE_URL:+--baseURL \"$BASE_URL\"}'", "check:links:all": "HTMLTEST_ARGS= npm run _check:links", "check:links": "npm run _check:links", "clean": "rm -Rf public/* resources", @@ -26,7 +25,7 @@ "make:public": "git init -b main public", "precheck:links:all": "npm run build", "precheck:links": "npm run build", - "postbuild:preview": "npm run _check:links", + "postbuild:preview": "printf 'User-agent: *\\nDisallow: /\\n' > public/robots.txt && find public -name '*.html' -exec perl -0pi -e 's|||g; s|||g; s|||i unless /]+name=\"?robots\"?[^>]+noindex, nofollow/i' {} +", "postbuild:production": "npm run _check:links", "serve": "npm run _serve", "test": "npm run check:links",