pipeline {
  agent none
  parameters {
    string(name: 'RELEASE', description: 'Release numeric name (e.g. 2022.10.01)', trim: true)
    string(name: 'PACKAGER_LIST', description: 'List of projects with debian package(s) (packagers) to build for this release separated with , (e.g. xivo,xivo-confgend,xivo-sysconfd)', trim: true)
    string(name: 'LTS_CODENAME', description: 'The production release name (eg. kuma)', trim: true)
    booleanParam(name: 'DO_TECHNICAL_RELEASE', defaultValue: true, description: 'Publish xivo-<X.Y>-latest on the archive repo')
    booleanParam(name: 'DO_PROD_RELEASE', defaultValue: true, description: 'Execute the production release on debian repo')
    booleanParam(name: 'SKIP_TAG', defaultValue: false, description: 'Skip the tagging stages. xivo-multi-tag already skips the packagers it has tagged, so replaying them is harmless; use this to go straight to a publication stage.')
  }
  stages {
    stage('check-parameters') {
      agent any
      steps {
        script {
          // The pipeline publishes without asking for a confirmation, so the values it
          // builds the repository names from are checked before anything is tagged.
          if (!(params.RELEASE ==~ /[0-9]{4}\.[0-9]{2}\.[0-9]{2}/)) {
            error "RELEASE must be a numeric release like 2026.10.00, got '${params.RELEASE}'."
          }
          if (params.DO_PROD_RELEASE && !params.LTS_CODENAME) {
            error 'LTS_CODENAME is required by the prod-release stage: it names the repository to publish to.'
          }
          echo "PROD build for XiVO ${params.RELEASE} (${params.LTS_CODENAME})"
        }
      }
    }
    stage('create-prod-tag') {
      agent any
      when {
        expression { !params.SKIP_TAG }
      }
      steps {
        echo "Tagging for real"
        sh 'set -xe; xivo-multi-tag prod ${RELEASE} ${PACKAGER_LIST}'
        echo "Tagging done !"
      }
    }
    stage('technical-release') {
      agent any
      when {
        expression { params.DO_TECHNICAL_RELEASE == true }
      }
      steps {
        echo "Adding new version to xivo-${RELEASE.substring(0,7)}-latest."
        build(
            job: 'create-prod-and-publish',
            parameters: [
                string(name: 'DESTINATION', value: "xivo-${RELEASE.substring(0,7)}-latest"),
                string(name: 'SOURCE_SNAPSHOT', value: "xivo-${RELEASE}"),
                string(name: 'REPO', value: "archive")],
            wait: true
        )
        echo "New version xivo-${RELEASE.substring(0,7)}-latest successfully created."
      }
    }
    stage('prod-release') {
      agent any
      when {
        expression { params.DO_PROD_RELEASE == true }
      }
      steps {
        echo "Adding new version to xivo-${LTS_CODENAME}."
        build(
            job: 'create-prod-and-publish',
            parameters: [
                string(name: 'DESTINATION', value: "xivo-${LTS_CODENAME}"),
                string(name: 'SOURCE_SNAPSHOT', value: "xivo-${RELEASE}"),
                string(name: 'REPO', value: "debian")],
            wait: true
        )
        echo "New version xivo-${LTS_CODENAME} successfully created."
      }
    }
  }
}
