##### --------------------------------------------------------------------------- #####
##### This workflow looks for new dockefiles and builds them to the test target.  #####
##### This in intended to start on a pull request (PR)                            #####
##### Ana06 is being used instead of jitterbit because of the filter option       #####
##### --------------------------------------------------------------------------- #####

name: Test New Dockerfiles

on: pull_request

jobs:

  find_new_dockerfiles:
    runs-on: ubuntu-latest
    outputs:
      json: ${{ steps.files.outputs.added_modified }}
    steps:
      - uses: actions/checkout@v4
      - id: files
        uses: Ana06/get-changed-files@v2.3.0
        with:
          format: 'json'
          filter: 'Dockerfile' 
      - run: "echo ${{ steps.files.outputs.all }}"
  
  build_to_test:
    needs: find_new_dockerfiles
    if: needs.find_new_dockerfiles.outputs.json != '[]'
    runs-on: ubuntu-latest
    strategy:
      matrix:
        added_modified: ${{ fromJson(needs.find_new_dockerfiles.outputs.json) }}
    steps:
      - name: parse file path
        id: parse
        run: |
          tool=$(echo    "${{ matrix.added_modified }}" | cut -f 1 -d "/" )
          version=$(echo "${{ matrix.added_modified }}" | cut -f 2 -d "/" )
          echo "tool=$tool"       >> $GITHUB_OUTPUT
          echo "version=$version" >> $GITHUB_OUTPUT
   
##### --------------------------------------------------------------------------- #####
##### Workflows still cannot be run in parrallel as of 2022-12-01                 #####
##### --------------------------------------------------------------------------- #####

#    - name: test files
#      uses: ./.github/workflows/build-to-test.yml
#      with:
#        path_to_context: "./${{ steps.parse.outputs.tool }}/${{ steps.parse.outputs.version }}"
#        cache: ${{ steps.parse.outputs.tool }}

##### --------------------------------------------------------------------------- #####
##### The steps of ./.github/workflows/build-to-test.yml are copied here.         #####
##### --------------------------------------------------------------------------- ##### 

      - name: Checkout
        uses: actions/checkout@v4
        
      - name: Layer check
        run: |
          #checking layers
          warning=''
          app_layer=$(grep FROM ./${{ steps.parse.outputs.tool }}/${{ steps.parse.outputs.version }}/Dockerfile | grep -E "as app|AS app")
          tst_layer=$(grep FROM ./${{ steps.parse.outputs.tool }}/${{ steps.parse.outputs.version }}/Dockerfile | grep -E "as test|AS test")
          if [ -z "$app_layer" ] ; then echo "FATAL : app layer is missing" ; warning='warning' ; fi
          if [ -z "$tst_layer" ] ; then echo "FATAL : test layer is missing" ; warning='warning' ; fi
          if [ -n "$warning" ] ; then echo "Please see template for recommended format https://github.com/StaPH-B/docker-builds/blob/master/dockerfile-template/Dockerfile" ; exit 1 ; fi
      
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v3

      - name: Cache Docker layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache-${{ steps.parse.outputs.tool }}
          key: ${{ runner.os }}-buildx-${{ steps.parse.outputs.tool }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-${{ steps.parse.outputs.tool }}
      
      - name: Build to test
        id: docker_build_to_test
        uses: docker/build-push-action@v5
        with:
          context: ./${{ steps.parse.outputs.tool }}/${{ steps.parse.outputs.version }}
          file: ./${{ steps.parse.outputs.tool }}/${{ steps.parse.outputs.version }}/Dockerfile
          target: test
          tags: ${{ steps.parse.outputs.tool }}:${{ steps.parse.outputs.version }}
          load: true
          push: false
          cache-from: type=local,src=/tmp/.buildx-cache-${{ steps.parse.outputs.tool }}
          cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-${{ steps.parse.outputs.tool }}-new

      - name: Check labels
        run: |
          # checking labels
          warning=''
          for label in base.image dockerfile.version software software.version description website maintainer maintainer.email
          do
            value=$(docker inspect --format '{{ index .Config.Labels "'$label'"}}' ${{ steps.parse.outputs.tool }}:${{ steps.parse.outputs.version }} )
            if [ -z "$value" ] ; then warning='warning' ; echo "FATAL : $label label not found in ${{ steps.parse.outputs.tool }}:${{ steps.parse.outputs.version }}" ; fi
          done
          
          if [ -z "$(docker inspect --format '{{.Config.WorkingDir}}' ${{ steps.parse.outputs.tool }}:${{ steps.parse.outputs.version }} )" ] ;
          then 
            warning='warning'
            echo "FATAL : WORKDIR not set."
          fi
          
          if [ -n "$warning" ] ; then echo "Please see template for recommended format https://github.com/StaPH-B/docker-builds/blob/master/dockerfile-template/Dockerfile" ; exit 1 ; fi 

      - name: Check commonly overlooked commands
        run: |
          # checking commands
          docker run ${{ steps.parse.outputs.tool }}:${{ steps.parse.outputs.version }} ps --help

      - name: Move cache # apparently prevents the cache from growing in size forever
        run: |
          rm -rf /tmp/.buildx-cache-${{ steps.parse.outputs.tool }}
          mv /tmp/.buildx-cache-${{ steps.parse.outputs.tool }}-new /tmp/.buildx-cache-${{ steps.parse.outputs.tool }}
      
      - name: Image digest
        run: echo ${{ steps.docker_build_to_test.outputs.digest }}