name: build

on: workflow_call

jobs:

  docker-base:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[wip]')"
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 2
      - name: Check if base files changed
        id: check_changes
        run: |
          if [[ "${{ github.ref_name }}" == "dev" ]] && git diff --name-only HEAD~1 HEAD | grep -qE '(cleanup\.list|environment_base\.yml|Dockerfile\.base)$'; then
            echo "changed=true" >> $GITHUB_OUTPUT
          else
            echo "changed=false" >> $GITHUB_OUTPUT
          fi
      - name: Set up QEMU
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/setup-buildx-action@v3
      - name: Login to DockerHub
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/build-push-action@v6
        with:
          push: true
          file: Dockerfile.base
          tags: justold/immunopipe-base:latest

  docker-rpkgs:
    runs-on: ubuntu-latest
    needs: docker-base
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 2
      - name: Check if rpkgs files changed
        id: check_changes
        run: |
          if [[ "${{ github.ref_name }}" == "dev" ]] && git diff --name-only HEAD~1 HEAD | grep -qE '(cleanup\.list|environment_(base|rpkgs)\.yml|Dockerfile\.(base|rpkgs))$'; then
            echo "changed=true" >> $GITHUB_OUTPUT
          else
            echo "changed=false" >> $GITHUB_OUTPUT
          fi
      - name: Set up QEMU
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/setup-buildx-action@v3
      - name: Login to DockerHub
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        if: steps.check_changes.outputs.changed == 'true'
        uses: docker/build-push-action@v6
        with:
          push: true
          file: Dockerfile.rpkgs
          tags: justold/immunopipe-rpkgs:latest

  docker:
    runs-on: ubuntu-latest
    needs: docker-rpkgs
    steps:
      - name: Delete cache to save space
        # https://github.com/orgs/community/discussions/25678#discussioncomment-5242449
        run: rm -rf /opt/hostedtoolcache
      - uses: actions/checkout@v6
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          push: true
          pull: false
          # load: true
          file: Dockerfile
          tags: justold/immunopipe:${{ github.ref_name }}
