Get information about other branches from within a Jenkinsfile

做~自己de王妃 提交于 2019-12-23 17:11:24

问题


Jenkins Blue Ocean is running on the same local network as a linked Bitbucket Server instance. A multibranch project in Jenkins is able to create a branch for each branch of a linked Bitbucket repository on the local Bitbucket Server. But within the Jenkinsfile, I am not able to get information about the other branches.

What specific syntax needs to be added to the simplified Jenkinsfile below in order for the resulting console log to:
1. Print a complete list of all the branches in the remote repository, and
2. Filter the list to include only those branches whose titles contain a specific substring xyx?

This question asks about Bitbucket repository branch information within the Bitbucket object model, which should be accessible via the Bitbucket API or CLI.


Jenkinsfile

The simplified code fo the Jenkinsfile is as follows. Note that I have already tried a few approaches:

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

    try {
        stage ('Clone') {
            def repoUrl = checkout(scm).GIT_URL
            sh "echo 'The repo URL is: ${repoUrl}'"
            def branch = checkout(scm).GIT_BRANCH
            sh "echo 'The name of the branch containing the commit for this build is: ${branch}'"
            sh "git remote show"
            sh "git remote show origin"
        }
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}

The preceding Jenkinsfile is not isolating the intended information.


The Logs

The complete logs that result from running the preceding Jenkinsfile are as follows:

General SCM<1s
    Cloning the remote Git repository
    Cloning with configured refspecs honoured and without tags
    Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git init /var/jenkins_home/workspace/ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA # timeout=10
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git --version # timeout=10
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
     > git config --add remote.origin.fetch +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit # timeout=10
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    No valid HEAD. Skipping the resetting
     > git clean -fdx # timeout=10
    Fetching without tags
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
    Checking out Revision 146a9be9f1500df335614cc7d769d14e6de16649 (GWS-43-getissueforcommit)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 146a9be9f1500df335614cc7d769d14e6de16649
     > git branch -a -v --no-abbrev # timeout=10
     > git checkout -b GWS-43-getissueforcommit 146a9be9f1500df335614cc7d769d14e6de16649
    Commit message: "remove extraneous info"
     > git rev-list --no-walk ecb67a432da567cf2e6d91f031b8ceb2f59ed087 # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    Resetting working tree
     > git reset --hard # timeout=10
     > git clean -fdx # timeout=10
    [Bitbucket] Notifying commit build result

echo 'The repo URL is: http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git'— Shell Script<1s
    [ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + echo The repo URL is: http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
    The repo URL is: http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git

General SCM<1s
    > git rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    Resetting working tree
     > git reset --hard # timeout=10
     > git clean -fdx # timeout=10
    Fetching without tags
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git --version # timeout=10
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
    Checking out Revision 146a9be9f1500df335614cc7d769d14e6de16649 (GWS-43-getissueforcommit)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 146a9be9f1500df335614cc7d769d14e6de16649
     > git branch -a -v --no-abbrev # timeout=10
     > git branch -D GWS-43-getissueforcommit # timeout=10
     > git checkout -b GWS-43-getissueforcommit 146a9be9f1500df335614cc7d769d14e6de16649
    Commit message: "remove extraneous info"
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    Resetting working tree
     > git reset --hard # timeout=10
     > git clean -fdx # timeout=10
    [Bitbucket] Notifying commit build result

echo 'The name of the branch containing the commit for this build is: GWS-43-getissueforcommit'— Shell Script<1s
    [ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + echo The name of the branch containing the commit for this build is: GWS-43-getissueforcommit
    The name of the branch containing the commit for this build is: GWS-43-getissueforcommit

git remote show— Shell Script<1s
    [ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + git remote show
    origin

git remote show origin— Shell Script<1s
    [ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + git remote show origin
    fatal: could not read Username for 'http://<bitbucket-ip-on-lan>:7990': No such device or address
    script returned exit code 128

来源:https://stackoverflow.com/questions/50141718/get-information-about-other-branches-from-within-a-jenkinsfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!