name: Docker Build & Push Simapp (main)
# Build & Push builds the simapp docker image on every push to main and
# and pushes the image to https://ghcr.io/cosmos/ibc-go-simd
on:
  workflow_dispatch:
  push:
    branches:
      - main
      - release/v*
    paths:
      - '.github/workflows/docker.yml'
      - '**.go'

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ibc-go-simd

jobs:
  docker-build:
    runs-on: depot-ubuntu-22.04-4
    permissions:
      packages: write
      contents: read
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Extract metadata (tags, labels) for Docker
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/cosmos/${{ env.IMAGE_NAME }}

    - name: Compute release branch tag
      id: reltag
      if: startsWith(github.ref_name, 'release/v')
      shell: bash
      run: |
        RAW="${GITHUB_REF_NAME}"
        SANITIZED="${RAW//\//-}"
        echo "full_tag=${{ env.REGISTRY }}/cosmos/${{ env.IMAGE_NAME }}:branch-${SANITIZED}" >> "$GITHUB_OUTPUT"

    - name: Build Docker image
      uses: docker/build-push-action@v6
      with:
        context: .
        tags: ${{ steps.meta.outputs.tags }}
        build-args: |
          IBC_GO_VERSION=${{ github.ref_name }}

    - name: Test simd is runnable
      run: |
        docker run --rm ${{ steps.meta.outputs.tags }}
    - name: Log in to the Container registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Push Docker image
      uses: docker/build-push-action@v6
      with:
        context: .
        push: true
        tags: |
          ${{ steps.meta.outputs.tags }}
          ${{ steps.reltag.outputs.full_tag }}
        build-args: |
          IBC_GO_VERSION=${{ github.ref_name }}
