首页 > 解决方案 > 从 Jenkinsfile 中获取有关其他分支的信息

问题描述

Jenkins Blue Ocean 与链接的 Bitbucket 服务器实例在同一本地网络上运行。Jenkins 中的多分支项目能够为本地 Bitbucket 服务器上链接的 Bitbucket 存储库的每个分支创建一个分支。但在 Jenkinsfile 中,我无法获得有关其他分支的信息。

Jenkinsfile为了生成控制台日志,需要将哪些特定语法添加到以下简化版
中: 1. 打印远程存储库中所有分支的完整列表,以及
2. 过滤列表以仅包含标题包含特定的子串xyx

此问题询问有关 Bitbucket 对象模型中的 Bitbucket 存储库分支信息,这些信息应该可以通过 Bitbucket API 或 CLI 访问。


詹金斯文件

的简化代码Jenkinsfile如下。请注意,我已经尝试了几种方法:

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
    }
}

前面Jenkinsfile没有隔离预期的信息。


日志

运行上述程序产生的完整日志Jenkinsfile如下:

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

标签: gitjenkinscontinuous-integrationbitbucketjenkins-pipeline

解决方案


推荐阅读