Jenkins Pipeline 选择特定的分支 [英] Jenkins Pipeline Choose Specific Branch

查看:37
本文介绍了Jenkins Pipeline 选择特定的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Jenkins 流水线,我希望有一个用户输入来检查他们选择的特定分支.即如果我创建一个分支'foo'并提交它,我希望能够从菜单中构建该分支.由于有几个用户都在创建分支,我希望它在声明性管道中而不是 GUI 中.在下面显示的这个阶段,我希望在 Jenkins 轮询 git 以找出可用的分支之后,用户输入来检查分支.这可能吗?

I have a Jenkins Pipeline that I would like to have a user input on to checkout a specific branch of their choosing. i.e. If I create a branch 'foo' and commit it, I'd like to be able to build on that branch from a menu. As there are several users all creating branches I want this to be in a declarative pipeline rather than GUI. At this stage shown below, I'd like a user input to checkout the branch after Jenkins has polled git to find out the branches available. Is this possible?

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

我目前有这个,但不漂亮;

I currently have this but it's not pretty;

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

推荐答案

你可以使用Build with Parameter"和 https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin .

Instead of taking the input as a string you can use "Build with Parameter" along with https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin .

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

By using the plugin you can instruct the Jenkins to fetch all the available branches, tags from GIT repository.

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

Get the branch name in the pipeline with the parameter BRANCH_TO_BUILD and checkout the chosen branch .

这篇关于Jenkins Pipeline 选择特定的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆