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

on:
  push:
    branches:
      - "release"

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 }}"




  helm-chart-deploy:
    runs-on: ubuntu-latest
    needs: [generate-build-number]
    env: 
        CI_COMMIT_AUTHOR: Continuous Integration
    steps:

      - name: Install Helm 
        run: curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

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

      - name:  Build and Package Helm chart
        run: |
          cd ..
          echo '${{ secrets.GPG_PRIVATE_KEY }}' > private.key
          gpg --import private.key || true
          rm private.key
          echo "GPG key imported successfully"
          gpg --export-secret-keys >~/.gnupg/secring.gpg 
          echo "GPG key exported successfully"
          eval `ssh-agent -s`
          ssh-add - <<< '${{ secrets.HELM_CHART_GITHUB_REPO_DEPLOY_KEY }}'
          git clone git@github.com:OneUptime/helm-chart.git
          cd oneuptime/HelmChart/Public
          helm lint oneuptime
          helm package --sign --key 'key@oneuptime.com' --keyring ~/.gnupg/secring.gpg oneuptime --version 7.0.${{needs.generate-build-number.outputs.build_number}} --app-version 7.0.${{needs.generate-build-number.outputs.build_number}} 
          echo "Helm Chart Package created successfully"
          cd ..
          ls
          echo "Copying the package to helm-chart repo"
          rm -r ../../helm-chart/oneuptime
          cp -r ./Public/* ../../helm-chart
          echo "Package copied successfully"
          cd .. && cd .. && cd helm-chart
          echo "Updating helm-chart repo"
          git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}"
          git config --global user.email "hello@oneuptime.com"
          echo "Git config set successfully"
          echo "Adding the package to helm-chart repo"
          helm repo index .
          git add -A
          git commit -m "Helm Chart Release 7.0.${{needs.generate-build-number.outputs.build_number}}"
          git push origin master

  nginx-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy nginx.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Nginx/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  e2e-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy e2e.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./E2E/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  isolated-vm-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy isolated-vm.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./IsolatedVM/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  test-server-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy test-server.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./TestServer/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  otel-collector-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy otel-collector.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./OTelCollector/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}



  status-page-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy status-page.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./StatusPage/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  test-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy test.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Tests/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

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

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy ingestor.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Ingestor/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  probe-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy probe.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Probe/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}


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

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy haraka.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Haraka/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  admin-dashboard-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy admin-dashboard.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./AdminDashboard/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}


  dashboard-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy dashboard.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Dashboard/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  app-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy app.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./App/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}


  copilot-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy app.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Copilot/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}

  accounts-docker-image-deploy:
    needs: [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=release,enable=true
            type=semver,value=7.0.${{needs.generate-build-number.outputs.build_number}},pattern={{version}},enable=true

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

      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

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

      - name: Generate Dockerfile from Dockerfile.tpl
        run: npm run prerun

      # Build and deploy accounts.

      - name: Login to Docker Hub 
        uses: docker/login-action@v2.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          file: ./Accounts/Dockerfile
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            GIT_SHA=${{ github.sha }}
            APP_VERSION=7.0.${{needs.generate-build-number.outputs.build_number}}


  publish-npm-packages:
    runs-on: ubuntu-latest
    needs: [generate-build-number]
    env:
      CI_PIPELINE_ID: ${{github.run_number}}
      NPM_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
      PACKAGE_VERSION: 7.0.${{needs.generate-build-number.outputs.build_number}}
    steps:
      - name: Checkout 
        uses: actions/checkout@v2
      - name: Preinstall 
        run: npm run prerun
      - name: Publish Infrastructure Agent
        run: bash ./Scripts/NPM/PublishAllPackages.sh
        
  

  test-e2e-release-saas: 
    runs-on: ubuntu-latest
    needs: [copilot-docker-image-deploy, accounts-docker-image-deploy, admin-dashboard-docker-image-deploy, app-docker-image-deploy, dashboard-docker-image-deploy, haraka-docker-image-deploy, ingestor-docker-image-deploy, isolated-vm-docker-image-deploy, otel-collector-docker-image-deploy, probe-docker-image-deploy, status-page-docker-image-deploy, test-docker-image-deploy, test-server-docker-image-deploy, publish-npm-packages, e2e-docker-image-deploy, helm-chart-deploy, generate-build-number, nginx-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@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0
      - run: npm run prerun && bash ./Tests/Scripts/enable-billing-env-var.sh
      - name: Start Server with release tag
        run: 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
        run: export $(grep -v '^#' config.env | xargs) && 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
      
          # 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-release-self-hosted: 
    runs-on: ubuntu-latest
    # After all the jobs runs
    needs: [copilot-docker-image-deploy, accounts-docker-image-deploy, admin-dashboard-docker-image-deploy, app-docker-image-deploy, dashboard-docker-image-deploy, haraka-docker-image-deploy, ingestor-docker-image-deploy, isolated-vm-docker-image-deploy, otel-collector-docker-image-deploy, probe-docker-image-deploy, status-page-docker-image-deploy, test-docker-image-deploy, test-server-docker-image-deploy, publish-npm-packages, e2e-docker-image-deploy, helm-chart-deploy, generate-build-number, nginx-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@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 18.3.0
      - run: npm run prerun
      - name: Start Server with release tag
        run: 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
        run: export $(grep -v '^#' config.env | xargs) && 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
      
          # 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

  github-release:
    needs: [test-e2e-release-saas, test-e2e-release-self-hosted, generate-build-number]
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/release'
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}
      - run: echo "${{needs.generate-build-number.outputs.build_number}}"
      - name: "Build Changelog"
        id: build_changelog
        uses: mikepenz/release-changelog-builder-action@v4.2.0
        with:
          configuration: "./Scripts/Release/ChangelogConfig.json"
      - run: echo "Changelog:"    
      - run: echo "${{steps.build_changelog.outputs.changelog}}"
      - uses: ncipollo/release-action@v1
        with:
          tag: "7.0.${{needs.generate-build-number.outputs.build_number}}"
          artifactErrorsFailBuild: true
          body: |
            ${{steps.build_changelog.outputs.changelog}}


  infrastructure-agent-deploy:
    needs: [github-release]
    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@v5
        with:
          install-only: true

      - name: Show GoReleaser version
        run: goreleaser -v

      - name: Run GoReleaser
        run: cd InfrastructureAgent && export GORELEASER_CURRENT_TAG=7.0.${{needs.generate-build-number.outputs.build_number}} && goreleaser release --clean --snapshot
      
      # Upload binaries to github release
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            InfrastructureAgent/dist/*
          token: ${{ secrets.GITHUB_TOKEN }}
          draft: false
          prerelease: false
          tag_name: 7.0.${{needs.generate-build-number.outputs.build_number}}


