image: node:20-bookworm-slim

stages:
  - build
  - docker-build
  - docker-release

build-all:
  stage: build
  variables:
    TURBO_TOKEN: ${TURBO_TOKEN}
    TURBO_TEAM: ${TURBO_TEAM}
    ZAMMAD_URL: ${ZAMMAD_URL}
  script:
    - npm install npm@latest -g
    - npm install -g turbo
    - npm ci
    - turbo build

.docker-build:
  image: registry.gitlab.com/digiresilience/link/link-stack/buildx:${CI_COMMIT_REF_NAME}
  services:
    - docker:dind
  stage: docker-build
  variables:
    DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
    DOCKER_CONTEXT: .
  only:
    - main
    - develop
    - tags
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - DOCKER_BUILDKIT=1 docker build --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${DOCKER_CONTEXT}
    - docker push ${DOCKER_NS}:${DOCKER_TAG}

.docker-release:
  image: registry.gitlab.com/digiresilience/link/link-stack/buildx:${CI_COMMIT_REF_NAME}
  services:
    - docker:dind
  stage: docker-release
  variables:
    DOCKER_TAG: ${CI_COMMIT_SHORT_SHA}
    DOCKER_TAG_NEW: ${CI_COMMIT_REF_NAME}
  only:
    - main
    - develop
    - tags
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker pull ${DOCKER_NS}:${DOCKER_TAG}
    - docker tag ${DOCKER_NS}:${DOCKER_TAG} ${DOCKER_NS}:${DOCKER_TAG_NEW}
    - docker push ${DOCKER_NS}:${DOCKER_TAG_NEW}

buildx-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/buildx
    DOCKERFILE_PATH: ./docker/buildx/Dockerfile

buildx-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/buildx

link-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/link
    DOCKERFILE_PATH: ./apps/link/Dockerfile

link-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/link

leafcutter-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/leafcutter
    DOCKERFILE_PATH: ./apps/leafcutter/Dockerfile

leafcutter-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/leafcutter

bridge-frontend-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-frontend
    DOCKERFILE_PATH: ./apps/bridge-frontend/Dockerfile

bridge-frontend-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-frontend

bridge-worker-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-worker
    DOCKERFILE_PATH: ./apps/bridge-worker/Dockerfile

bridge-worker-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-worker

bridge-whatsapp-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-whatsapp
    DOCKERFILE_PATH: ./apps/bridge-whatsapp/Dockerfile

bridge-whatsapp-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/bridge-whatsapp

signal-cli-rest-api-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/signal-cli-rest-api
    DOCKERFILE_PATH: ./docker/signal-cli-rest-api/Dockerfile

signal-cli-rest-api-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/signal-cli-rest-api

memcached-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/memcached
    DOCKERFILE_PATH: ./docker/memcached/Dockerfile

memcached-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/memcached

nginx-proxy-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/nginx-proxy
    DOCKERFILE_PATH: ./docker/nginx-proxy/Dockerfile

nginx-proxy-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/nginx-proxy

opensearch-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/opensearch
    DOCKERFILE_PATH: ./docker/opensearch/Dockerfile

opensearch-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/opensearch

opensearch-dashboards-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/opensearch-dashboards
    DOCKERFILE_PATH: ./docker/opensearch-dashboards/Dockerfile

opensearch-dashboards-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/opensearch-dashboards

postgresql-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/postgresql
    DOCKERFILE_PATH: ./docker/postgresql/Dockerfile

postgresql-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/postgresql

redis-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/redis
    DOCKERFILE_PATH: ./docker/redis/Dockerfile

redis-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/redis

zammad-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad
    DOCKERFILE_PATH: ./docker/zammad/Dockerfile
    DOCKER_CONTEXT: ./docker/zammad
  before_script:
    - apk --update add nodejs npm
  script:
    - npm install npm@latest -g
    - npm install -g turbo
    - npm ci
    - turbo build --force --filter @link-stack/zammad-addon-*
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - DOCKER_BUILDKIT=1 docker build --build-arg EMBEDDED=true --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${DOCKER_CONTEXT}
    - docker push ${DOCKER_NS}:${DOCKER_TAG}

zammad-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad

zammad-standalone-docker-build:
  extends: .docker-build
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad-standalone
    DOCKERFILE_PATH: ./docker/zammad/Dockerfile
    DOCKER_CONTEXT: ./docker/zammad
  before_script:
    - apk --update add nodejs npm
  script:
    - npm install npm@latest -g
    - npm install -g turbo
    - npm ci
    - turbo build --force --filter @link-stack/zammad-addon-*
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - DOCKER_BUILDKIT=1 docker build --pull --no-cache -t ${DOCKER_NS}:${DOCKER_TAG} -f ${DOCKERFILE_PATH} ${DOCKER_CONTEXT}
    - docker push ${DOCKER_NS}:${DOCKER_TAG}

zammad-standalone-docker-release:
  extends: .docker-release
  variables:
    DOCKER_NS: ${CI_REGISTRY}/digiresilience/link/link-stack/zammad-standalone
