name: 'Docker Build Smoke Test'

# Verifies the full Docker build chain works without any caching.
# Catches native module compilation failures (e.g., isolated-vm, sqlite3)
# that layer caching can mask in the regular E2E pipeline.
#
# Full chain: pnpm install → pnpm build (no Turbo cache) →
#             build base image (no Docker cache) →
#             build n8n + runners images (no Docker cache)

on:
  schedule:
    - cron: '0 3 * * *' # 3:00 AM UTC, after the nightly Docker build at midnight
  pull_request:
    paths:
      - 'docker/images/n8n/**'
      - 'docker/images/n8n-base/**'
      - 'docker/images/runners/**'
      - 'scripts/build-n8n.mjs'
      - 'scripts/dockerize-n8n.mjs'
      - 'scripts/smoke-n8n-image.mjs'
  workflow_dispatch:

concurrency:
  group: docker-smoke-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  docker-smoke-test:
    name: 'Docker Build (no cache)'
    runs-on: blacksmith-4vcpu-ubuntu-2204
    if: ${{ !github.event.pull_request.head.repo.fork }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

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

      - name: Build full chain (no cache)
        uses: ./.github/actions/setup-nodejs
        with:
          build-command: 'pnpm build:docker:clean'
          enable-docker-cache: true

      - uses: azure/setup-helm@b9e51907a09c216f16ebe8536097933489208112 # v4.3.0

      - id: cloud-token
        uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
        with:
          app-id: ${{ secrets.N8N_ASSISTANT_APP_ID }}
          private-key: ${{ secrets.N8N_ASSISTANT_PRIVATE_KEY }}
          owner: n8n-io
          repositories: n8n-cloud

      - name: Verify image against live n8n-cloud helm chart
        env:
          GH_TOKEN: ${{ steps.cloud-token.outputs.token }}
        run: pnpm build:docker:smoke

  notify-on-failure:
    name: Notify on nightly smoke test failure
    runs-on: ubuntu-slim
    needs: [docker-smoke-test]
    if: needs.docker-smoke-test.result == 'failure' && github.event_name == 'schedule'
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          sparse-checkout: .github/scripts/slack
          sparse-checkout-cone-mode: false
      - name: Notify Slack
        env:
          SLACK_TOKEN: ${{ secrets.QBOT_SLACK_TOKEN }}
        run: |
          node .github/scripts/slack/notify.mjs \
            --channel C0A9RLY8Y20 \
            --text '🚨 Nightly Docker smoke test failed (no-cache build) - ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
