name: release

on:
  push:
    branches:
      - 'v[0-9]+.[0-9]+.[0-9]+'

permissions:
  contents: write

jobs:
  create_release:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Extract branch name
        shell: bash
        run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
        id: extract_branch
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.extract_branch.outputs.branch }}
          release_name: ${{ steps.extract_branch.outputs.branch }}
          body: |
            Please see usage on [readme](https://github.com/gtsteffaniak/filebrowser).
            Docker repo: https://hub.docker.com/r/gtstef/filebrowser/tags
          draft: false
          prerelease: false
  test-backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: 'stable'
      - working-directory: backend
        run: go test -race -v ./...
  lint-backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
            go-version: 'stable'
      - uses: golangci/golangci-lint-action@v6
        with:
          version: v1.59
          working-directory: backend
  format-backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: 'stable'
      - working-directory: backend
        run: go fmt ./...
  lint-frontend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
      - working-directory: frontend
        run: npm i eslint && npm run lint

  push_release_to_registry:
    needs: [lint-frontend, lint-backend, test-backend, format-backend]
    name: Push release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3.0.0
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3.0.0
        # Workaround to fix error:
        # failed to push: failed to copy: io: read/write on closed pipe
        # See https://github.com/docker/build-push-action/issues/761
        with:
          driver-opts: |
            image=moby/buildkit:v0.10.6
      - name: Login to Docker Hub
        # Only push to Docker Hub when making a release
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: gtstef/filebrowser
      - name: Strip v from version number
        id: modify-json
        run: |
          JSON="${{ steps.meta.outputs.tags }}"
          # Use jq to remove 'v' from the version field
          JSON=$(echo "$JSON" | sed 's/filebrowser:v/filebrowser:/')
          echo "cleaned_tag=$JSON" >> $GITHUB_OUTPUT
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          file: ./Dockerfile
          push: true
          tags: ${{ steps.modify-json.outputs.cleaned_tag }}
          labels: ${{ steps.meta.outputs.labels }}