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"
  tags:
    - global-runner
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - cp /dist.py ./
    - python dist.py
  artifacts:
    paths:
      - dist/*.mkp

upload:
  stage: upload
  image:
    name: curlimages/curl:latest
    pull_policy: always # available: always, if-not-present, never
  tags:
    - global-runner
  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_job:
  image: gitlab/glab:latest
  stage: release
  tags:
    - global-runner
  script:
    - pwd
    - ls -la
    - ls -la dist
    - echo ${CHECKMK_PLUGIN_FILENAME}
    - glab config set check_update false --global
    - glab auth login --job-token $CI_JOB_TOKEN --hostname $CI_SERVER_HOST --api-protocol $CI_SERVER_PROTOCOL
    - GITLAB_HOST=$CI_SERVER_URL glab release list -R $CI_PROJECT_PATH
    - glab release create $CI_COMMIT_TAG -T $CI_COMMIT_TAG --notes "${CI_COMMIT_TAG_MESSAGE}" -r "${CI_COMMIT_SHA}" -n "Release ${CI_COMMIT_TAG}"
    - glab release create $CI_COMMIT_TAG --assets-links='[{"name":"$CHECKMK_PLUGIN_FILENAME","url":"${PACKAGE_REGISTRY_URL}/${CHECKMK_PLUGIN_FILENAME}","link_type":"package"}]'
    #- glab release create $CI_COMMIT_TAG -T $CI_COMMIT_TAG --notes "${CI_COMMIT_TAG_MESSAGE}" ./dist/*.mkp
