You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Pipeline禁用Polyspace参数时仍选择对应Agent的问题求助

问题原因

Jenkins声明式流水线里,阶段级的agent配置会比when条件先执行。哪怕你用when条件跳过Polyspace阶段,Jenkins还是会提前给这个阶段分配YYY_1节点,还会执行SCM代码拉取,这就导致哪怕阶段禁用,也得等这个节点空闲,白白浪费时间。

解决方案

把Polyspace阶段里的节点分配逻辑,从声明式的agent改成脚本式的node()块,这样只有when条件满足时,才会去申请节点和拉取代码。

修改后的Polyspace阶段代码

stage('Polyspace') {
    when { expression { params.POLYSPACE } }
    options {
        timeout(time: 3, unit: 'HOURS') 
    }
    steps {
        script {
            // 只有POLYSPACE参数为true时,才会申请YYY_1节点
            node('YYY_1') {
                // 如果需要在这个节点重新拉取代码就加这句;要是全局已经拉过且节点能共享工作区,就删掉
                checkout scm
                // 执行Polyspace任务
                mqPipelineConfigPolyspace(config)
            }
        }
    }
}

原理说明

  • 删掉阶段级的agent配置,避免Jenkins提前预分配节点
  • 脚本式的node('YYY_1') { ... }会在when条件验证通过后才执行,这时候才会去申请YYY_1节点并跑后续步骤
  • 如果流水线已经在XXX_1节点拉过代码,且YYY_1能访问这个工作区(比如用共享存储),可以不用加checkout scm,直接复用已有代码;不然就得加这句在YYY_1上重新拉取。
完整修改后的流水线代码
@Library('SLXXX') _

log.init("info")

pipeline {
    agent {
        node {
            label 'XXX_1'
        }
    }
    triggers {
        cron('H 1 * * *')
    }
    options {
        disableConcurrentBuilds()
        buildDiscarder(logRotator(numToKeepStr: '10'))
        timeout(time: 4, unit: 'HOURS')
        timestamps ()
    }
    
    parameters {
        booleanParam(name: 'INIT', defaultValue: true, description: 'Init after yaml script update')
        booleanParam(name: 'COMPILE', defaultValue: true, description: 'Run COMPILE')
        booleanParam(name: 'POLYSPACE', defaultValue: false, description: 'Run polyspace')
    }
    
    stages {
        stage('Init') {
            when { expression { params.INIT } }
            steps {
                script {
                    xxx
                }
                script {
                    xxx
                }
            }
        }
    
        stage('Compile') {
            when { expression { params.COMPILE } }
            steps {
                dir('ThirdParty/Jenkins') {
                    xxx
                }
                
                archiveArtifacts artifacts: 'xxx'
                archiveArtifacts artifacts: 'xxx'
                archiveArtifacts artifacts: 'xxx'
            }
        }
        stage('Polyspace') {
            when { expression { params.POLYSPACE } }
            options {
                timeout(time: 3, unit: 'HOURS') 
            }
            steps {
                script {
                    node('YYY_1') {
                        checkout scm
                        // run polyspace build from yaml file description
                        mqPipelineConfigPolyspace(config)
                    }
                }
            }
        }   
    }
    
    post { 
        always { 
            echo 'This will always run for saving artifacts from failed tests'
        }
        
        success {
            echo 'This will run only if successful'
            script {
                xxx
            }
        }
        failure {
            echo 'This will run only if failed'
            script {
                xxx
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Mihai Toma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:07:48