当前位置: 首页 > 面试题库 >

从Jenkinsfile获取源存储库的详细信息

阳兴文
2023-03-14
问题内容

Jenkinsfile使用checkout scm命令从链接的Bitbucket存储库中检索最新提交。

为了使Jenkinsfile能够从源存储库中提取repositorySlugprojectKey作为变量,然后将这些变量作为控制台输出打印出来,需要向Jenkinsfile中添加什么特定的语法?**

样本Jenkinsfile:

我尝试将Jenkins Pipeline SCM步骤文档中的想法纳入以下示例Jenkinsfile中,其结果日志将在下面进一步显示:

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

    try {
        stage ('Clone') {
            def commitHash = checkout(scm).GIT_COMMIT
            sh "echo 'Commit hash is: ${commitHash}'"
            println commitHash

            def repName = checkout(scm).repoName
            sh "echo 'Repository Name is: ${repName}'"
            println repName

            def rep = checkout(scm).repo
            sh "echo 'Repository is: ${rep}'"
            println rep

            def nm = checkout(scm).name
            sh "echo 'Name is: ${nm}'"
            println nm
        }
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}

电流输出:

这是Jenkins在运行前面的Jenkinsfile时生成的控制台输出:

General SCM<1s
echo 'Commit hash is: bd279b90ad9f78ee8abb4d4fbf2a621d42150dd3'— Shell Script<1s
bd279b90ad9f78ee8abb4d4fbf2a621d42150dd3— Print Message<1s
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
    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 
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch
    Checking out Revision bd279b90ad9f78ee8abb4d4fbf2a621d42150dd3 (sample-issue-branch)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f bd279b90ad9f78ee8abb4d4fbf2a621d42150dd3
    Commit message: "name"
    [Bitbucket] Notifying commit build result
echo 'Repository Name is: null'— Shell Script<1s
null— Print Message<1s
General SCM<1s
echo 'Repository is: null'— Shell Script<1s
null— Print Message<1s
General SCM<1s
echo 'Name is: null'— Shell Script<1s
null— Print Message<1s

请注意,projectKeyrepositorySlug可以在上面的日志中以以下形式使用:

http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git

重述的问题:

对于上面的数据,需要将什么特定语法添加到Jenkins文件中,以使生成的Jenkins日志打印出以下内容:

The projectKey is: JSP
The repositorySlug is: jenkinsfile-simple-repo

问题答案:

这应该可以,但是我目前可能不知道有一种更简单的方法。

基本上,它检索SCM插件返回的完整URL,将其分割/并提取所需的部分。

def repoUrl = checkout(scm).GIT_URL
def key = repoUrl.tokenize('/')[3]
def slug = repoUrl.tokenize('/')[4]
slug = slug.substring(0, slug.lastIndexOf('.')) //Remove .git
echo "The projectKey is: ${key}"
echo "The repositorySlug is: ${slug}"


 类似资料:
  • 问题内容: 有没有人知道您是否可以在Maven存储库中找到源JAR? 问题答案: Maven Micro-Tip:获取源代码和Javadocs 在IDE中使用Maven时,经常会发现IDE需要解析库依赖项的源代码和Javadocs。有一个简单的方法可以实现该目标。 第一个命令将尝试下载pom文件中每个依赖项的源代码。 第二个命令将尝试下载Javadocs。 Maven受这里的图书馆打包人员的摆布。

  • 接口说明 获取角色详细信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 GET /permissions/api/team/role/v1.0.0/info 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 token string header 是 当前登录用户的TOKEN roleId string query 是

  • 接口说明 获取素材详细信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 GET /wish3dearth/api/material/v1.0.0/getMaterialDetail 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 serviceDomain string query 否 serviceDomain t

  • 接口说明 获取角色详细信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 GET /permissions/api/team/role/v1.0.

  • 接口说明 获取素材详细信息 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 GET /wish3dearth/api/material/v1.0.0

  • 在当前版本中,您不仅可以查看AssetBundle文件的包含资源信息和依赖关系信息,同时还可以查看每种资源的详细使用信息。目前,UWA资源检测服务对于主流资源的支持如下: 纹理资源 对于纹理资源,UWA可以提供以下重要信息:分辨率、资源格式、内存占用、Mipmap功能是否开启 和 Read/Write是否开启。这些均为影响纹理文件尺寸和内存占用的重要因素。因此,我们将其在此进行详细展示,您可以对每