diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index cd2dc9b..0df2b5f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,9 +1,8 @@ name: CI on: + pull_request: push: - branches: - - main permissions: contents: read @@ -14,46 +13,8 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout repo + uses: actions/checkout@v4 - - uses: peaceiris/actions-mdbook@v2 - with: - mdbook-version: "latest" - - - name: Build mdbook - run: mdbook build --dest-dir out - - - name: Cache Cargo registry and Cargo bin - id: cargo-cache - uses: actions/cache@v5 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - ~/.cargo/bin - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock','**/Cargo.toml') }} - restore-keys: | - ${{ runner.os }}-cargo- - - - name: Install mdbook-utils - if: steps.cargo-cache.outputs.cache-hit != 'true' - run: cargo install mdbook-utils - - - name: Generate sitemap - run: mdbook-utils sitemap -o out/sitemap.xml -b https://rsdocsjp.org/ -y - - - name: Upload Pages artifact - uses: actions/upload-pages-artifact@v4 - with: - path: ./out - - deploy: - needs: build - runs-on: ubuntu-latest - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - steps: - - name: Deploy - id: deployment - uses: actions/deploy-pages@v4 + - name: Build Check + uses: ./.github/workflows/build diff --git a/.github/workflows/build/action.yml b/.github/workflows/build/action.yml new file mode 100644 index 0000000..6e4d091 --- /dev/null +++ b/.github/workflows/build/action.yml @@ -0,0 +1,34 @@ +name: Build + +runs: + using: 'composite' + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Install mdbook + uses: peaceiris/actions-mdbook@v2 + with: + mdbook-version: "latest" + + - name: Build mdbook + run: mdbook build --dest-dir out + + - name: Cache Cargo registry and Cargo bin + id: cargo-cache + uses: actions/cache@v5 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + ~/.cargo/bin + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock','**/Cargo.toml') }} + restore-keys: | + ${{ runner.os }}-cargo- + + - name: Install mdbook-utils + if: steps.cargo-cache.outputs.cache-hit != 'true' + run: cargo install mdbook-utils + + - name: Generate sitemap + run: mdbook-utils sitemap -o out/sitemap.xml -b https://rsdocsjp.org/ -y diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..5b0a7f3 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,63 @@ +name: Deploy site to Github Pages + +on: + push: + branches: ["main"] + workflow_dispatch: + +permissions: + contents: write + +concurrency: + group: "pages" + cancel-in-progress: true + +jobs: + build-and-commit: + name: Deploy + + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Build Check + uses: ./.github/workflows/build + + - name: Move out directory + run: | + mkdir -p tmp/out + mv out tmp/out + + - name: Checkout gh-pages + uses: actions/checkout@v4 + with: + ref: gh-pages + + - name: Preserve specific directories + run: | + mkdir -p preserve + mv out/preview preserve/preview + + - name: Adapt new build + run: | + mv -f tmp/out out + mv -f preserve/preview out/preview + + - name: Clean + run: | + find . -type f ! -path "./.github/*" ! -path "./out/*" ! -path "./.git/*" -delete + + - name: Commit changes to gh-pages branch + run: | + git config --global user.name "GitHub Actions" + git config --global user.email "action@github.com" + git add -N . + set +e + git diff --exit-code --quiet + if [[ $? -eq 1 ]]; then + git add . + git commit -m "Deploy to gh-pages: ${{ github.sha }}" + git push origin gh-pages + fi diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml new file mode 100644 index 0000000..72141c0 --- /dev/null +++ b/.github/workflows/gh-pages.yml @@ -0,0 +1,40 @@ +name: Deploy site to Github Pages + +on: + push: + branches: ["gh-pages"] + workflow_dispatch: + +permissions: + contents: read + pages: write + id-token: write + +concurrency: + group: "pages" + cancel-in-progress: true + +jobs: + deploy: + name: Deploy + + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: gh-pages + + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 + with: + path: ./out + + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 \ No newline at end of file diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 0000000..6018fe4 --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,75 @@ +name: Deploy PR previews + +on: + pull_request: + types: [opened, synchronize, reopened, closed] + +permissions: + contents: write + pull-requests: write + +concurrency: + group: pr-${{ github.event.pull_request.number }} + cancel-in-progress: true + +env: + PAGE_DIR: pr-preview-${{ github.event.pull_request.number }} + +jobs: + build-and-commit: + if: github.event.action != 'closed' + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Build Check + uses: ./.github/workflows/build + + - name: Move out directory + run: | + mkdir -p tmp/out + mv -f out tmp/out + + - name: Checkout gh-pages + uses: actions/checkout@v4 + with: + ref: gh-pages + + - name: Commit changes to gh-pages branch + run: | + git config --global user.name "GitHub Actions" + git config --global user.email "action@github.com" + mkdir -p out/preview/${{ env.PAGE_DIR }} + mv tmp/out /out/preview/${{ env.PAGE_DIR }} + git add . + git commit -m "Deploy PR preview for #${{ github.event.pull_request.number }}" + git push origin gh-pages + + - name: Comment PR + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `Preview deployed to: https://${context.repo.owner}.github.io/preview/${process.env.PAGE_DIR}` + }) + + cleanup: + if: github.event.action == 'closed' + runs-on: ubuntu-latest + steps: + - name: Checkout gh-pages + uses: actions/checkout@v4 + with: + ref: gh-pages + + - name: Remove preview directory + run: | + rm -rf ./out/preview/${{ env.PAGE_DIR }} || exit 0 + git config user.name github-actions + git config user.email github-actions@github.com + git add . + git commit -m "Remove PR preview for #${{ github.event.pull_request.number }}" && git push origin gh-pages || echo "No changes to commit"