import java.text.SimpleDateFormat import groovy.transform.Field @Field List props = [] pipeline { agent none parameters { choice( name: 'ACTION', choices: ['ALL','1CDB','KD'], description: 'Выбор бэкапа' ) } options { disableConcurrentBuilds() timestamps() buildDiscarder(logRotator(numToKeepStr: '10')) skipDefaultCheckout() timeout(time: 2400, unit: 'MINUTES') } environment { BUILD_USER = getBuildUser() CURRENT_DATE = new Date().format( 'dd.MM.yy_HH:mm' ) } triggers{ parameterizedCron(''' 00 23 * * 5 %ACTION=KD ''') } stages { stage('Prepare_MASTER') { agent {label "master"} steps { deleteDir() gitClone("master") } } stage('Prepare_1CSRV') { agent {label "mssql"} steps { deleteDir() gitClone("master") } } ///// stage('Backup_KD') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'KD'; environment name: 'ACTION', value: 'ALL'} } steps { pwsh script: "./1csrv/1csrv_session.ps1 KD $env.JOB_NAME" updateDesc("${env.STAGE_NAME}") sendME("${env.STAGE_NAME}","Backup success!!!") telegramSend(message: 'KD backup success!!!', chatId: 648920818) } } ///// stage('Backup_1C_MSSQL') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: '1CDB'; environment name: 'ACTION', value: 'ALL'} } steps { pwsh script: "./1csrv/1csrv_session.ps1 1CDB $env.JOB_NAME" updateDesc("${env.STAGE_NAME}") sendME("${env.STAGE_NAME}","Backup success!!!") telegramSend(message: '1C backup success!!!', chatId: 648920818) } } } post { success { node ("master") { jabberNotify notificationStrategy: 'success', extraMessage: 'MSSQL и Сетевые диски успешно сбэкапированы, можно забирать', targets: 'user@domen' } } aborted { node ("master") { jabberNotify notificationStrategy: 'aborted', notifyCulprits: true, notifyFixers: true, notifySuspects: true, notifyUpstreamCommitters: true, extraMessage: 'Пришлось остановить', targets: 'user@domen' } } failure { node ("master") { jabberNotify notificationStrategy: 'failure', notifyCulprits: true, notifyFixers: true, notifySuspects: true, notifyUpstreamCommitters: true, extraMessage: 'Бэкап сетевых дисков завершился с ошибкой', targets: 'user@domen' } } } } def getBuildUser() { return currentBuild.rawBuild.getCause(Cause.UserIdCause)?.getUserId() } def updateDesc(ACTION) { def d = [ACTION: '', BACKUP_DATE: '', END_TIME: ''] END_TIME = new Date().format( 'dd.MM.yy_HH:mm' ) d.ACTION = "${ACTION}" d.BACKUP_DATE = "${CURRENT_DATE}" d.END_TIME = "${END_TIME}" d1 = d.clone() props << d1 println d1 d.clear() println props println d println "${END_TIME}" def item = Jenkins.instance.getItemByFullName("${JOB_NAME}") item.setDescription("${getDescTemplate(props)}") } def sendME(ACTION,STATUS) { emailext body: "${STATUS}", subject: "${ACTION}", to: 'therion@nasda.ru' } def gitClone(String stand) { checkout poll: false, scm: [ $class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, userRemoteConfigs: [[url: 'git@gitlab.domen:user/sys.git']] ] } def scriptDir = getClass().protectionDomain.codeSource.location.path println "${scriptDir}" String currentDir = new File(".").getAbsolutePath() def getDescTemplate(List props) { return """
Задача бэкапирует базы данных 1С| Задания бэкапа: | |||||||
|---|---|---|---|---|---|---|---|
| Бэкапирование | Дата выполнения | Время завершения | |||||
| ${prop.ACTION} | ${prop.BACKUP_DATE} | ${prop.END_TIME} | |||||