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

如何不运行Jenkins流水线即可更新Gitlab仓库目录对应的选择参数列表

解决方案

首先明确核心问题:你当前的静态choice参数是在Jenkinsfile解析、流水线执行完成后才会更新选项,因此必须提前运行一次流水线才能刷新参数列表,无法做到手动执行时实时拉取最新目录。

你可以通过以下两种方案实现需求:


方案1:使用Active Choices插件(推荐)

这是体验最优的方案,无需提前运行流水线,每次点击「构建」按钮时会实时拉取Gitlab最新目录作为选项。

操作步骤:

  1. 先在Jenkins插件管理中安装「Active Choices」插件并重启
  2. 替换原有静态choice参数为Active Choices Reactive Parameter,参数配置如下:
    • 名称:BACKUP
    • 描述:Selecciona el backup para realizar el Rollback
    • 脚本类型:Groovy脚本
    • 脚本内容参考(替换成你自己的配置即可):
    import jenkins.model.Jenkins
    import groovy.json.JsonSlurper
    
    // 基础配置替换为你的实际信息
    def gitlabUrl = "https://你的Gitlab域名/api/v4"
    def projectId = "你的Gitlab项目ID"
    def branch = "仓库分支名,如main"
    def targetDirPath = "" // 要读取的目录路径,根目录留空即可
    def gitlabCredId = "你存放在Jenkins的Gitlab访问令牌凭证ID"
    
    // 拉取Gitlab仓库目录列表
    def gitlabToken = Jenkins.instance.getCredential(gitlabCredId).getSecret()
    def apiUrl = "${gitlabUrl}/projects/${projectId}/repository/tree?ref=${branch}&path=${targetDirPath}&per_page=100"
    def conn = new URL(apiUrl).openConnection()
    conn.setRequestProperty("PRIVATE-TOKEN", gitlabToken)
    def resp = new JsonSlurper().parse(conn.getInputStream())
    
    // 过滤出所有目录并排序返回
    return resp.findAll { it.type == "dir" }.collect { it.name }.sort()
    
    • 勾选「构建时动态计算参数」选项
  3. 保存配置即可

注意事项:

如果运行时报脚本权限错误,到「Manage Jenkins」->「In-process Script Approval」中审批对应的方法即可。


方案2:使用input动态步骤(无需插件)

如果无法安装第三方插件,可以将目录选择逻辑放到流水线第一个阶段的input步骤中,点击执行后会实时拉取代码生成选项,无需提前运行流水线刷新参数。

改造后的完整流水线示例:

pipeline {
    agent any
    stages {
        stage('选择回滚备份') {
            steps {
                script {
                    checkout scm
                    // 实时读取当前仓库最新目录列表
                    def backupList = sh(returnStdout: true, script: "ls $WORKSPACE").split().sort()
                    // 动态生成选择框
                    env.SELECTED_BACKUP = input(
                        message: '请选择要回滚的备份',
                        parameters: [
                            choice(name: 'BACKUP', choices: backupList, description: 'Selecciona el backup para realizar el Rollback')
                        ]
                    )
                }
            }
        }
        stage('Realizando el Rollback con el Backup seleccionado') {
            steps {
                script {
                    sh "echo Se realiza el Rollback con el Backup $env.SELECTED_BACKUP"
                    withCredentials([string(credentialsId: 'ID', variable: 'VARIABLE')]) {
                        sh "script.sh $env.SELECTED_BACKUP"
                    }
                }
            }
        }
    }
    post {
        always {
            script {
                sh 'rm -rf *'
            }
        }
    }
}

方案说明:

该方案不需要安装额外插件,兼容性更强,但选择框会在你点击执行流水线、拉取完代码后才弹出,和方案1的「点击构建时直接看到选项」的体验略有差异。


内容的提问来源于stack exchange,提问作者Salvador Arreola Rojas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 23:36:02