Jenkins管道选择特定分支

eek*_*nky 5 linux git jenkins-pipeline

我有一个Jenkins管道,我想让用户输入以检查他们选择的特定分支。即,如果我创建一个分支'foo'并提交,我希望能够从菜单上建立该分支。由于有多个用户都在创建分支,因此我希望它位于声明性管道而不是GUI中。在下面显示的这个阶段,我希望用户输入在詹金斯(Jenkins)对git进行轮询以找出可用的分支之后检出该分支。这可能吗?

stage('Checkout') {
      checkout([$class: 'GitSCM',
                branches: [[name: '*/master']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [[$class: 'CloneOption', noTags: true, reference: '', shallow: true]],
                submoduleCfg: [],
                userRemoteConfigs: [[credentialsId: 'secretkeys', url: 'git@github.com:somekindofrepo']]
                ]);
    }
  }
Run Code Online (Sandbox Code Playgroud)

我目前有这个,但是还不漂亮。

pipeline {
    agent any
    stages {
        stage("Checkout") {
            steps {
                    checkout([$class: 'GitSCM',
                        branches: [
                            [name: '**']
                        ],
                        doGenerateSubmoduleConfigurations: false,
                        extensions: [[$class: 'LocalBranch', localBranch: "**"]],
                        submoduleCfg: [],
                        userRemoteConfigs: [
                            [credentialsId: 'repo.notification-sender', url: 'git@github.com:repo/notification-sender.git']
                        ]
                    ])
                }
            }
        stage("Branch To Build") {
            steps {
                    script {
                        def gitBranches = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref --all | sed s:origin/:: | sort -u')
                        env.BRANCH_TO_BUILD = input message: 'Please select a branch', ok: 'Continue',
                            parameters: [choice(name: 'BRANCH_TO_BUILD', choices: gitBranches, description: 'Select the branch to build?')]
                    }
                    git branch: "${env.BRANCH_TO_BUILD}", credentialsId: 'repo.notification-sender', url: 'git@github.com:repo/notification-sender.git'
                }
            }
          }
    post {
      always {
        echo 'Cleanup'
        cleanWs()
        }
    }
  }
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以将“带参数构建”与https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin一起使用,而不是将输入作为字符串。

通过使用该插件,您可以指示 Jenkins 从 GIT 存储库中获取所有可用的分支和标签。

使用参数 BRANCH_TO_BUILD 获取管道中的分支名称并签出所选分支。