import java.text.SimpleDateFormat import groovy.transform.Field @Field List props = [] pipeline { agent none parameters { choice( name: 'ACTION', choices: ['ALL','PROG','DOC','WORK','MOVE'], 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 01 * * 6 %ACTION=ALL ''') } stages { stage('Prepare_MASTER') { agent {label "master"} steps { deleteDir() gitClone("master") } } stage('Prepare_SITE') { agent {label "net"} steps { deleteDir() gitClone("master") } } ///// stage('Backup_WORK') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'WORK'; environment name: 'ACTION', value: 'ALL'} } steps { pwsh script: "./site/site_session.ps1 WORK $env.JOB_NAME" updateDesc("${env.STAGE_NAME}") sendME("${env.STAGE_NAME}","Backup success!!!") telegramSend(message: 'WORK backup success!!!', chatId: 648920818) } } ///// ///// stage('Backup_PROG') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'PROG'; environment name: 'ACTION', value: 'ALL'} } steps { pwsh script: "./site/site_session.ps1 PROG $env.JOB_NAME" updateDesc("${env.STAGE_NAME}") sendME("${env.STAGE_NAME}","Backup success!!!") telegramSend(message: 'PROG backup success!!!', chatId: 648920818) } } ///// stage('Backup_DOC') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'DOC'; environment name: 'ACTION', value: 'ALL'} } steps { pwsh script: "./site/site_session.ps1 DOC $env.JOB_NAME" updateDesc("${env.STAGE_NAME}") sendME("${env.STAGE_NAME}","Backup success!!!") telegramSend(message: 'DOC backup success!!!', chatId: 648920818) } } //// stage('Move_del') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'MOVE'; environment name: 'ACTION', value: 'ALL'} } steps { sh ''' mv /srv/SITE/*.7z /srv/1CSRV/ find /srv/1CSRV/ -name "*.7z" -mtime +6 -exec rm -f {} \\; ''' telegramSend(message: 'ALL MOVE success!!!', chatId: 648920818) } } //// } post { success { node ("master") { jabberNotify notificationStrategy: 'success', notifyCulprits: true, notifyFixers: true, notifySuspects: true, notifyUpstreamCommitters: true, extraMessage: 'Сетевые диски успешно сбэкапированы, можно забирать', 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 """ Задача бэкапирует сетевые диски
Если сборка не прошла, звонить :)
${props.collect { prop -> def idx = props.findIndexOf { it.equals(prop) } return """ """ }.join('')}
Задания бэкапа:
Бэкапирование Дата выполнения Время завершения
${prop.ACTION} ${prop.BACKUP_DATE} ${prop.END_TIME}



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