name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  build-and-push:
    runs-on: ubuntu 24

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

    - name: Log in to Docker Hub
      run: echo '${{ secrets.DOCKER_HUB_PASS }}' | docker login -u djangohelpdesk --password-stdin

    - name: Build Docker image
      run: docker build --file standalone/Dockerfile -t djangohelpdesk/standalone:latest ..

    - name: Build extras Docker image
      run: docker build --file standalone/Dockerfile.extras -t djangohelpdesk/standalone-extras:latest ..

    - name: Push Docker image
      run: docker push djangohelpdesk/standalone:latest

    - name: Push extras Docker image
      run: docker push djangohelpdesk/standalone-extras:latest

    - name: Tag and push Docker image with year, month and Git SHA
      run: docker tag djangohelpdesk/standalone:latest djangohelpdesk/standalone:$(date +%Y-%m)-$(git rev-parse --short HEAD) ; docker push djangohelpdesk/standalone:$(date +%Y-%m)-$(git rev-parse --short HEAD)

    - name: Tag and push extras Docker image with year, month and Git SHA
      run: docker tag djangohelpdesk/standalone-extras:latest djangohelpdesk/standalone-extras:$(date +%Y-%m)-$(git rev-parse --short HEAD) ; docker push djangohelpdesk/standalone-extras:$(date +%Y-%m)-$(git rev-parse --short HEAD)

    - name: If we are at a tag, add a tag to the current docker image push the image with the tag
      if: startsWith(github.ref, 'refs/tags/')
      run: docker tag djangohelpdesk/standalone:latest djangohelpdesk/standalone:${{ github.ref }} ; docker push djangohelpdesk/standalone:${{ github.ref }}
