on:
  push:
    branches:
      - master

jobs:

  build:
    name: Build, push, and deploy
    runs-on: ubuntu-latest
    steps:

    - name: Checkout master
      uses: actions/checkout@v2

    - name: Build PostgreSQL container image
      run: | 
        docker build --build-arg DATABASE_TYPE=postgresql \
        --tag ghcr.io/$GITHUB_ACTOR/umami:postgresql-$(echo $GITHUB_SHA | head -c7) \
        --tag ghcr.io/$GITHUB_ACTOR/umami:postgresql-latest \
        .

    - name: Build MySQL container image
      run: |
        docker build --build-arg DATABASE_TYPE=mysql \
        --tag ghcr.io/$GITHUB_ACTOR/umami:mysql-$(echo $GITHUB_SHA | head -c7) \
        --tag ghcr.io/$GITHUB_ACTOR/umami:mysql-latest \
        .

    - name: Docker login
      env:
        CR_PAT: ${{ secrets.CR_PAT }}
      run: docker login -u $GITHUB_ACTOR -p $CR_PAT ghcr.io
      
    - name: Push image to GitHub
      run: |
        # Push each image individually, avoiding pushing to umami:latest
        # as MySQL or PostgreSQL are required
        docker push ghcr.io/$GITHUB_ACTOR/umami:postgresql-$(echo $GITHUB_SHA | head -c7)
        docker push ghcr.io/$GITHUB_ACTOR/umami:postgresql-latest
        docker push ghcr.io/$GITHUB_ACTOR/umami:mysql-$(echo $GITHUB_SHA | head -c7)
        docker push ghcr.io/$GITHUB_ACTOR/umami:mysql-latest