name: Build and Push Docker Image

on:
  push:
    tags:
      - 'v*'

env:
  REGISTRY: ghcr.io
  SERVER_IMAGE_NAME: neuronapp/neuron/server

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Get the version
        id: get_version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Build and push Neuron server image
        uses: docker/build-push-action@v5
        with:
          context: ./server
          file: ./server/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:latest
            ${{ env.REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }}
          labels: |
            org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
            org.opencontainers.image.revision=${{ github.sha }}