首页 文章

来自SCM的Jenkin管道在不同的分支中不起作用

提问于
浏览
0

我正在使用Jenkins中的管道,该环境已经从SCM配置了管道脚本,然后使用groovy文件处理管道内的阶段/作业 . 这个脚本位于主分支的Bitbucket上 .

每次jenkins作业启动时都会调用master分支,它运行没有问题,并且管道的各个阶段都会运行 .

现在我在bitbucket上创建了一个新的分支,并修改了groovy文件以包含更多的步骤(比如运行单元测试和更多东西),我希望jenkins运行该脚本但是我指定的分支(我创建的那个) .

事情就是这个,尽管我在_1546144中指定了我的分支,jenkins仍然运行主分支 . 以下是我配置的一些图像 .
如何在SCM的管道脚本中指定我想要运行的分支?

enter image description here

enter image description here

Lightweight checkout support not available, falling back to full checkout.
Checking out git git@bitbucket.xxxxxx/xxxxxx.git into /data/jobs/extractor-pipeline-test-dev/workspace@script to read extractor-dev/Jenkinsfile
Cloning the remote Git repository
Cloning repository git@bitbucket.org:xxxxxx/xxxxxxxxxx.git
 > /usr/bin/git init /data/jobs/extractor-pipeline-test-dev/workspace@script # timeout=10
Fetching upstream changes from git@bitbucket.org:xxxx/xxxxxx.git
 > /usr/bin/git --version # timeout=10
 > /usr/bin/git fetch --tags --progress git@bitbucket.org:xxxxxx/deploy.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git config remote.origin.url git@bitbucket.org:xxxxx/deploy.git # timeout=10
 > /usr/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git config remote.origin.url git@bitbucket.org:xxxxxxx/deploy.git # timeout=10
Fetching upstream changes from git@bitbucket.org:xxxxx/deploy.git
 > /usr/bin/git fetch --tags --progress git@bitbucket.org:grydev/gp_deploy.git +refs/heads/*:refs/remotes/origin/*
**Seen branch in repository origin/DEVOPS-568-pipeline-ci
Seen branch in repository origin/dev
Seen branch in repository origin/master**
Seen 3 remote branches
 > /usr/bin/git tag -l # timeout=10
Checking out Revision e3270789a8181b26464f878bfccdf39b3fdabcb0 (master)
Commit message: " ....."
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f e3270789a8181b26464f878bfccdf39b3fdabcb0
 > /usr/bin/git rev-list e3270789a8181b26464f878bfccdf39b3fdabcb0 # timeout=10

这是groovy文件,但是groovy文件会执行将要部署的代码的步骤 . 它不运行任何jenkin脚本 . 其中“master”表示要部署的maser代码而不是部署脚本 .

Groovy文件:

def call(body) {

    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    def artifactName = 'imp'
    def artifactExt = '.war'
    def artifactVersion = '0.0.1'

    def buildPath = 'target/'
    def warFile = artifactName + '-' + artifactVersion + artifactExt
    def warPath = buildPath + warFile
    def warNoVersion = artifactName + artifactExt

    def deployPath = '/var/lib/tomcat8/webapps/'
    def deployFile = deployPath + warNoVersion

    node {
        // Clean workspace before doing anything
        //deleteDir()

        try {

            stage ('Code Checkout') {
                git branch: 'master',
                    credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                    url: 'ssh://git@bitbucket.org/xxxxx/xxxximporter'

enter image description here

enter image description here

enter image description here

1 回答

  • 2

    问题是即使Jenkinsfile来自所需的分支,代码检出也是通过"master"分支进行的 . The reason is code checkout is from branch "master" in "code checkout stage" . 更改代码如下:

    try {
        stage ('Code Checkout') {
            git branch: 'REQUIRED BRANCH',
                credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                url: 'ssh://git@bitbucket.org/xxxxx/xxxximporter'
    

    另一种更好的选择是提供 GIT BRANCH as parameter from the Jenkins job . 下面的快照 .

    enter image description here

    UPDATE: This can be achieved by installing the git parameter plugin.

    并添加以下co de snippet in your "code checkout" stage 并相应更改 . 这里 "gitbranch" is the parameter that you are passing from build .

    checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: gitbranch]], doGenerateSubmoduleConfigurations: false,    
    extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '********', url: '**********']]]
    

相关问题