Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

git - Get the list of merged branch from github in jenkinsfile

I am trying to get the list of merged branch in my master branch by using git branch -a --merged master in jenkins. When i try locally i get all the branch name after doing git pill/ git fetch --all. the output shows locally:

git branch -a --merged master
    * master
      remotes/origin/master
      remotes/origin/devBranch/1.0.0
      remotes/origin/devBranch/1.1.0
      remotes/origin/devBranch/1.2.0

same thing when i do in jenkinsfile. it only shows the master branch.

jenkinsfile:

steps{
                withCredentials([usernamePassword(credentialsId: 'some_jenkins_cred_id', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
                    sh("""
                    git config --global credential.username $GIT_USERNAME
                    git config --global credential.helper "!echo password=$GIT_PASSWORD; echo"
                    git fetch --all
                    git pull origin master
                    git branch -a --merged master
                    """)
                }
        }

i get output:

          + git fetch --all
          Fetching origin
          + git pull origin master
          From https://github.optum.com/paymentintegrity/orbit-spike-ness-logging
          * branch            master     -> FETCH_HEAD
          + git branch -a --merged master
    ----> * master
            remotes/origin/master

Any way to get the list of branch in jenkinsfile?

Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The --all option to git fetch is useless here (there are cases where it does something useful, but you're not in one of those situations). Don't use it. It's not wrong but it doesn't help and will mislead you. This is a general rule for all users of Git: unless you can describe what --all means—beyond this single sentence from the git fetch documentation:

--all
?????Fetch all remotes.

—you should definitely not use the --all option. (Ask yourself: why did I use this option? If your answer is because I wanted all branches, note that --all never means "all branches".)

Now, the reason you're getting the results you get in Jenkins is because Jenkins has configured its own Git repository as a single-branch clone, and quite possibly also as a shallow clone. These are all options that you can set when you configure the Jenkins plugin. Setting up a single-branch clone makes the clone go faster (as does using a shallow clone of some specified depth). But it means that you literally can't get the information you wanted to get.

You'll need to choose: do you want to be able to use git branch -r --merged master,1 or do you want a faster clone? If you want to be able to inspect the remote-tracking names with git branch -r, stop using a single-branch clone: put the refspec (search the linked plugin page for the word refspec) back to the standard default:

The default refspec is +refs/heads/*:refs/remotes/REPOSITORYNAME/ where REPOSITORYNAME is the value you specify in the above repository "Name" field. The default refspec retrieves all branches. If a checkout only needs one branch, then a more restrictive refspec can reduce the data transfer from the remote repository to the agent workspace. For example, +refs/heads/master:refs/remotes/origin/master will retrieve only the master branch and nothing else.


1I used -r here because that's the option you really need, not -a.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...