从Jenkinsfile获取源存储库的详细信息
Jenkinsfile使用checkout scm
命令从链接的Bitbucket存储库中检索最新提交。
为了使Jenkinsfile能够从源存储库中提取
repositorySlug
和projectKey
作为变量,然后将这些变量作为控制台输出打印出来,需要向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<1secho '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
可以在上面的日志中以以下形式使用:
http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
The projectKey is: JSPThe repositorySlug is: jenkinsfile-simple-repo
回答:
这应该可以,但是我目前可能不知道有一种更简单的方法。
基本上,它检索SCM插件返回的完整URL,将其分割/
并提取所需的部分。
def repoUrl = checkout(scm).GIT_URLdef 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获取源存储库的详细信息 的全部内容, 来源链接: utcz.com/qa/411812.html