从 Jenkinsfile 获取源存储库的详细信息 [英] get details of source repository from Jenkinsfile
问题描述
Jenkinsfile 使用 checkout scm
命令从链接的 Bitbucket 存储库中检索最近的提交.
A Jenkinsfile uses a checkout scm
command to retrieve the most recent commit from a linked Bitbucket respository.
为了使 Jenkinsfile 能够从源存储库中提取 repositorySlug
和 projectKey
作为变量,需要向 Jenkinsfile 添加哪些特定语法,以及然后将这些变量打印为控制台输出?**
What specific syntax needs to be added to the Jenkinsfile in order for the Jenkinsfile to be able to extract the
repositorySlug
, andprojectKey
from the source repository as variables, and then print out those variables as console output?**
Jenkins 文件示例:
我尝试从 Jenkins Pipeline SCM Step Documentation 在以下示例 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
请注意,projectKey
和 repositorySlug
在上述日志中的格式为:
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
重述问题:
对于上面的数据,需要向 Jenkinsfile 添加什么特定语法才能使生成的 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屋!