Reputation: 9181
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
, andprojectKey
from the source repository as variables, and then print out those variables as console output?**
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
}
}
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
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
Upvotes: 2
Views: 5095
Reputation: 4956
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}"
Upvotes: 2