image: node:20-bookworm-slim

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

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

.docker-build:
  image: registry.gitlab.com/guardianproject-ops/docker-alpine-git:latest
  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/guardianproject-ops/docker-alpine-git:latest
  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}

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

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

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

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

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

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

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

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

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

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

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

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

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

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