stages:
  - build
  - upload
  - release

variables:
  #PROJECT_AUTHOR: "${CI_COMMIT_AUTHOR}"
  PROJECT_NAME: "${CI_PROJECT_NAME}"
  CHECKMK_PLUGIN_FILENAME: "${PROJECT_NAME}-${CI_COMMIT_TAG}.mkp"
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PROJECT_NAME}/${CI_COMMIT_TAG}"

build:
  stage: build
  image: gitlab.ard.de:4567/checkmk/build-mkp:latest
  #image: "${CI_REGISTRY}/checkmk/build-mkp:latest"
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - python dist.py
  artifacts:
    paths:
      - dist/*.mkp

upload:
  stage: upload
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
        --upload-file dist/${CHECKMK_PLUGIN_FILENAME} "${PACKAGE_REGISTRY_URL}/${CHECKMK_PLUGIN_FILENAME}"

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      release-cli create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG --ref "$CI_COMMIT_SHA" \
        --description "${CI_COMMIT_TAG_MESSAGE}" \
        --assets-link "{\"name\":\"${CHECKMK_PLUGIN_FILENAME}\", \"url\":\"${PACKAGE_REGISTRY_URL}/${CHECKMK_PLUGIN_FILENAME}\", \"link_type\": \"package\"}"
