如何使用管道从另一个作业中获取其他作业构建的git commit hash? 我正在寻找类似的东西:
Jenkins.instance.getAllItems(hudson.model.Job)
.filter { it.fullName.equals(params.theOtherJobNameSpecifiedViaParamByTheUser)[0]
.getBuild(params.buildNoSpecifiedViaParamByTheUser)
.getGitCommitHashOfTheSelectedBuild()
答案 0 :(得分:0)
您可以通过XML或JSON使用Jenkins的API。
首先获取您需要的网址:
XML: https://JENKINS-HOST/jenkins/job/JOB-NAME/job/JOB-BRANCH/BUILD-NUMBER/api/xml
JSON: https://JENKINS-HOST/jenkins/job/JOB-NAME/job/JOB-BRANCH/BUILD-NUMBER/api/json
然后解析输出。 我倾向于使用XPATH作为xml,也使用shell。
def generate_xpath_url (URL, BRANCH) {
branch = params.BRANCH_NAME
jenkins_job_url = params.OTHER_JENKINS_JOB_URL
remote_name = parse_git_remote_name(URL);
url = other_jenkins_job_url + 'lastSuccessfulBuild/api/xml?xpath=//refsremotes' + remote_name + branch + '/marked/SHA1'
return url
}
...
lastBuildCommitUrl = generate_xpath_url(env.GIT_URL, env.GIT_BRANCH)
lastBuildNumberUrl = env.JOB_URL + 'lastSuccessfulBuild/buildNumber'
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'SomeJenkinsNonUser',
usernameVariable: 'JENKINS_API_USER',
passwordVariable: 'JENKINS_API_PSW']])
{
lastBuildCommit = sh " curl -sLu ${JENKINS_API_USER}:${JENKINS_API_PSW} \"${lastBuildCommitUrl}\" | sed -e 's/<[^>]*>//g' """)
lastBuildNumber = sh([script:"curl -sLu ${JENKINS_API_USER}:${JENKINS_API_PSW} \"${lastBuildNumberUrl}\"", returnStdout: true]).trim();
}
上面的代码假设你可以获得存储库的git url,你关心的存储库的git分支,&#34;其他jenkins工作的工作URL&#34;你想要的,还有一个非userid,它有一个api令牌,可以用来从jenkins中访问jenkins api。