name: Build & Push
# Build & Push builds the simapp docker image on every push to main and
# and pushes the image to https://ghcr.io/cosmos/simapp
on:
  pull_request:
    paths:
      - "Dockerfile"
  push:
    branches:
      - main
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+" # Push events to matching v*, i.e. v1.0.0, v20.15.10
      - "v[0-9]+.[0-9]+.[0-9]+-rc.*" # Push events to matching v*, i.e. v1.0.0-rc.1, v20.15.10-rc.5
      - "v[0-9]+.[0-9]+.[0-9]+-beta.*" # Push events to matching v*, i.e. v1.0.0-beta.1, v20.15.10-beta.5
  workflow_dispatch:
    inputs:
      tags:
        description: "SDK version (e.g 0.47.1)"
        required: true
        type: string

permissions:
  contents: read
  packages: write
  id-token: write

env:
  # Use docker.io for Docker Hub if empty
  REGISTRY: ghcr.io
  # github.repository as <account>/<repo>
  IMAGE_NAME: cosmos/simapp

jobs:
  build:
    runs-on: depot-ubuntu-22.04-4
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=semver,pattern=v{{major}}.{{minor}}
            type=semver,pattern={{version}},value=v${{ inputs.tags }},enable=${{ inputs.tags != '' }}
          flavor: |
            latest=false

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

      - name: Log into registry ${{ env.REGISTRY }}
        if: ${{ github.event_name != 'pull_request' }}
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Depot
        uses: depot/setup-action@v1

      - name: Publish to GitHub Packages
        uses: depot/build-push-action@v1
        with:
          project: gnm1jqptpw
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
