name: Docker Image CI

on:
  push:
    branches: [ "dev" ]
  pull_request:
    branches: [ "dev" ]

jobs:

  build_n_push:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Extract branch name
      shell: bash
      run: echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >>$GITHUB_OUTPUT
      id: extract_branch
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1
      with:
        install: true
    
    - name: Docker Build and Tag Prod
      run: docker build --load -t saile2204/ionos_ddns_updater:latest-${{ steps.extract_branch.outputs.branch }} -t saile2204/ionos_ddns_updater:${{ steps.extract_branch.outputs.branch }}-$GITHUB_SHA .
    
    - name: Docker Build and Tagd
      run: docker build --load -t saile2204/ionos_ddns_updater:$(date +%s) .
    
    - name: Log in to Docker Hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKERUSERNAME }}
        password: ${{ secrets.DOCKERPASSWORD }}
        
    - name: Push to Dockerhub
      run: |
          docker push saile2204/ionos_ddns_updater:latest-${{ steps.extract_branch.outputs.branch }}
          docker push saile2204/ionos_ddns_updater:${{ steps.extract_branch.outputs.branch }}-$GITHUB_SHA
