name: 'Build: Base Image'

on:
  push:
    branches:
      - master
    paths:
      - 'docker/images/n8n-base/Dockerfile'
      - '.github/workflows/build-base-image.yml'
  pull_request:
    paths:
      - 'docker/images/n8n-base/Dockerfile'
      - '.github/workflows/build-base-image.yml'
  workflow_dispatch:
    inputs:
      push:
        description: 'Push to registries'
        required: false
        default: false
        type: boolean

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # Node 26 requires alpine 3.23 on DHI (3.22 pairing isn't published).
        # While we're on alpine 3.22 awaiting NODE-4184 (graphicsmagick → sharp
        # migration that unblocks 3.23), Node 26 base builds can't succeed.
        # Restore '26' to this matrix once 3.23 is back.
        node_version: ['22', '24.15.0']
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Set up QEMU
        uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

      - name: Login to DHI Registry (for pulling base images)
        uses: ./.github/actions/docker-registry-login
        with:
          login-ghcr: 'false'
          login-dhi: 'true'
          dockerhub-username: ${{ secrets.DOCKER_USERNAME }}
          dockerhub-password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Login to Docker registries (for pushing)
        if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.push == true)
        uses: ./.github/actions/docker-registry-login
        with:
          login-ghcr: 'true'
          login-dockerhub: 'true'
          dockerhub-username: ${{ secrets.DOCKER_USERNAME }}
          dockerhub-password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and push
        uses: docker/build-push-action@d08e5c354a6adb9ed34480a06d141179aa583294 # v7.0.0
        with:
          context: .
          file: ./docker/images/n8n-base/Dockerfile
          build-args: |
            NODE_VERSION=${{ matrix.node_version }}
          platforms: linux/amd64,linux/arm64
          provenance: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.push == true) }}
          sbom: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.push == true) }}
          push: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.push == true) }}
          tags: |
            ${{ secrets.DOCKER_USERNAME }}/base:${{ matrix.node_version }}-${{ github.sha }}
            ${{ secrets.DOCKER_USERNAME }}/base:${{ matrix.node_version }}
            ghcr.io/${{ github.repository_owner }}/base:${{ matrix.node_version }}-${{ github.sha }}
            ghcr.io/${{ github.repository_owner }}/base:${{ matrix.node_version }}
          no-cache: true
