从Jenkinsfile获取源存储库的详细信息 [英] get details of source repository from Jenkinsfile

查看:211
本文介绍了从Jenkinsfile获取源存储库的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A Jenkinsfile uses a checkout scm command to retrieve the most recent commit from a linked Bitbucket respository.

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

What specific syntax needs to be added to the Jenkinsfile in order for the Jenkinsfile to be able to extract the repositorySlug, and projectKey from the source repository as variables, and then print out those variables as console output?**


样本Jenkins文件:

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

I tried to incorporate ideas from the Jenkins Pipeline SCM Step Documentation in the following example Jenkinsfile whose resulting logs will be shown further below:

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时生成的控制台输出:

Here is the console output that Jenkins generates when running the preceding 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在以上日志中以以下格式提供:

Note that the projectKey and the repositorySlug are available in the logs above in the form of:

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


重定问题:

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

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

推荐答案

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

This should work, but there might be a simpler way I'm presently not aware of.

基本上,它会检索SCM插件返回的完整URL,并按/进行拆分,然后提取您需要的部分.

Basically, it retrieves the full URL returned by the SCM plugin, splits it by / and extracts the parts you need.

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

这篇关于从Jenkinsfile获取源存储库的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆