docker-build:
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      changes:
        - docker/**/*
        - docker-compose.yml
    - if: $SCHEDULED
  image: docker:26.1
  services:
    - name: docker:26.1-dind
      alias: docker
      command: ["--tls=false"]
  variables:
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""
    #DOCKER_BUILDKIT: 1
    #COMPOSE_DOCKER_CLI_BUILD: 1
  before_script:
    - docker info
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    #- docker compose build --pull --build-arg BUILDKIT_INLINE_CACHE=1
    - docker compose build --pull --no-cache
    - docker compose push

helm-package:
  stage: build
  rules:
      - if: $CI_COMMIT_BRANCH == "main"
        changes:
          - helm/**/*
          - .gitlab-ci.yml
      - if: $SCHEDULED
  image: alpine/helm:3.15.1
  before_script:
    - helm repo add stable https://charts.helm.sh/stable
    - helm repo add bitnami https://charts.bitnami.com/bitnami
    - helm repo add coder-v2 https://helm.coder.com/v2
  script:
    - helm package helm/vdo-ninja
    - helm dep build helm/ti-coder
    - helm package helm/ti-coder
    - helm package helm/ti-lamp
  artifacts:
    paths:
      - vdo-ninja-0.1.2.tgz
      - ti-coder-0.4.9.tgz
      - ti-lamp-0.1.2.tgz
    expire_in: 1 hour

helm-publish:
  stage: publish
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      changes:
        - helm/**/*
        - .gitlab-ci.yml
    - if: $SCHEDULED
  dependencies:
    - helm-package
  image: curlimages/curl:latest
  script:
    - 'curl --request POST --user gitlab-ci-token:$CI_JOB_TOKEN --form "chart=@vdo-ninja-0.1.2.tgz" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/helm/api/stable/charts"'
    - 'curl --request POST --user gitlab-ci-token:$CI_JOB_TOKEN --form "chart=@ti-coder-0.4.9.tgz" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/helm/api/stable/charts"'
    - 'curl --request POST --user gitlab-ci-token:$CI_JOB_TOKEN --form "chart=@ti-lamp-0.1.2.tgz" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/helm/api/stable/charts"'

stages:
  - build
  - publish
