I've a monorepo with many submodules,everytime a PR is raised on my monrepo to update hashes of the submodule, i need to run a jenkins job to see if the hashes in the source branch of PR are in remote of the submdule.
This to avoid failure while building sub modules(because sometimes people fail to commit their changes to remote, but still update hash)
Thank you in advance
git clone <url> --branch $PR_SOURCE_BRANCH --single-branch <somefoldername>
after this??
As suggested by the gentlemen @joanis in the comments, i shifted my focus from git commands to bitbucket rest APIs, and response is quick
node {
//SCM checkout
stage('Perform Hash check') {
cleanWs()
sh "git clone <url> --branch $PR_SOURCE_BRANCH --single-branch <somefoldername>"
dir(<somefoldername>) {
def map = [:]
def commitList = sh( script:"(git submodule status | awk '{ print \$1 \" \" \$2 }')",returnStdout: true).replace("-","").split("\n")
commitList.each {
map[it.split(" ")[0].trim()] = it.split(" ")[1].trim()
}
map.each { commitid, reponame ->
def url = "https://api.bitbucket.org/2.0/repositories/reposlug/$reponame/commits/$commitid"
final def (String response, int code) = sh(script: "curl -s -w '\\n%{response_code}' -u username:app-password $url", returnStdout: true).trim().tokenize("\n")
if (code != 200) {
error()
}
}
}
}
}
Promoting my comment to an answer, since the solution worked.
Doing some research, I could not find a Git command to do what you want, but if your Git server includes a repo browsing interface, like GitHub does, trying to fetch
https://github.com/<space>/<repo>/commit/<hash>
will give you a 200 on success, 404 on failure and thus tell you if the commit exists.
Since this page only points to an index of the files in the commit, but does not include any actual contents, this should be a very efficient check.