import java.text.SimpleDateFormat import groovy.transform.Field @Field List props = [] pipeline { agent none parameters { choice( name: 'ACTION', choices: ['DAY','WEEK'], description: 'Выбор бэкапа' ) } options { disableConcurrentBuilds() timestamps() buildDiscarder(logRotator(numToKeepStr: '10')) skipDefaultCheckout() timeout(time: 360, unit: 'MINUTES') } environment { BUILD_USER = getBuildUser() CURRENT_DATE = new Date().format( 'dd.MM.yy_HH:mm' ) } triggers{ parameterizedCron(''' 30 19 * * 1,2,3,4 %ACTION=DAY 30 19 * * 5 %ACTION=WEEK ''') } stages { stage('Prepare_MASTER') { agent {label "master"} steps { deleteDir() gitClone("master") } } stage('Prepare_SITE') { agent {label "net"} steps { deleteDir() gitClone("master") } } ///// stage('1CDB') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'DAY'; environment name: 'ACTION', value: 'WEEK'} } steps { pwsh script: "./site/1cdb_session.ps1 1CDB" updateDesc("${env.STAGE_NAME}") sendME("${env.STAGE_NAME}","Backup success!!!") telegramSend(message: '1C backup success!!!', chatId: 648920818) } } //// stage('Move_day') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'MOVE'; environment name: 'ACTION', value: 'DAY'} } steps { sh ''' mv /srv/SITE/1CDB*.7z /srv/SITE/1CDB/ find /srv/SITE/1CDB/ -name "*.7z" -mtime +10 -exec rm -f {} \\; ''' telegramSend(message: 'Day 1CDB MOVE success!!!', chatId: 648920818) } } //// //// stage('Move_week') { agent {label "master"} when { beforeAgent true anyOf {environment name: 'ACTION', value: 'MOVE'; environment name: 'ACTION', value: 'WEEK'} } steps { sh ''' mv /srv/SITE/1CDB*.7z /srv/1CSRV/1CDB/ find /srv/1CSRV/1CDB/ -name "*.7z" -mtime +60 -exec rm -f {} \\; ''' telegramSend(message: 'WEEK 1CDB MOVE success!!!', chatId: 648920818) } } //// } post { success { node ("master") { jabberNotify notificationStrategy: 'success', notifyCulprits: true, notifyFixers: true, notifySuspects: true, notifyUpstreamCommitters: true, extraMessage: 'Бэкап 1С завершен успешно', targets: 'user@domen' } } aborted { node ("master") { jabberNotify notificationStrategy: 'aborted', notifyCulprits: true, notifyFixers: true, notifySuspects: true, notifyUpstreamCommitters: true, extraMessage: 'Бэкап 1С пришлось остановить', targets: 'user@domen' } } failure { node ("master") { jabberNotify notificationStrategy: 'failure', notifyCulprits: true, notifyFixers: true, notifySuspects: true, notifyUpstreamCommitters: true, extraMessage: 'Бэкап 1С завершился с ошибкой', 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 """
Задача бэкапирует сетевые диски| Задания бэкапа: | |||||||
|---|---|---|---|---|---|---|---|
| Бэкапирование | Дата выполнения | Время завершения | |||||
| ${prop.ACTION} | ${prop.BACKUP_DATE} | ${prop.END_TIME} | |||||