get details of source repository from Jenkinsfile

匿名 (未验证) 提交于 2019-12-03 01:09:02

问题:

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

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?**


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


Current Output:

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

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


Restated Question:

For the data above, what specific syntax would need to be added to the Jenkinsfile in order for the resulting Jenkins logs to print out the following:

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

回答1:

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!