name: Bridge UI Build and Publish

permissions:
  contents: read
  actions: read
  packages: write

on:
  pull_request:
    branches:
      - main
    paths:
      - '.github/workflows/bridge-ui-publish.yml'
      - 'bridge-ui/**'
      - '!bridge-ui/**/*.md'
  push:
    branches:
      - main
    paths:
      - '.github/workflows/bridge-ui-publish.yml'
      - 'bridge-ui/**'
      - '!bridge-ui/**/*.md'

jobs:
  publish:
    if: github.event.pull_request.head.repo.fork == false
    # ~1 min saved vs small
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-med
    strategy:
      matrix:
        environment: [development, production]
    env:
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
      DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Get version from package.json
        id: package-version
        uses: martinbeentjes/npm-get-version-action@master
        with:
          path: ./bridge-ui

      - name: Set Docker Tag
        id: docker-tag
        run: echo "DOCKER_TAG=${GITHUB_SHA:0:7}-$(date +%s)-bridge-ui-${{ steps.package-version.outputs.current-version }}-${{ matrix.environment }}" | tee $GITHUB_ENV

      - name: Login to Docker Repository
        if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
        uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 #v3.4.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 #v3.11.1

      - name: Docker Image Build and Publish
        id: docker-build-publish
        uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 #v6.18.0
        with:
          context: .
          file: ./bridge-ui/Dockerfile
          platforms: linux/amd64
          push: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
          tags: consensys/linea-bridge-ui:${{ env.DOCKER_TAG }}
          build-args: |
            ENV_FILE=./bridge-ui/.env.production
            NEXT_PUBLIC_ENVIRONMENT=${{ env.NEXT_PUBLIC_ENVIRONMENT }}
            NEXT_PUBLIC_WALLET_CONNECT_ID=${{ env.NEXT_PUBLIC_WALLET_CONNECT_ID }}
            NEXT_PUBLIC_INFURA_ID=${{ env.NEXT_PUBLIC_INFURA_ID }}
            NEXT_PUBLIC_QUICKNODE_ID=${{ env.NEXT_PUBLIC_QUICKNODE_ID }}
            NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID=${{ env.NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID }}
            NEXT_PUBLIC_LIFI_API_KEY=${{ env.NEXT_PUBLIC_LIFI_API_KEY }}
            NEXT_PUBLIC_LIFI_INTEGRATOR_NAME=${{ env.NEXT_PUBLIC_LIFI_INTEGRATOR_NAME }}
            NEXT_PUBLIC_ONRAMPER_API_KEY=${{ env.NEXT_PUBLIC_ONRAMPER_API_KEY }}
            NEXT_PUBLIC_LAYERSWAP_API_KEY=${{ env.NEXT_PUBLIC_LAYERSWAP_API_KEY }}
          cache-from: type=registry,ref=consensys/linea-bridge-ui:buildcache
          cache-to: type=registry,ref=consensys/linea-bridge-ui:buildcache,mode=max
        env:
          NEXT_PUBLIC_WALLET_CONNECT_ID: ${{ secrets.PUBLIC_WALLET_CONNECT_ID }}
          NEXT_PUBLIC_INFURA_ID: ${{ secrets.PUBLIC_BRIDGE_UI_INFURA_ID }}
          NEXT_PUBLIC_QUICKNODE_ID: ${{ secrets.PUBLIC_BRIDGE_UI_QUICKNODE_ID }}
          NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID: ${{ secrets.PUBLIC_DYNAMIC_ENVIRONMENT_ID }}
          NEXT_PUBLIC_LIFI_API_KEY: ${{ secrets.PUBLIC_LIFI_API_KEY }}
          NEXT_PUBLIC_LIFI_INTEGRATOR_NAME: ${{ secrets.PUBLIC_LIFI_INTEGRATOR_NAME }}
          NEXT_PUBLIC_ONRAMPER_API_KEY: ${{ secrets.PUBLIC_ONRAMPER_API_KEY }}
          NEXT_PUBLIC_LAYERSWAP_API_KEY: ${{ secrets.PUBLIC_LAYERSWAP_API_KEY }}
          NEXT_PUBLIC_ENVIRONMENT: ${{ matrix.environment }}

  test-build:
    if: github.event.pull_request.head.repo.fork == true
    # ~1 min saved vs small
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-med
    name: Test Build Bridge UI
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup nodejs environment
        uses: ./.github/actions/setup-nodejs
        with:
          node-version: 18.17.0
          pnpm-install-options: '--frozen-lockfile --prefer-offline'

      - name: Test Build Bridge UI
        run: pnpm run -F bridge-ui build;
        env:
          NODE_OPTIONS: --max-old-space-size=8192 # Temporary fix for out of memory error
