# This workflow will publish new Docker images to the GitHub Container Registry when a new release is published.
name: Publish Docker Images

on:
  release:
    types: [published]
  workflow_dispatch:

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

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Convert repository name to lowercase
        run: |
          echo "REPO_LOWER=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}

      - name: Build and push API image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: src/AliasVault.Api/Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-api:latest,${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-api:${{ github.ref_name }}

      - name: Build and push Client image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: src/AliasVault.Client/Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-client:latest,${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-client:${{ github.ref_name }}

      - name: Build and push Admin image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: src/AliasVault.Admin/Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-admin:latest,${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-admin:${{ github.ref_name }}

      - name: Build and push SMTP image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: src/Services/AliasVault.SmtpService/Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-smtp:latest,${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-smtp:${{ github.ref_name }}

      - name: Build and push Reverse Proxy image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-reverse-proxy:latest,${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-reverse-proxy:${{ github.ref_name }}

      - name: Build and push InstallCli image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: src/Utilities/AliasVault.InstallCli/Dockerfile
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-installcli:latest,${{ env.REGISTRY }}/${{ env.REPO_LOWER }}-installcli:${{ github.ref_name }}
