name: All tools CI

permissions:
  contents: read
  actions: read
  packages: write

on:
  push:
    branches:
      - main
    paths:
      - 'operations/'
      - '.github/workflows/all-tools.yml'
  pull_request:
    branches:
      - main
    paths:
      - 'operations/'
      - '.github/workflows/all-tools.yml'

jobs:
  changes:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    name: Filter commit changes
    outputs:
      all-tools: ${{ steps.filter.outputs['all-tools'] }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Filter commit changes
        uses: dorny/paths-filter@v3
        id: filter
        with:
          base: ${{ github.ref }}
          list-files: "json"
          filters: |
            all-tools:
              - 'operations/**'
              - '.github/workflows/all-tools.yml'
              - '.github/workflows/reuse-*.yml'

  store_image_name_and_tags:
    uses: ./.github/workflows/reuse-store-image-name-and-tags.yml

  check_image_tags_exist:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    name: Check image tags exist
    needs: [ changes, store_image_name_and_tags ]
    if: ${{ needs.changes.outputs['all-tools'] == 'false' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Check image tags exist
        uses: ./.github/actions/check-image-tags-exist
        with:
          last_commit_tag: ${{ needs.store_image_name_and_tags.outputs.last_commit_tag }}
          image_name: consensys/linea-alltools

  all-tools-tag-only:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    name: All tools tag only
    needs: [ changes, store_image_name_and_tags, check_image_tags_exist ]
    if: ${{ github.event_name != 'pull_request' && needs.changes.outputs['all-tools'] == 'false' }}
    outputs:
      image_tagged: ${{ steps.image_tag_push.outputs.image_tagged }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Tag and push image
        id: image_tag_push
        uses: ./.github/actions/image-tag-and-push
        with:
          commit_tag: ${{ needs.store_image_name_and_tags.outputs.commit_tag }}
          last_commit_tag: ${{ needs.store_image_name_and_tags.outputs.last_commit_tag }}
          develop_tag: ${{ needs.store_image_name_and_tags.outputs.develop_tag }}
          image_name: consensys/linea-alltools
          last_commit_tag_exists: ${{ needs.check_image_tags_exist.outputs.last_commit_tag_exists }}
          docker_username: ${{ secrets.DOCKERHUB_USERNAME }}
          docker_password: ${{ secrets.DOCKERHUB_TOKEN }}

  build-and-publish:
    needs: [ changes, store_image_name_and_tags, all-tools-tag-only ]
    if: ${{ always() && (needs.changes.outputs['all-tools'] == 'true' || needs.all-tools-tag-only.result != 'success' || needs.all-tools-tag-only.outputs.image_tagged != 'true') }}
    # ~0.5 mins saved vs small
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-med
    env:
      COMMIT_TAG: ${{ needs.store_image_name_and_tags.outputs.commit_tag }}
      DEVELOP_TAG: ${{ needs.store_image_name_and_tags.outputs.develop_tag }}
      IMAGE_NAME: consensys/linea-alltools
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
      DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
    name: All tools build and push
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Login to Docker Hub
        if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
        uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 #v3.4.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      # Note: Building arm* images on QEMU emulator can get extremely slow
      # when downloading large packages with npm / yarn. Arm* based docker
      # images are not necessarily needed as we run amd64 machines for most
      # cases. We can later set up self-hosted arm64 github runners if we
      # want arm* based images back.
      # - name: Set up QEMU
      #   uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 #v3.6.0
      #   with:
      #     platforms: 'arm64,arm'
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 #v3.11.1
      - name: Show the "version" build argument
        run: |
          echo "We inject the commit tag in the docker image ${{ env.COMMIT_TAG }}"
          echo COMMIT_TAG=${{ env.COMMIT_TAG }} >> GITHUB_ENV
      - name: Build and push all tools image
        uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 #v6.18.0
        with:
          context: .
          file: ./operations/Dockerfile
          platforms: linux/amd64  # Note: Build amd64 image only
          push: ${{ github.event_name != 'pull_request' }}
          tags: |
            ${{ env.IMAGE_NAME }}:${{ env.COMMIT_TAG }}
          cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
          cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max
