代码之家  ›  专栏  ›  技术社区  ›  Ramkumar Ramagopalan

如何从git repo生成groovy脚本列表文件?

  •  1
  • Ramkumar Ramagopalan  · 技术社区  · 7 年前

    这可能吗?如果可能,怎么可能?

    3 回复  |  直到 4 年前
        1
  •  0
  •   nradev    7 年前

    如果你打算使用管道,它会更容易。 您可以使用以下命令递归地从目录中获取文件:

    import groovy.io.FileType
    
    def fileList = []
    
    def dir = new File("your_repo_dir")
    dir.eachFileRecurse (FileType.FILES) { file ->
      fileList << file
    }
    

    然后在作业属性中,需要添加选项参数:

    choiceParam(name: 'Repo Files', choices: fileList.join("\n"), description: '')
    
        2
  •  0
  •   Romain DEQUIDT    5 年前

    将ActiveChoiceActiveParam与公共Git存储库一起使用:

    parameters {
        stringParam {
            name('BRANCH_NAME')
            defaultValue('master')
            description('git branche name')
            trim(true)
        }
        activeChoiceReactiveParam('PLAYBOOK') {
            description('Select a playbook')
            filterable()
            choiceType('SINGLE_SELECT')
            groovyScript {
                script("""
                    |def fileList = ['/bin/bash', '-c', "git clone --single-branch --branch " + BRANCH_NAME + " https://git.repository.com/scm/project/repo.git > /dev/null 2>&1 ; cd repo ; git ls-tree -r origin/" + BRANCH_NAME + " --name-only"].execute()
                    |fileList.waitFor()
                    |return ["playbook-default.yml"] + fileList.text.readLines().findAll { it.startsWith("playbook").endsWith(".yml") }
                    """.stripMargin())
                fallbackScript('return ["playbook-default.yml"]')
            }
            referencedParameter('BRANCH_NAME')
        }
    

        3
  •  0
  •   Romain DEQUIDT    4 年前

    如果要从HTTPS GIT URL+Jenkins凭据获取分支列表,请使用以下ActiveChoiceActiveParam:

    activeChoiceReactiveParam('BRANCH_NAME') {
            description('Branch from git repo')
            filterable()
            choiceType('SINGLE_SELECT')
            groovyScript {
                script("""
                    |credentialsId = '<TO-REPLACE-WITH-YOUR-CREDENTIAL-ID'
                    |def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class, jenkins.model.Jenkins.instance, null, null ).find{it.id == credentialsId}
                    |def url = 'https://' + creds.username + ':' + java.net.URLEncoder.encode(creds.password.getPlainText()) + '@bitbucket.url.com/scm/project/repo.git'
                    |def fileList = ['/bin/bash', '-c', 'rm -rf branch-name > /dev/null 2>&1 ; git clone ' + url + ' branch-name > /dev/null 2>&1 ; cd branch-name ; git for-each-ref --format="%(refname)" --sort -committerdate | sed "s|refs/[a-z]*/||g" | sed "s|origin/||g" '].execute()
                    |fileList.waitFor()
                    |return fileList.text.readLines()
                    |""".stripMargin())
                fallbackScript('return ["branch-name-not-found"]')
            }
        }