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

如何根据Jenkins选择参数加载对应团队的凭据?

可以通过以下几种方式实现Jenkins流水线根据选择的参数动态加载对应凭据:

方案一:拆分Stage配合When条件(声明式流水线标准写法)

适合需要单独追踪每个团队凭据加载步骤执行状态的场景,通过when条件判断参数值,只执行对应团队的步骤:

pipeline {
    agent any
    parameters {
        choice( choices: ['ChooseOne', 'team1', 'team2'], name: 'JenkinsInstance')
    }
    stages {
        stage('Create Credentials') {
            agent {
                container('main')
            }
            stages {
                stage('Use Team1 Credentials') {
                    when {
                        expression { params.JenkinsInstance == 'team1' }
                    }
                    steps {
                        withCredentials([[$class: 'VaultUsernamePasswordCredentialBinding', 
                            credentialsId: 'jenkins-team1-api-token', 
                            passwordVariable: 'JENKINS_USER_TOKEN', 
                            usernameVariable: 'JENKINS_USERNAME']]) {
                            sh '''
                            echo "Creating UsernamePassword credentials ID..."
                            src/jenkins/jenkins-build-agent/create-creds.sh
                            '''.stripIndent()
                        }
                    }
                }
                stage('Use Team2 Credentials') {
                    when {
                        expression { params.JenkinsInstance == 'team2' }
                    }
                    steps {
                        withCredentials([[$class: 'VaultUsernamePasswordCredentialBinding', 
                            credentialsId: 'jenkins-team2-api-token', 
                            passwordVariable: 'JENKINS_USER_TOKEN', 
                            usernameVariable: 'JENKINS_USERNAME']]) {
                            sh '''
                            echo "Creating UsernamePassword credentials ID..."
                            src/jenkins/jenkins-build-agent/create-creds.sh
                            '''.stripIndent()
                        }
                    }
                }
                stage('Invalid Selection') {
                    when {
                        expression { params.JenkinsInstance == 'ChooseOne' }
                    }
                    steps {
                        error "请从下拉菜单中选择有效的团队"
                    }
                }
            }
        }
    }
}

方案二:Script块内做条件判断(灵活简洁)

如果不想拆分多个Stage,可以在script块中通过逻辑判断动态确定凭据ID,再统一执行后续脚本:

pipeline {
    agent any
    parameters {
        choice( choices: ['ChooseOne', 'team1', 'team2'], name: 'JenkinsInstance')
    }
    stages {
        stage('Create Credentials') {
            agent {
                container('main')
            }
            steps {
                script {
                    def targetCredsId = ''
                    switch(params.JenkinsInstance) {
                        case 'team1':
                            targetCredsId = 'jenkins-team1-api-token'
                            break
                        case 'team2':
                            targetCredsId = 'jenkins-team2-api-token'
                            break
                        default:
                            error "请选择有效的团队选项"
                    }
                    
                    withCredentials([[$class: 'VaultUsernamePasswordCredentialBinding', 
                        credentialsId: targetCredsId, 
                        passwordVariable: 'JENKINS_USER_TOKEN', 
                        usernameVariable: 'JENKINS_USERNAME']]) {
                        sh '''
                        echo "Creating UsernamePassword credentials ID..."
                        src/jenkins/jenkins-build-agent/create-creds.sh
                        '''.stripIndent()
                    }
                }
            }
        }
    }
}

方案三:动态拼接凭据ID(最简洁,需命名规则固定)

如果你的凭据ID遵循jenkins-${团队名}-api-token的固定命名规则,可以直接通过参数拼接生成凭据ID,省去条件判断:

pipeline {
    agent any
    parameters {
        choice( choices: ['ChooseOne', 'team1', 'team2'], name: 'JenkinsInstance')
    }
    stages {
        stage('Create Credentials') {
            agent {
                container('main')
            }
            steps {
                script {
                    if(params.JenkinsInstance == 'ChooseOne') {
                        error "请选择有效的团队"
                    }
                    def targetCredsId = "jenkins-${params.JenkinsInstance}-api-token"
                    
                    withCredentials([[$class: 'VaultUsernamePasswordCredentialBinding', 
                        credentialsId: targetCredsId, 
                        passwordVariable: 'JENKINS_USER_TOKEN', 
                        usernameVariable: 'JENKINS_USERNAME']]) {
                        sh '''
                        echo "Creating UsernamePassword credentials ID..."
                        src/jenkins/jenkins-build-agent/create-creds.sh
                        '''.stripIndent()
                    }
                }
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 03:24:50