on:
  workflow_dispatch:
  push:
    branches: [main]
    paths:
    - design/**

name: Update GitHub Pages

env:
  rust_version: 1.76.0

# Allow only one doc pages build to run at a time for the entire smithy-rs repo
concurrency:
  group: github-pages-yml
  cancel-in-progress: true

jobs:
  build-and-deploy-docs:
    if: github.repository == 'smithy-lang/smithy-rs'
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        persist-credentials: false
    - uses: dtolnay/rust-toolchain@master
      with:
        toolchain: ${{ env.rust_version }}
    - name: Generate docs
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        git config --local user.name "AWS SDK Rust Bot"
        git config --local user.email "aws-sdk-rust-primary@amazon.com"
        git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}
        git fetch origin github-pages

        pushd design &>/dev/null
        cargo install mdbook
        cargo install --locked mdbook-mermaid
        mdbook build --dest-dir ../../output
        popd &>/dev/null

        git checkout origin/github-pages -b github-pages
        rm -rf design
        mv ../output design
        git add design
        git commit -m "Update design docs"
        git push origin github-pages
