【问题标题】:Jenkins Pipeline Choose Specific BranchJenkins Pipeline 选择特定的分支
【发布时间】:2018-01-23 14:23:03
【问题描述】:

我有一个 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()
        }
    }
  }

【问题讨论】:

    标签: linux git jenkins-pipeline


    【解决方案1】:

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

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

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

    【讨论】:

    • 如果您使用管道并希望在分支结帐中指定为变量,则必须在 Pipeline SCM 部分取消选择轻量级结帐,否则会出错。
    【解决方案2】:

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

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

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

    节点{

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2016-08-15
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多