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)
  }
  stages {
    stage('welcome') {
      agent any
      input {
        message "Ready to release XiVO ${RELEASE} (${LTS_CODENAME}) in production?"
        ok "Yes, go go go !"
      }
      steps {
        echo "Welcome to this new PROD build for XiVO ${RELEASE} (${LTS_CODENAME})!"
      }
    }
    stage('simulate-create-prod-tag') {
      agent any
      steps {
        echo "Simulating the package tagging automation"
        sh 'set -xe; xivo-multi-tag prod ${RELEASE} --dry-run ${PACKAGER_LIST}'
        echo "Simulation done !"
      }
    }
    stage('create-prod-tag') {
      agent any
      input {
        message "Tag ${PACKAGER_LIST} like the simulation ?"
        ok "Ready"
      }
      steps {
        echo "Tagging for real"
        sh 'set -xe; xivo-multi-tag prod ${RELEASE} ${PACKAGER_LIST}'
        echo "Tagging done !"
      }
    }
    stage('technical-release') {
      agent any
      input {
        message "Click \"Do technical release\" to release version xivo-${RELEASE.substring(0,7)}-latest on archive repo."
        ok "Do technical release"
      }
      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
      input {
        message "Click \"Do production release\" to release version xivo-${LTS_CODENAME} on debian repo."
        ok "Do production release"
      }
      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."
      }
    }
  }
}
