---
name: CI
on: [push]

jobs:
  release-please:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    outputs:
      release_created: ${{ steps.release-please.outputs.release_created }}
      tag_name: ${{ steps.release-please.outputs.tag_name }} #  e.g. v1.0.0
      version: ${{ steps.release-please.outputs.version }} # e.g. 1.0.0
      all: ${{ toJSON(steps.release-please.outputs) }}
    steps:
      - uses: google-github-actions/release-please-action@v3
        id: release-please
        with:
          command: manifest

  build:
    name: CI Image Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: docker/setup-buildx-action@v2
      - uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v4
        with:
          push: true
          tags: ghcr.io/postalserver/postal:ci-${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          target: ci

  test:
    name: Test Suite
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - run: docker-compose pull
        env:
          POSTAL_IMAGE: ghcr.io/postalserver/postal:ci-${{ github.sha }}
      - run: docker-compose run postal sh -c 'bundle exec rspec'
        env:
          POSTAL_IMAGE: ghcr.io/postalserver/postal:ci-${{ github.sha }}

  release-branch:
    name: Release (branch)
    runs-on: ubuntu-latest
    needs: [build]
    if: startsWith(github.ref, 'refs/heads/')
    steps:
      - uses: actions/checkout@v3
      - uses: docker/setup-buildx-action@v2
      - uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: tag
        run: |
          TAG="${GITHUB_REF#refs/heads/}"
          if [ -z "$TAG" ]; then exit 1; fi
          if [[ $TAG == "main" ]]; then TAG="latest"; else TAG="branch-${TAG}"; fi
          echo "tag=${TAG}" >> $GITHUB_OUTPUT
      - uses: docker/build-push-action@v4
        with:
          push: true
          tags: |
            ghcr.io/postalserver/postal:${{ steps.tag.outputs.tag }}
            ghcr.io/postalserver/postal:commit-${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          target: full

  publish-image:
    name: Publish Image
    runs-on: ubuntu-latest
    needs: [build, test, release-please]
    if: ${{ needs.release-please.outputs.release_created }}
    steps:
      - uses: actions/checkout@v3
      - uses: docker/setup-buildx-action@v2
      - uses: docker/login-action@v2
        with:
          registry: ${{ vars.KRYSTAL_REGISTRY_HOST }}
          username: ${{ vars.KRYSTAL_REGISTRY_USERNAME }}
          password: ${{ secrets.KRYSTAL_REGISTRY_PASSWORD }}
      - uses: docker/build-push-action@v4
        with:
          push: true
          tags: |
            ghcr.io/postalserver/postal:stable
            ghcr.io/postalserver/postal:${{ needs.release-please.outputs.version }}
          cache-from: type=local,src=/cache/krystal-identity
          cache-to: type=local,dest=/cache/krystal-identity,mode=max
          target: full
          build-args: |
            VERSION=${{ needs.release-please.outputs.version }}
