import java.text.SimpleDateFormat import groovy.transform.Field // Misc manipulations import java.nio.charset.StandardCharsets pipeline { agent { node { label 'mail_acc' } } parameters { choice( name: 'ACTION', choices: ['New-Mail', 'Delete-Mail', 'Get-Mail'], description: 'Выбор действия, Get-Mail - только обновит список пользователей' ) string( name: 'email', defaultValue: '@domain', description: 'Желаемый email пользователя, если меняем пароль, выбираем из списка, при создании нового, выбрать свободное имя' ) string( name: 'MailPass', defaultValue: '123qweASD', description: 'Пароль пользователя, стандартный лучше не использовать, во избежании случайного подключения другого пользователя' ) booleanParam( name: 'NeedSharedMail', defaultValue: false, description: 'Нужны права на общий ящик: shared@domain' ) } options { disableConcurrentBuilds() timestamps() buildDiscarder(logRotator(numToKeepStr: '10')) skipDefaultCheckout() timeout(time: 40, unit: 'MINUTES') } environment { BUILD_USER = getBuildUser() CURRENT_DATE = new Date().format('dd.MM.yy_HHmm') DIR_MAIL = "./jenkins/ps1/" username = "${BUILD_USER}"+"${BUILD_ID}" } stages { stage('getmail') { when { beforeAgent true environment name: 'ACTION', value: 'Get-Mail' } steps { dir("${DIR_MAIL}") { powershell ''' ./getmailbox.ps1 ''' updateDesc('mailbox.nausd') } } } stage('newmail') { when { beforeAgent true environment name: 'ACTION', value: 'New-Mail' } steps { dir("${DIR_MAIL}") { println "${username}" println "${email}" println "${MailPass}" powershell script: "./newmailbox.ps1 $username $email $MailPass $NeedSharedMail" updateDesc('mailbox.nausd') } } } stage('removemailbox') { when { beforeAgent true environment name: 'ACTION', value: 'Delete-Mail' } steps { dir("${DIR_MAIL}") { println "${username}" println "${email}" println "${MailPass}" powershell script: "./removemailbox.ps1 $email" updateDesc('mailbox.nausd') } } } } } def updateDesc(String propertyFile) { def keyMapping = [ DisplayName: 'UserName', WindowsEmailAddress: 'EMail', CustomAttribute1: 'Password', ] def props = [] def mailInfo = [:] readFile("${propertyFile}").split('\n').each { line -> if (!line.isEmpty()) { def fields = line.split(':').collect { it.trim() } mailInfo.put(keyMapping.get(fields.first()), fields.last()) if (fields.first().equals(keyMapping.keySet().last())) { props.add(mailInfo.clone()) mailInfo.clear() } } else { return } } int NUMBER_OF_MAILS = props.size(); println("NUMBER_OF_MAILS: ${NUMBER_OF_MAILS}") props.each { println("Mail Info
: ${it}") } def item = Jenkins.instance.getItemByFullName("${JOB_NAME}") item.setDescription("${getDescTemplate(props)}") } def getBuildUser() { return currentBuild.rawBuild.getCause(Cause.UserIdCause)?.getUserId() } /* def gitClone(String stand) { checkout poll: false, scm: [ $class : 'GitSCM', branches : [[name: 'master']], doGenerateSubmoduleConfigurations: false, userRemoteConfigs : [[url: 'git@gitsd.naumen.ru:repo/scripts_tpivi.git']] ] } */ def scriptDir = getClass().protectionDomain.codeSource.location.path println "${scriptDir}" String currentDir = new File(".").getAbsolutePath() def getDescTemplate(List props) { return """ Задача управляет тестовым почтовым сервером Exchange
Если сборка не прошла, то обращаться в ТПиВИ
${props.collect { prop -> def idx = props.findIndexOf { it.equals(prop) } return """ """ }.join('')}
Почтовые ящики:
Имя пользователя Почтовый ящик пользователя Пароль пользователя
${prop.UserName} ${prop.EMail} ${prop.Password}



При запуске необходимо указать параметры:
""" }