name: Publish VM Image

on:
  push:
    branches: [master, develop, feature/*]

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

env:
  GO_VERSION: 1.22
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

permissions:
  contents: read
  packages: write

jobs:
  release-image:
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

      - run: |
          DOCKER_REGISTRY=`echo "${{ env.REGISTRY }}/${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]'`
          echo "DOCKER_REGISTRY=$DOCKER_REGISTRY" >> $GITHUB_ENV

          REPO_NAME=`echo "${{ github.repository }}" | awk -F'/' '{print $2}' | tr '[:upper:]' '[:lower:]'`
          echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
      # login to ghcr (public) with codebase remaining private
      - name: Log in to the Container registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push sonrvm image
        uses: docker/build-push-action@v6
        with:
          push: true
          target: sonrvm
          tags: ghcr.io/${{ github.repository_owner }}/sonrvm:latest

      - name: Build and push sonr-runner image
        uses: docker/build-push-action@v6
        with:
          push: true
          target: sonr-runner
          tags: ghcr.io/${{ github.repository_owner }}/sonr-runner:latest
