# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
# docs: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions

name: 🧪 Tests

on:
  push:
    branches: [master, main]
    tags-ignore: ['**']
    paths-ignore: ['**.md']
  pull_request:
    paths-ignore: ['**.md']

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
  gitleaks:
    name: Check for GitLeaks
    runs-on: ubuntu-latest
    steps:
      - {uses: actions/checkout@v4, with: {fetch-depth: 0}}
      - uses: gacts/gitleaks@v1

  golangci-lint:
    name: Run golangci-lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - {uses: actions/setup-go@v5, with: {go-version-file: go.mod}}
      - uses: golangci/golangci-lint-action@v6

  go-test:
    name: Unit tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - {uses: actions/setup-go@v5, with: {go-version-file: go.mod}}
      - run: go test -race ./...

  build:
    name: Build for ${{ matrix.os }} (${{ matrix.arch }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        os: [linux, darwin, windows] # freebsd
        arch: [amd64, arm64] # 386
    needs: [golangci-lint, go-test]
    steps:
      - uses: actions/checkout@v4
      - {uses: actions/setup-go@v5, with: {go-version-file: go.mod}}
      - {uses: gacts/github-slug@v1, id: slug}
      - env:
          GOOS: ${{ matrix.os }}
          GOARCH: ${{ matrix.arch }}
          CGO_ENABLED: 0
          LDFLAGS: -s -w -X gh.tarampamp.am/error-pages/internal/appmeta.version=${{ steps.slug.outputs.commit-hash-short }}
        run: go build -trimpath -ldflags "$LDFLAGS" -o ./error-pages ./cmd/error-pages/
      - if: matrix.os == 'linux' && matrix.arch == 'amd64'
        run: ./error-pages --version && ./error-pages -h
      - if: matrix.os == 'linux' && matrix.arch == 'amd64'
        run: mkdir ./out && ./error-pages --log-level=debug build --index --target-dir ./out
      - if: matrix.os == 'linux' && matrix.arch == 'amd64'
        run: |
          test -f ./out/index.html
          test -f ./out/ghost/404.html
          test -f ./out/l7/404.html
          test -f ./out/shuffle/404.html
          test -f ./out/noise/404.html
          test -f ./out/hacker-terminal/404.html
          test -f ./out/cats/404.html
          test -f ./out/lost-in-space/404.html
          test -f ./out/app-down/404.html
          test -f ./out/connection/404.html
          test -f ./out/orient/404.html

  docker-image:
    name: Build the docker image
    runs-on: ubuntu-latest
    needs: [golangci-lint, go-test]
    steps:
      - uses: actions/checkout@v4
      - {uses: gacts/github-slug@v1, id: slug}
      - uses: docker/build-push-action@v6
        with:
          context: .
          file: Dockerfile
          push: false
          build-args: "APP_VERSION=${{ steps.slug.outputs.commit-hash-short }}"
          tags: app:ci
