Jenkins Pipeline选择特定分支

时间:2018-01-23 14:23:04

标签: 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']]
                ]);
    }
  }

我现在有这个,但它并不漂亮;

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()
        }
    }
  }

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:0)

这是一个没有任何插件的解决方案 使用詹金斯 2.249.2, 和声明性管道:

以下模式通过动态下拉菜单提示用户(让他选择一个分支):

(周围的 withCredentials 块是可选的,仅当您的脚本和 jenkins configuratoin 确实使用凭据时才需要)

节点{

withCredentials([[$class: 'UsernamePasswordMultiBinding',
              credentialsId: 'user-credential-in-gitlab',
              usernameVariable: 'GIT_USERNAME',
              passwordVariable: 'GITSERVER_ACCESS_TOKEN']]) {
    BRANCH_NAMES = sh (script: 'git ls-remote -h https://${GIT_USERNAME}:${GITSERVER_ACCESS_TOKEN}@dns.name/gitlab/PROJS/PROJ.git | sed \'s/\\(.*\\)\\/\\(.*\\)/\\2/\' ', returnStdout:true).trim()
}

} 管道{

agent any

parameters {
    choice(
        name: 'BranchName',
        choices: "${BRANCH_NAMES}",
        description: 'to refresh the list, go to configure, disable "this build has parameters", launch build (without parameters)to reload the list and stop it, then launch it again (with parameters)'
    )
}

stages {
    stage("Run Tests") {
        steps {
            sh "echo SUCCESS on ${BranchName}"
        }
    }
}

}

缺点是应该刷新 jenkins 配置并使用空白运行来使用脚本刷新列表... 解决方案(不是我的):使用用于专门刷新值的附加参数可以减少此限制的烦恼:

parameters {
        booleanParam(name: 'REFRESH_BRANCHES', defaultValue: false, description: 'refresh BRANCH_NAMES branch list and launch no step')
        choice(
          name: 'BranchName',
          choices: "${BRANCH_NAMES}",
          description: 'choose a branch (ignored if refresh_branches is selected)'
    )
}

然后在舞台上:

stage('a stage') {
   when {
      expression { 
         return ! params.REFRESH_BRANCHES.toBoolean()
      }
   }
   ...
}