spec:
  inputs:
    # Job and Stage name
    as:
      default: 'module-release'
      description: 'Defines the name of this job.'
    stage:
      default: 'release'
      description: 'Defines the stage that this job will belong to.'

    root_dir:
      default: ${CI_PROJECT_DIR}
      description: 'Root directory for the OpenTofu module.'

    module_name:
      default: ${CI_PROJECT_NAME}
      description: |
        Name of the module that should be published.
        Must comply with the naming restrictions of the module registry.

    module_system:
      default: local
      description: |
        System that this module can be used with.
        e.g. 'local' or 'azurerm'
    
    module_version:
      description: |
        Version of the module that should be published.
        e.g. '0.1.0'
      default: ${CI_COMMIT_TAG}

---

'$[[ inputs.as ]]':
  stage: $[[ inputs.stage ]]
  image: curlimages/curl:8.8.0
  variables:
    TAR_FILENAME: /tmp/${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}.tgz
    TF_ROOT: $[[ inputs.root_dir ]]
    UPLOAD_URL:
      ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/terraform/modules/$[[ inputs.module_name ]]/$[[ inputs.module_system ]]/$[[ inputs.module_version ]]/file
  script:
    - >-
      tar -vczf "$TAR_FILENAME"
      -C "$TF_ROOT"
      --exclude=./.git
      --exclude=./.terraform*
      .
    - >-
      curl
      --fail-with-body
      --location
      --header "JOB-TOKEN: ${CI_JOB_TOKEN}"
      --upload-file "$TAR_FILENAME"
      "$UPLOAD_URL"
