我试图根据时钟时间设置超时

时间:2017-09-28 17:02:26

标签: jenkins groovy timeout jenkins-pipeline

我想要一个jenkins管道运行几个步骤,然后等到上午9点(或直到用户点击继续/中止)。我也对其他建议持开放态度,但我希望能够轻松解决这个问题。

以下是代码片段(等待后有阶段),超时但我收到转换错误:

TimeoutStep.time expects int but received class java.lang.String
Could not instantiate {time=1033, unit=SECONDS} for TimeoutStep(time: int, unit?: TimeUnit[NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS])

pipeline {
    agent none
    stages{
        stage('Within Approved Window') {
            agent { label 'Linux' }
            steps {
                isBuildWindow()
            }
        }        
        stage('Enable SCOM Maintenance Mode') {
            agent { label 'Windows' }
            when {
                expression { params.'Enable SCOM maintenance mode' == 'Yes' }
            }
            steps {
                build job: 'Powershell Components/startGroupMaintenanceMode', parameters: [string(name: 'ScomServer', value: '*********'), string(name: 'GroupDisplayName', value: '********'), string(name: 'DurationInMin', value: '90'), string(name: 'Reason', value: 'ApplicationInstallation'), string(name: 'Comment', value: 'Monthly Patching')]
            }
        }
        stage('Zero Control-M Initiators') {
            agent { label 'Linux' }
            when {
                expression { params.'Zero Control-M Initiators' == 'Yes' }
            }
            steps {
                sh '*****'
            }
        }
        stage('Wait til 9am') {
            agent { label 'Linux' }
            steps {
                timeout(time: env.sleep_seconds, unit: 'SECONDS') {
                    input message: 'Shall We Continue?'
                    }
            }
        }
    }
}

void isBuildWindow() {
    script {
        Calendar now = Calendar.getInstance();
        int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        int hourOfDay = now.get(Calendar.HOUR_OF_DAY);
        int sleep_seconds = 0
        if ( dayOfWeek != Calendar.SUNDAY || hourOfDay < 8 || hourOfDay > 10 ) {
            echo 'Outside Approved Build Window - Sunday between 8 and 11am'
            currentBuild.result = 'FAILURE'
            sh "exit 1"
        } else {
            echo 'Inside Approved Build Window - Sunday between 8 and 11am'
            Calendar startTime = Calendar.getInstance();
            startTime.set(Calendar.HOUR, 9);
            startTime.set(Calendar.MINUTE, 0);
            startTime.set(Calendar.SECOND, 0);
            sleep_seconds = ((startTime.getTimeInMillis() - now.getTimeInMillis()) / 1000);
            env.SLEEP_SECONDS = sleep_seconds
        }
    }
}

0 个答案:

没有答案