name: ci

on:
  push:
    branches:
      - main
      - master
  release:
    types: [published]

permissions:
  contents: write
  packages: write

jobs:
  container-build:
    if: github.event_name == 'release'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Remove unnecessary files to release disk space
        run: |
          sudo rm -rf \
            "$AGENT_TOOLSDIRECTORY" \
            /opt/ghc \
            /opt/google/chrome \
            /opt/microsoft/msedge \
            /opt/microsoft/powershell \
            /opt/pipx \
            /usr/lib/mono \
            /usr/local/julia* \
            /usr/local/lib/android \
            /usr/local/lib/node_modules \
            /usr/local/share/chromium \
            /usr/local/share/powershell \
            /usr/local/share/powershell \
            /usr/share/dotnet \
            /usr/share/swift

      - name: Log in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build & push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: Dockerfile
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:latest
            ghcr.io/${{ github.repository }}:${{ github.ref_name }}

  deploy-docs:
    if: github.event_name == 'release'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Configure Git credentials
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
      - uses: actions/setup-python@v5
        with: { python-version: '3.x' }
      - run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
      - uses: actions/cache@v4
        with:
          key: mkdocs-material-${{ env.cache_id }}
          path: .cache
          restore-keys: mkdocs-material-
      - run: pip install mkdocs-material mkdocs-awesome-pages-plugin mkdocs-glightbox
      - run: mkdocs gh-deploy --force