系统相关
首页 > 系统相关> > linux – Jenkins Pipeline选择特定分支

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

解决方法:

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

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

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

标签:git,linux,jenkins-pipeline
来源: https://codeday.me/bug/20190622/1263188.html