Jenkins:使用Jenkinsfile从Git存储库阅读分支

xsuvu9jc  于 2023-03-28  发布在  Git
关注(0)|答案(1)|浏览(141)

我正在尝试修改一个现有的Jenkinsfile,以便在使用“Build with parameters”时,它正确显示Git仓库中当前存在的分支。截至目前,代码生成用于生成分支,环境和包列表的文本文件。我面临的问题是,已经从仓库中删除的旧分支仍然显示在下拉列表中,因为目前的脚本只增加了新的行。有人能给予我一些提示吗?
当前代码如下所示:

[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: '', 
            filterLength: 1, filterable: false, name: 'Branch', 
            randomName: 'choice-parameter-618718466038260', referencedParameters: 'Environment', 
            script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], 
                script: [classpath: [], sandbox: false, script: '''\
                    def name = []
                    def branchesPath = new File("/test_team/$Environment/Test_Team_Branches")
                    branchesPath.eachFile{ file->
                    if (file.isDirectory() && new File(file, ".git").exists()) {
                        name.add(file.toString().split('/')[-1])
                    }                   
                    return name
                    '''.stripIndent()
                ]
            ]
        ],
        [$class: 'DynamicReferenceParameter', choiceType: 'ET_FORMATTED_HIDDEN_HTML', description: 'Workspace where the testpacks are. Autogenereted, DO NOT MODIFY',
            name: 'Workspace', omitValueField: true, randomName: 'choice-parameter-3584144593172859', 
            referencedParameters: 'Environment,Branch', 
            script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], 
                script: [classpath: [], sandbox: true, script: '''\
                    file = new File("/test_team/$Environment/Test_Team_Branches/$Branch/auxBranchPackageFile_${Environment}_${Branch}.txt")
                    def workspace= file.readLines()[0].trim().split(\' \')[-1]
                    return "<input name=\\\"value\\\" value=\\\"${workspace}\\\" class=\\\"setting-input\\\" type=\\\"text\\\">"
                    '''.stripIndent()
                ]
            ]
        ], 
        [$class: 'CascadeChoiceParameter', choiceType: 'PT_CHECKBOX', description: '', 
            filterLength: 1, filterable: false, name: 'Packages', 
            randomName: 'choice-parameter-618718467816659', referencedParameters: 'Branch,Environment', 
            script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''], 
                script: [classpath: [], sandbox: false, script: '''\
                    def name=[]
                    file = new File("/test_team/$Environment/Test_Team_Branches/$Branch/auxBranchPackageFile_${Environment}_${Branch}.txt")
                    def workspace = file.readLines()[0].trim().split(\' \')[-1]
                    file.eachLine { line ->
                        if(line.contains("-P "+Environment+" "+workspace)){
                            line = line.replaceAll("-P "+Environment+" "+workspace,"")
                            line = line.replaceAll("/","")
                            if(!line.contains("ToolConfiguration")){
                                name.add(line.toString().trim())
                                }
                            }
                    }
                    return name
                    '''.stripIndent()
                ]
            ]
        ],

我尝试使用Git参数插件来填充下拉列表,而不是当前的Cascade Choice参数。但如果我的理解是正确的,以下参数-“environment”和“packages”依赖于使用“分支”创建的文件,打破了管道,因此下拉列表显示为空。我试图避免重写整个参数结构,但到目前为止我已经没有主意了。

fcg9iug3

fcg9iug31#

你可以使用GitHub API来填充下拉列表。看看下面的管道。你可能需要创建访问令牌来验证请求。

properties([
    parameters([
        [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Branch', 
            name: 'Bracnh',
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return [\'Could not get Branch\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        '''def token = 'xxxxxxxxxxx';
def content = new URL("https://api.github.com/repos/OWNER/REPO/branches").getText(useCaches: true, requestProperties: ['Aothorization': 'Bearer ' + token]);
def jsonSlurper = new groovy.json.JsonSlurper();
return jsonSlurper.parseText(content).name;'''
                ]
            ]
        ]
    ])
])
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    echo "Branch:::: ${params.Branch}"
                }
            }
        }
    }
}

相关问题