---
name: release
on:
  push:
    branches:
      - 'main'
    tags:
      - 'v*'
  pull_request:
    branches:
      - 'main'

env:
  DOCKER_HOST: tcp://docker-in-docker:2375

jobs:
  build:
    name: Build and push docker image
    runs-on: docker
    container:
      image: node:21-alpine
    steps:
      - name: Install docker
        shell: /bin/ash -e {0}
        run: apk add --update docker git bash
      - name: Check out the repo
        uses: https://github.com/actions/checkout@v4
      - name: Get Metadata For Docker
        id: meta
        uses: https://github.com/docker/metadata-action@v5
        with:
          images: forgejo.merr.is/${{ github.repository }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
          labels: |
            org.opencontainers.image.licenses=MIT
          annotations: |
            manifest:org.opencontainers.image.licenses=MIT
      - name: Set up Docker Buildx
        uses: https://github.com/docker/setup-buildx-action@v3
        with:
          driver-opts: |
            network=host
          config: .forgejo/build_configs/buildkitd.toml
      - name: Login to docker repo
        uses: docker/login-action@v3
        with:
          registry: "forgejo.merr.is"
          username: ${{ github.actor }}
          password: ${{ secrets.actions_token }}
      - name: Build and push the image
        uses: https://github.com/docker/build-push-action@v5
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      # - name: Extract Binary For Release
      #   uses: https://github.com/moosetheory/actions-docker-extract@v3.1
      #   id: extract
      #   if: startsWith(github.ref, 'refs/tags/')
      #   with:
      #     shell_command: /bin/ash -c
      #     image: ${{ fromJSON(steps.meta.outputs.json).tags[0] }}
      #     path: /usr/share/nginx/html
      #     destination: dist
      # - name: Create a Release
      #   uses: https://forgejo.merr.is/actions/forgejo-release@alpinev1
      #   if: startsWith(github.ref, 'refs/tags/')
      #   with:
      #     direction: upload
      #     release-dir: dist
      #     token: ${{ secrets.ADMIN_TOKEN }}

