name: "Publish builder image"

on:
  workflow_dispatch:
  push:
    paths:
      - 'builder/**'

permissions:
  packages: write

jobs:
  publish_images:
    name: 'Build podman builder image'
    runs-on: ubuntu-latest
    env:
      IMAGETAG: ${{ github.ref_name }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true
      - id: build
        name: Build podman image
        run: |
          # Build the module images
          cd builder/
          bash build-builder
      - id: publish
        name: Publish podman image to GH registry
        run: |
          # Publish the branch
          trap 'buildah logout ghcr.io' EXIT
          buildah login -u ${{ github.actor }} --password-stdin ghcr.io <<<"${{ secrets.GITHUB_TOKEN }}"
          image='ghcr.io/nethserver/nethsecurity-builder'
          if [[ "${IMAGETAG}" == "main" ]]; then
              IMAGETAG=latest
          fi
          buildah push $image docker://${image}:${IMAGETAG:-latest}
          urls="${image}:${IMAGETAG:-latest}"$'\n'"${urls}"
          echo "::notice title=Image URLs::${urls}"