name: CI

on:
  pull_request:
  workflow_dispatch:
  push:
    branches:
      - main
  schedule:
    - cron: "0 0 */5 * *"

jobs:
  typos:
    name: "Check typos"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check typos
        uses: crate-ci/typos@master
        with:
          config: ./typos.toml

  lychee:
    name: "Check links"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # Takes too long
      #- name: Restore lychee cache
      #  uses: actions/cache@v3
      #  with:
      #    path: .lycheecache
      #    key: lychee-cache
      #    restore-keys: lychee-cache
      #key: cache-lychee-${{ github.sha }}
      #restore-keys: cache-lychee-
      - name: Check links
        id: lychee
        uses: lycheeverse/lychee-action@v1
        with:
          args: --config ./lychee.toml .
          fail: true

  clippy:
    name: "Clippy"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: "Install Rust"
        run: |
          sudo apt update
          DEBIAN_FRONTEND=noninteractive \
            sudo apt install --yes rustup
            rustup default 1.91.0

      - name: Run clippy
        run: cargo clippy --all-targets --all-features

  #check-version:
  #  name: Check version
  #  runs-on: ubuntu-latest
  #  # Skip this job on push to main
  #  # if: github.event_name != 'push' || github.ref != 'refs/heads/main'
  #  steps:
  #    - uses: actions/checkout@v4
  #      with:
  #        fetch-depth: 0
  #
  #    - name: check version
  #      run: |
  #        # Get base branch (usually main)
  #        BASE_REF="${{ github.base_ref }}"
  #        if [ -z "$BASE_REF" ]; then
  #          BASE_REF="main"
  #        fi
  #
  #        # Get current version
  #        CURRENT_VERSION=$(grep -m 1 '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
  #        echo "Current version: $CURRENT_VERSION"
  #
  #        # Get version from base branch
  #        git fetch origin $BASE_REF
  #        BASE_VERSION=$(git show origin/$BASE_REF:Cargo.toml | grep -m 1 '^version = ' | sed 's/version = "\(.*\)"/\1/')
  #        echo "Base version: $BASE_VERSION"
  #
  #        # Check if version changed
  #        if [ "$CURRENT_VERSION" = "$BASE_VERSION" ]; then
  #          echo "::error::Version in Cargo.toml has not been changed."
  #          exit 1
  #        else
  #          echo "Version has been updated from $BASE_VERSION to $CURRENT_VERSION"
  #        fi

  buildandtest:
    name: "Build and test"
    runs-on: ubuntu-latest
    needs:
    #  - check-version
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - name: "Install Rust"
        run: |
          sudo apt update
          DEBIAN_FRONTEND=noninteractive \
            sudo apt install --yes rustup
            rustup default 1.91.0

      - name: Build
        run: cargo build --release --package webpage

      - name: Test
        run: cargo test --release --workspace

      - uses: actions/upload-artifact@v3
        with:
          name: webpage
          path: "target/release/webpage"
          retention-days: 10


  docker:
    name: "Build container"
    runs-on: ubuntu-latest
    # Only run on push to main or workflow_dispatch
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
    needs:
      - typos
      - clippy
      - buildandtest
    #  - check-version
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v4

      - name: Log in to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ github.server_url }}
          username: mark
          password: ${{ secrets.API_TOKEN }}

      - name: Extract metadata for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: git.betalupi.com/mark/betalupi-webpage
          tags: |
            type=ref,event=branch
            type=sha,prefix={{branch}}-
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          # Tags looks like ghcr.io/user/app:latest

  deploy:
    name: "Deploy on waypoint"
    runs-on: ubuntu-latest
    # Only run on push to main or workflow_dispatch
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
    needs:
      - docker

    steps:
      - name: Setup SSH
        if: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'workflow_dispatch' }}
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_SSH_PRIV_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          eval "$(ssh-agent -s)"
          ssh-add ~/.ssh/id_ed25519
          ssh-keyscan -p 5342 waypoint.betalupi.com >> ~/.ssh/known_hosts

      - name: Deploy
        if: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'workflow_dispatch' }}
        run: |
          ssh mark@waypoint.betalupi.com -p 5342 \
              "
              cd /home/mark/docker/betalupi-webpage; \
              docker compose down; \
              docker image prune -af; \
              docker compose up -d;
              "
