3
votes

I'm working with pipelines in Jenkins in a environment which already has configured a pipeline script from SCM which then uses a groovy file for the stages/jobs inside the pipeline. This scripts are on Bitbucket in a master branch.

Every time jenkins jobs starts it call master branch and it runs with no problem and the stages of the pipeline are run.

Now I created a new branch on bitbucket and modified the groovy file to include some more steps (like running unit tests and some more stuff) and I would like that jenkins runs that script but with the branch I specified (the one I created).

Thing is that eventhough I specified my branch in in "branch specifier", jenkins still runs the master branch. Here are some images of what I have configured.
How can I specify the branch I want to be run on the pipeline script from SCM?

enter image description here enter image description here

Lightweight checkout support not available, falling back to full checkout.
Checking out git [email protected]/xxxxxx.git into /data/jobs/extractor-pipeline-test-dev/workspace@script to read extractor-dev/Jenkinsfile
Cloning the remote Git repository
Cloning repository [email protected]:xxxxxx/xxxxxxxxxx.git
 > /usr/bin/git init /data/jobs/extractor-pipeline-test-dev/workspace@script # timeout=10
Fetching upstream changes from [email protected]:xxxx/xxxxxx.git
 > /usr/bin/git --version # timeout=10
 > /usr/bin/git fetch --tags --progress [email protected]:xxxxxx/deploy.git +refs/heads/*:refs/remotes/origin/*
 > /usr/bin/git config remote.origin.url [email protected]: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 [email protected]:xxxxxxx/deploy.git # timeout=10
Fetching upstream changes from [email protected]:xxxxx/deploy.git
 > /usr/bin/git fetch --tags --progress [email protected]: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

This is the groovy file, but groovy files does steps of the code that will be deployed. It doesn't run any jenkin script. Where it says "master" is of maser code to be deploy and not the deploy script.

Groovy file:

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://[email protected]/xxxxx/xxxximporter'

enter image description here

enter image description here

enter image description here

4
everything looks fine...have you verified the commit id in the console output ?...can you share the console output by hiding the confidential information..Here_2_learn
there I edited it and add that. 3 branches are seen, but master is run. I want that job run my branch "DEVOPS-568-pipeline-ci"Chanafot
I include the part where starts running the master branch job. then job run successfully an pipeline ends ok but only wit master branchChanafot
can you share the pipeline script where the "checkout scm" is happening ?Here_2_learn
the script only contains this: @Library("gp-extractor-deploy") _ DeployExtractor { confiFileId = "extractor-conf.properties" serverIp = 'x.x.x.x' slackChannel = '#xxxxx' } which call a groovy file in which all jobs/steps are runChanafot

4 Answers

3
votes

The issue is even though the Jenkinsfile is from the required branch, code checkout is happening through "master" branch. The reason is code checkout is from branch "master" in "code checkout stage". Change the code as follows:

try {
    stage ('Code Checkout') {
        git branch: 'REQUIRED BRANCH',
            credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            url: 'ssh://[email protected]/xxxxx/xxxximporter'

An alternative and rather better option is to provide GIT BRANCH as parameter from the Jenkins job. Snapshot below.

enter image description here

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

and add the following code snippet in your "code checkout" stage and change accordingly. Here "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: '**********']]]
1
votes

For me, no one way mentioned above was helpful.

It seems that Git parameter plugin passes branch as 'origin/<...>' which does not transform to a valid refspec according to log:

> git fetch --tags --force --progress -- ssh://git@***************/group/repo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse "refs/remotes/origin/origin/devel^{commit}" # timeout=10
> git rev-parse "refs/remotes/origin/refs/heads/origin/devel^{commit}" # timeout=10
> git rev-parse "refs/heads/origin/devel^{commit}" # timeout=10

I was not able to use Jenkinsfile with "refs/tags/${TAG}".

Also, when "Lightweight checkout" is unchecked, job always fails with '<...>@script\Jenkinsfile not found'.

0
votes

I have the exactly same issue here, and tried many times. It turned out that the Jenkinsfile specified in script path, is only obtained from git default url, never use the branch we specified in Branch to build.

To avoid this, I have to use multiple branch pipeline....

0
votes

In the 'Branch specifier' click on the 'add branch' and put your selected branch again. This worked for me even with an environment variable.