name: Push Test Images to Docker Hub and GitHub Container Registry

concurrency:
  group: test-release
  cancel-in-progress: true

on:
  push:
    branches:
      - "master"
jobs:
  generate-build-number:
    runs-on: ubuntu-latest
    outputs:
      build_number: ${{ steps.buildnumber.outputs.build_number }}
    steps:
      - name: Generate build number
        id: buildnumber
        uses: onyxmueller/build-tag-number@v1.0.2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      - run: echo "Build number is ${{ steps.buildnumber.outputs.build_number }}"

  read-version:
    runs-on: ubuntu-latest
    outputs:
      major_minor: ${{ steps.determine.outputs.semver_base }}
      semver_base: ${{ steps.determine.outputs.semver_base }}
      major: ${{ steps.determine.outputs.major }}
      minor: ${{ steps.determine.outputs.minor }}
      patch: ${{ steps.determine.outputs.patch }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}
      - name: Determine semver base
        id: determine
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPOSITORY: ${{ github.repository }}
        run: |
          set -euo pipefail

          VERSION_RAW="$(tr -d ' \n' < VERSION)"
          if [[ -z "$VERSION_RAW" ]]; then
            echo "VERSION is empty" >&2
            exit 1
          fi

          IFS='.' read -r major minor patch <<< "$VERSION_RAW"
          if [[ -z "$minor" ]]; then
            echo "VERSION must contain major and minor components" >&2
            exit 1
          fi
          patch="${patch:-0}"

          for part_name in major minor patch; do
            part="${!part_name}"
            if ! [[ "$part" =~ ^[0-9]+$ ]]; then
              echo "Invalid ${part_name} component '$part' in VERSION" >&2
              exit 1
            fi
          done

          target_patch="$patch"
          latest_tag="$(gh release view --repo "$REPOSITORY" --json tagName --jq '.tagName' 2>/dev/null || echo "")"

          if [[ -n "$latest_tag" ]]; then
            latest_tag="${latest_tag#v}"
            latest_tag_core="${latest_tag%%+*}"
            latest_tag_core="${latest_tag_core%%-*}"
            IFS='.' read -r rel_major rel_minor rel_patch _ <<< "$latest_tag_core"
            rel_patch="${rel_patch:-0}"
            if [[ "$rel_major" =~ ^[0-9]+$ && "$rel_minor" =~ ^[0-9]+$ && "$rel_patch" =~ ^[0-9]+$ ]]; then
              if [[ "$rel_major" == "$major" && "$rel_minor" == "$minor" ]]; then
                target_patch=$((rel_patch + 1))
              fi
            fi
          fi

          new_version="${major}.${minor}.${target_patch}"

          echo "semver_base=${new_version}" >> "$GITHUB_OUTPUT"
          echo "major=${major}" >> "$GITHUB_OUTPUT"
          echo "minor=${minor}" >> "$GITHUB_OUTPUT"
          echo "patch=${target_patch}" >> "$GITHUB_OUTPUT"
          echo "Using version base: ${new_version}"

  publish-mcp-server:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    env:
      CI_PIPELINE_ID: ${{ github.run_number }}
      NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
    permissions:
      contents: write  # For creating releases
      packages: write  # For publishing packages
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Full history for changelog generation

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: latest
          cache: 'npm'
          registry-url: 'https://registry.npmjs.org'

      - name: Install Common dependencies
        run: cd Common && npm install

      - name: Install root dependencies
        run: npm install

      - name: Install Script dependencies
        run: cd Scripts && npm install

      - name: Determine version
        id: version
        run: |
          VERSION="${{needs.read-version.outputs.major_minor}}-test"
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Publishing MCP server version: $VERSION"

      - name: Verify MCP server directory
        run: |
          MCP_DIR="./MCP"
          
          if [ ! -d "$MCP_DIR" ]; then
            echo "❌ MCP server directory not found"
            exit 1
          fi
          
          echo "✅ MCP server directory found"
          echo "📊 Source files:"
          find "$MCP_DIR" -type f -name "*.ts" -o -name "*.js" -o -name "*.json" | wc -l
          echo "📁 Directory structure:"
          ls -la "$MCP_DIR"

      - name: Update package version
        run: |
          cd MCP
          npm version ${{ steps.version.outputs.version }} --no-git-tag-version

      - name: Install dependencies and build
        run: |
          cd MCP
          npm update @oneuptime/common
          npm install
          npm run build

      - name: Run tests
        run: |
          cd MCP
          npm test || echo "No tests found or tests failed, continuing..."

      - name: Publish to npm (dry run for test releases)
        run: |
          cd MCP
          npm pack --dry-run
          echo "✅ Dry run completed successfully for test release"

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

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push Docker images (test)
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image mcp-server \
              --version "${{ steps.version.outputs.version }}" \
              --dockerfile ./MCP/Dockerfile.tpl \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test
            echo "✅ Pushed test Docker images to Docker Hub and GitHub Container Registry"

      - name: Upload MCP server artifact
        uses: actions/upload-artifact@v4
        with:
          name: mcp-server-${{ steps.version.outputs.version }}
          path: ./MCP/
          retention-days: 90

  

  llm-docker-image-deploy: 
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:

      # Docker compose needs a lot of space to build images, so we need to free up some space first in the GitHub Actions runner
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@main
        with:
          # this might remove tools that are actually needed,
          # if set to "true" but frees about 6 GB
          tool-cache: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true

      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/llm
            ghcr.io/oneuptime/llm
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest
      
      # - name: Setup Git LFS
      #   run: git lfs install
        
      # # Cannot do this, no space on the gitHub standard runner. We need to use the large runner which is selfhosted
      # - name: Download the Model from Hugging Face
      #   run: mkdir -p ./LLM/Models && cd ./LLM/Models && git clone https://${{ secrets.HUGGING_FACE_USERNAME }}:${{ secrets.HUGGING_FACE_PASSWORD }}@huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy nginx.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image llm \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./LLM/Dockerfile \
              --context ./LLM \
              --platforms linux/amd64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test


  nginx-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/nginx
            ghcr.io/oneuptime/nginx
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy nginx.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image nginx \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Nginx/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test


  e2e-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/e2e
            ghcr.io/oneuptime/e2e
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy e2e.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image e2e \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./E2E/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  test-server-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/test-server
            ghcr.io/oneuptime/test-server
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy test-server.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image test-server \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./TestServer/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  otel-collector-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/otel-collector
            ghcr.io/oneuptime/otel-collector
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy otel-collector.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image otel-collector \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./OTelCollector/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  isolated-vm-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/isolated-vm
            ghcr.io/oneuptime/isolated-vm
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy isolated-vm.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image isolated-vm \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./IsolatedVM/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  home-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/home
            ghcr.io/oneuptime/home
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy isolated-vm.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image home \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Home/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  
  
  status-page-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/status-page
            ghcr.io/oneuptime/status-page
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy status-page.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image status-page \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./StatusPage/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test



  test-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/test
            ghcr.io/oneuptime/test
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy test.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image test \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Tests/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  probe-ingest-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/probe-ingest
            ghcr.io/oneuptime/probe-ingest
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy probe-ingest.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image probe-ingest \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./ProbeIngest/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test



  server-monitor-ingest-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/server-monitor-ingest
            ghcr.io/oneuptime/server-monitor-ingest
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy ServerMonitorIngest.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image server-monitor-ingest \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./ServerMonitorIngest/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test
          
            


  incoming-request-ingest-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/incoming-request-ingest
            ghcr.io/oneuptime/incoming-request-ingest
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy incoming-request-ingest.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image incoming-request-ingest \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./IncomingRequestIngest/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  telemetry-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/telemetry
            ghcr.io/oneuptime/telemetry
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy incoming-request-ingest.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image telemetry \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Telemetry/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  probe-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/probe
            ghcr.io/oneuptime/probe
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy probe.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image probe \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Probe/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  dashboard-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/dashboard
            ghcr.io/oneuptime/dashboard
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy dashboard.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image dashboard \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Dashboard/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  admin-dashboard-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/admin-dashboard
            ghcr.io/oneuptime/admin-dashboard
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy admin-dashboard.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image admin-dashboard \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./AdminDashboard/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  app-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/app
            ghcr.io/oneuptime/app
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy app.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image app \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./App/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  
  
  
  api-reference-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/api-reference
            ghcr.io/oneuptime/api-reference
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy app.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image api-reference \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./APIReference/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  

  accounts-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/accounts
            ghcr.io/oneuptime/accounts
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy accounts.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image accounts \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Accounts/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  worker-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/worker
            ghcr.io/oneuptime/worker
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy accounts.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image worker \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Worker/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  copilot-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/copilot
            ghcr.io/oneuptime/copilot
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy accounts.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image copilot \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Copilot/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

  
  workflow-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/workflow
            ghcr.io/oneuptime/workflow
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy accounts.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image workflow \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Workflow/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test

    
  docs-docker-image-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - name: Docker Meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            oneuptime/docs
            ghcr.io/oneuptime/docs
          tags: |
            type=raw,value=test,enable=true
            type=raw,value=${{needs.read-version.outputs.major_minor}}-test,enable=true
            

      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: latest

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:qemu-v10.0.4
          

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

      - name: Generate Dockerfile from Dockerfile.tpl
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: npm run prerun

      # Build and deploy accounts.

      - name: Login to Docker Hub
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login --username "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin

      - name: Login to GitHub Container Registry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 3
          command: |
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username "${{ github.repository_owner }}" --password-stdin

      - name: Build and push
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 30
          max_attempts: 3
          command: |
            bash ./Scripts/GHA/build_docker_images.sh \
              --image docs \
              --version "${{needs.read-version.outputs.major_minor}}-test" \
              --dockerfile ./Docs/Dockerfile \
              --context . \
              --platforms linux/amd64,linux/arm64 \
              --git-sha "${{ github.sha }}" \
              --extra-tags test \
              --extra-enterprise-tags enterprise-test
  
  publish-terraform-provider:
    runs-on: ubuntu-latest
    needs: [read-version, generate-build-number]
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}
      - name: Skip Terraform provider publish for test release
        run: |
          VERSION="${{needs.read-version.outputs.major_minor}}-test"
          echo "Skipping Terraform provider publish for test release $VERSION"

                             

  test-helm-chart:
    runs-on: ubuntu-latest
    needs: [infrastructure-agent-deploy, publish-mcp-server, llm-docker-image-deploy, publish-terraform-provider, telemetry-docker-image-deploy, copilot-docker-image-deploy, docs-docker-image-deploy, worker-docker-image-deploy,  workflow-docker-image-deploy, isolated-vm-docker-image-deploy, home-docker-image-deploy, api-reference-docker-image-deploy,  test-server-docker-image-deploy, test-docker-image-deploy, probe-ingest-docker-image-deploy, server-monitor-ingest-docker-image-deploy, probe-docker-image-deploy, dashboard-docker-image-deploy, admin-dashboard-docker-image-deploy, app-docker-image-deploy, accounts-docker-image-deploy, otel-collector-docker-image-deploy, status-page-docker-image-deploy, nginx-docker-image-deploy, e2e-docker-image-deploy, incoming-request-ingest-docker-image-deploy]
    env:
      CI_PIPELINE_ID: ${{github.run_number}}
    steps:
      # Docker compose needs a lot of space to build images, so we need to free up some space first in the GitHub Actions runner
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@main
        with:
          # this might remove tools that are actually needed,
          # if set to "true" but frees about 6 GB
          tool-cache: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: latest
      - run: cd HelmChart && cd Tests && bash index.sh

  test-e2e-test-saas: 
    runs-on: ubuntu-latest
    needs: [test-helm-chart, generate-build-number, read-version]
    env:
      CI_PIPELINE_ID: ${{github.run_number}}
    steps:
      # Docker compose needs a lot of space to build images, so we need to free up some space first in the GitHub Actions runner
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@main
        with:
          # this might remove tools that are actually needed,
          # if set to "true" but frees about 6 GB
          tool-cache: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: latest
      - name: Preinstall
        run: |
          set -euo pipefail
          npm run prerun
      - name: Pin APP_TAG to test release
        run: |
          set -euo pipefail
          VERSION="${{needs.read-version.outputs.major_minor}}-test"
          SANITIZED_VERSION="${VERSION//+/-}"
          if [ -f config.env ]; then
            if grep -q '^APP_TAG=' config.env; then
              sed -i "s/^APP_TAG=.*/APP_TAG=${SANITIZED_VERSION}/" config.env
            else
              echo "APP_TAG=${SANITIZED_VERSION}" >> config.env
            fi
          else
            echo "APP_TAG=${SANITIZED_VERSION}" > config.env
          fi
      - name: Start Server with release tag
        run: |
          set -euo pipefail
          export $(grep -v '^#' config.env | xargs)
          export APP_TAG=${{needs.read-version.outputs.major_minor}}-test
          npm run start
      - name: Wait for server to start
        run: bash ./Tests/Scripts/status-check.sh http://localhost
      - name: Run E2E Tests. Run docker container e2e in docker compose file
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 90
          max_attempts: 3
          on_retry_command: docker compose -f docker-compose.dev.yml down -v || true
          command: |
            export $(grep -v '^#' config.env | xargs)
            export APP_TAG=${{needs.read-version.outputs.major_minor}}-test
            docker compose -f docker-compose.dev.yml up --exit-code-from e2e --abort-on-container-exit e2e || (docker compose -f docker-compose.dev.yml logs e2e && exit 1)
      - name: Upload test results
        uses: actions/upload-artifact@v4
        # Run this on failure
        if: failure()
        with:
          # Name of the artifact to upload.
          # Optional. Default is 'artifact'
          name: test-results-${{ github.job }}-${{ github.run_attempt }}
      
          # A file, directory or wildcard pattern that describes what to upload
          # Required.
          path: |
            ./E2E
      
        
          # Duration after which artifact will expire in days. 0 means using default retention.
          # Minimum 1 day.
          # Maximum 90 days unless changed from the repository settings page.
          # Optional. Defaults to repository settings.
          retention-days: 7
    
    
  test-e2e-test-self-hosted: 
    runs-on: ubuntu-latest
    # After all the jobs runs
    needs: [test-helm-chart, generate-build-number, read-version]
    env:
      CI_PIPELINE_ID: ${{github.run_number}}
    steps:
      # Docker compose needs a lot of space to build images, so we need to free up some space first in the GitHub Actions runner
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@main
        with:
          # this might remove tools that are actually needed,
          # if set to "true" but frees about 6 GB
          tool-cache: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: latest
      - name: Preinstall
        run: |
          set -euo pipefail
          npm run prerun
      - name: Pin APP_TAG to test release
        run: |
          set -euo pipefail
          VERSION="${{needs.read-version.outputs.major_minor}}-test"
          SANITIZED_VERSION="${VERSION//+/-}"
          if [ -f config.env ]; then
            if grep -q '^APP_TAG=' config.env; then
              sed -i "s/^APP_TAG=.*/APP_TAG=${SANITIZED_VERSION}/" config.env
            else
              echo "APP_TAG=${SANITIZED_VERSION}" >> config.env
            fi
          else
            echo "APP_TAG=${SANITIZED_VERSION}" > config.env
          fi
      - name: Start Server with release tag
        run: |
          set -euo pipefail
          export $(grep -v '^#' config.env | xargs)
          export APP_TAG=${{needs.read-version.outputs.major_minor}}-test
          npm run start
      - name: Wait for server to start
        run: bash ./Tests/Scripts/status-check.sh http://localhost
      - name: Run E2E Tests. Run docker container e2e in docker compose file
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 90
          max_attempts: 3
          on_retry_command: docker compose -f docker-compose.dev.yml down -v || true
          command: |
            export $(grep -v '^#' config.env | xargs)
            export APP_TAG=${{needs.read-version.outputs.major_minor}}-test
            docker compose -f docker-compose.dev.yml up --exit-code-from e2e --abort-on-container-exit e2e || (docker compose -f docker-compose.dev.yml logs e2e && exit 1)
      - name: Upload test results
        uses: actions/upload-artifact@v4
        # Run this on failure
        if: failure()
        with:
          # Name of the artifact to upload.
          # Optional. Default is 'artifact'
          name: test-results-${{ github.job }}-${{ github.run_attempt }}
      
          # A file, directory or wildcard pattern that describes what to upload
          # Required.
          path: |
            ./E2E
      
        
          # Duration after which artifact will expire in days. 0 means using default retention.
          # Minimum 1 day.
          # Maximum 90 days unless changed from the repository settings page.
          # Optional. Defaults to repository settings.
          retention-days: 7

  infrastructure-agent-deploy:
    needs: [read-version, generate-build-number]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}
          
      - name: Set up Go
        uses: actions/setup-go@v4

      - name: Install GoReleaser
        uses: goreleaser/goreleaser-action@v6.1.0
        with:
          install-only: true

      - name: GoReleaser Version
        run: goreleaser -v

      # This tool is used to generate .rpm and .deb packages
      - name: Install NFPM
        run: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest

      - name: Show GoReleaser version
        run: goreleaser -v

      - name: Run GoReleaser
        run: cd InfrastructureAgent && export GORELEASER_CURRENT_TAG=${{needs.read-version.outputs.major_minor}}.${{needs.generate-build-number.outputs.build_number}} && goreleaser release --clean --snapshot

      - name: Release MSI Images
        run: cd InfrastructureAgent && bash build-msi.sh ${{needs.read-version.outputs.major_minor}}.${{needs.generate-build-number.outputs.build_number}}
      

      - name: Upload Release Binaries
        uses: actions/upload-artifact@v4
        # Run this on failure
        with:
          # Name of the artifact to upload.
          # Optional. Default is 'artifact'
          name: binaries
      
          # A file, directory or wildcard pattern that describes what to upload
          # Required.
          path: |
            ./InfrastructureAgent/dist
      
        
          # Duration after which artifact will expire in days. 0 means using default retention.
          # Minimum 1 day.
          # Maximum 90 days unless changed from the repository settings page.
          # Optional. Defaults to repository settings.
          retention-days: 7
