variables:
  DOCKER_IMAGE: harbor.kvazaric.ru/kube-forge/kube-forge

  KANIKO_IMAGE: harbor.kvazaric.ru/postgres-tasks/kaniko_executor:debug
  KANIKO_CONTEXT: ${CI_PROJECT_DIR}
  RELEASE_CLI_IMAGE: harbor.kvazaric.ru/kube-forge/release-cli:v0.18.0
  CICD_MULTITOOL: harbor.kvazaric.ru/postgres-tasks/cicd-multitool:2.4
  BINARY_BUILDER_IMAGE: harbor.kvazaric.ru/mirror/library/golang:alpine

  PREPARE_RELEASE:
    options:
      - "No"
      - "Yes"
    value: "No"
    description: Create release from this build?

  WITHOUT_BUILD:
    options:
      - "No"
      - "Yes"
    value: "Yes"
    description: Create release without building?

stages:
  - prepare
  - build
  - release

make-version-name:
  stage: prepare
  image:
    name: ${CICD_MULTITOOL}
    entrypoint: [""]
  allow_failure: false
  tags:
    - build
    - docker
  script:
    - echo "VERSION=$(cat ${CI_PROJECT_DIR}/build/version.txt)" > version.env
  artifacts:
    reports:
      dotenv: version.env

build-docker:
  variables:
    DOCKERFILE_PATH: Dockerfile
    IMAGE_DESTINATION: image
  stage: build
  image:
    name: ${KANIKO_IMAGE}
    entrypoint: [""]
  allow_failure: false
  tags:
    - build
    - docker
  before_script:
    - mkdir -p /kaniko/.docker
    - echo "${DOCKER_AUTH_CONFIG}" > /kaniko/.docker/config.json
  script:
    - |
      /kaniko/executor --context "${KANIKO_CONTEXT}" \
        --dockerfile ${CI_PROJECT_DIR}/build/docker/Dockerfile \
        --destination "${DOCKER_IMAGE}:${VERSION}-${CI_COMMIT_SHORT_SHA}"
  dependencies:
    - make-version-name
  needs:
    - make-version-name
  rules:
    - if: $PREPARE_RELEASE == "Yes" && $WITHOUT_BUILD == "Yes"
      when: never
    - changes:
        - build/docker/**/*
        - cmd/**/*
        - internal/**/*
        - kubespray/**/*
        - go.sum
        - go.mod
        - requirements.txt
        - ansible.cfg
      if: $CI_COMMIT_REF_NAME =~ /^(master)$/
      when: always

build-binary:
  stage: build
  image:
    name: ${BINARY_BUILDER_IMAGE}
    entrypoint: [""]
  allow_failure: false
  tags:
    - build
    - docker
  script:
    - CGO_ENABLED=0 GOOS=linux  go build -v -o kube-forge ./cmd/main/main.go
  artifacts:
    paths:
      - ${CI_PROJECT_DIR}/kube-forge
  rules:
    - if: $PREPARE_RELEASE == "Yes" && $WITHOUT_BUILD == "Yes"
      when: never
    - changes:
        - build/docker/**/*
        - cmd/**/*
        - internal/**/*
        - kubespray/**/*
        - go.sum
        - go.mod
        - requirements.txt
        - ansible.cfg
      if: $CI_COMMIT_REF_NAME =~ /^(master)$/
      when: always

prepare-tarball:
  stage: build
  image: ${CICD_MULTITOOL}
  allow_failure: false
  tags:
    - build
    - docker
  script:
    - mkdir archive
    - cp kube-forge archive
    - cp requirements.txt archive
    - cp -R kubespray archive
    - tar zcvf kube-forge-${VERSION}-${CI_COMMIT_SHORT_SHA}.tar.gz archive
  dependencies:
    - make-version-name
    - build-binary
  needs:
    - make-version-name
    - build-binary
  artifacts:
    paths:
      - ${CI_PROJECT_DIR}/kube-forge-${VERSION}-${CI_COMMIT_SHORT_SHA}.tar.gz
  rules:
    - if: $PREPARE_RELEASE == "Yes" && $WITHOUT_BUILD == "Yes"
      when: never
    - changes:
        - build/docker/**/*
        - cmd/**/*
        - internal/**/*
        - kubespray/**/*
        - go.sum
        - go.mod
        - requirements.txt
        - ansible.cfg
      if: $CI_COMMIT_REF_NAME =~ /^(master)$/
      when: always

make-release:
  stage: release
  image: ${RELEASE_CLI_IMAGE}
  dependencies:
    - make-version-name
  needs:
    - make-version-name
  tags:
    - build
    - docker
  rules:
    - if: $PREPARE_RELEASE == "Yes"
  script:
    - VERSION=$(cat ${CI_PROJECT_DIR}/build/version.txt)
    - echo "Preparing release for version ${VERSION}-${CI_COMMIT_SHORT_SHA}"
  release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties
    name: kube-forge:${VERSION}-${CI_COMMIT_SHORT_SHA}
    tag_name: "${VERSION}-${CI_COMMIT_SHORT_SHA}"
    description: "Kube-forge release for version ${VERSION}-${CI_COMMIT_SHORT_SHA}"
    assets:
      links:
        - name: "Docker image"
          url: "https://harbor.kvazaric.ru/harbor/projects/4/repositories/kube-forge/artifacts-tab"
          link_type: "image"
  when: on_success
