如何添加超时以在 Jenkins 声明性管道中选择代理

问题描述 投票:0回答:1

在 Jenkins 中,我使用声明式管道根据用户输入参数选择代理。问题是,当代理离线时,管道将永远保留在构建队列中。我想添加某种超时来选择代理。

如何正确添加超时?任何帮助将不胜感激。

这是我的管道:

pipeline {
    agent { node { label "${params.AGENT}" }}
    parameters {
        choice(
            name: 'AGENT',
            description: 'Host where the pipeline will be run.',
            choices: [ 'foo', 'bar' ],
        )
    }
    stages {
        stage('Rest of the pipeline') {
            steps {
                echo "the rest of my pipeline..."
            }
        }
    }
}

我尝试添加超时选项

    options {
        timeout(time: 5, unit: 'MINUTES', activity: true)
    }

但这对于选择

agent
不起作用(我认为这发生在任何阶段之前)。

jenkins groovy jenkins-groovy jenkins-declarative-pipeline
1个回答
0
投票

我的解决方案是没有代理,直到我检查所需的代理作为单独的阶段在线,然后我在阶段级别而不是顶层指定代理。

pipeline {
    agent none
    parameters {
        choice(
            name: 'AGENT',
            description: 'Host where the pipeline will be run.',
            choices: [ 'foo', 'bar' ],
        )
    }
    stages {
        stage('Check Node Status') {
            steps {
                script {
                    if (!Jenkins.instance.getNode(params.AGENT).toComputer().isOnline()) {
                        error "Node ${params.AGENT} is offline"
                    }
                }
            }
        }
        stage('Rest of the pipeline') {
            agent { label params.DESTINATION_HOST }
            steps {
                echo "the rest of my pipeline..."
            }
        }
    }
}

这样做的缺点是,如果我有很多阶段,我必须一遍又一遍地指定

agent

文档:

© www.soinside.com 2019 - 2024. All rights reserved.