From b4fec007f8ee17e62abcc669227abe9dddf6cb67 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sun, 24 Dec 2017 10:49:20 +0100 Subject: [PATCH 1/3] Use master as fallback for non-existent branches When a PR is made directly from an upstream branch or to a different branch than `master` or `stable`, the respective branch at the other two repositories might not exist. So let's better check beforehand and softly fallback to `master` otherwise. The same procedure is e.g. used for CircleCi. --- vars/runPipeline.groovy | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/vars/runPipeline.groovy b/vars/runPipeline.groovy index c497d933..f525a6fc 100644 --- a/vars/runPipeline.groovy +++ b/vars/runPipeline.groovy @@ -64,6 +64,13 @@ def cloneLatestTag (repo_url, filter = '') { sh "git checkout ${LATEST}" } +/** + Checks whether a specific branch exists at a remote git repository. +*/ +def doesBranchExist(repo_url, branch) { + "git ls-remote --exit-code --heads ${repo_url} ${branch} > /dev/null".execute().exitValue == 0 +} + /** Utility to simplify repeating boilerplate of defining parallel steps over array of folders. Creates a map from @names array where each value @@ -118,7 +125,11 @@ def getSources (name) { dir(name) { // Checkout matching branches of other repos, either // target branch for PRs or identical branch name. def base_branch = env.CHANGE_TARGET ?: env.BRANCH_NAME - clone("https://github.com/dlang/${name}.git", base_branch) + def repo_url = "https://github.com/dlang/${name}.git" + if (doesBranchExist(repo_url, base_branch)) { + base_branch = "master" + } + clone(repo_url, base_branch) } }} From 0c48d04f909173c63be8a0f2bf0e4f732126ff59 Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Tue, 26 Dec 2017 16:06:17 +0100 Subject: [PATCH 2/3] fixed to use sh step instead of groovy's native execute --- vars/runPipeline.groovy | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/vars/runPipeline.groovy b/vars/runPipeline.groovy index f525a6fc..a00b4898 100644 --- a/vars/runPipeline.groovy +++ b/vars/runPipeline.groovy @@ -67,8 +67,12 @@ def cloneLatestTag (repo_url, filter = '') { /** Checks whether a specific branch exists at a remote git repository. */ -def doesBranchExist(repo_url, branch) { - "git ls-remote --exit-code --heads ${repo_url} ${branch} > /dev/null".execute().exitValue == 0 +def branchExists (repo_url, branch) { + def status = sh( + script: "git ls-remote --exit-code --heads ${repo_url} ${branch} > /dev/null", + returnStatus: true + ) + return status == 0 } /** @@ -126,9 +130,9 @@ def getSources (name) { dir(name) { // target branch for PRs or identical branch name. def base_branch = env.CHANGE_TARGET ?: env.BRANCH_NAME def repo_url = "https://github.com/dlang/${name}.git" - if (doesBranchExist(repo_url, base_branch)) { - base_branch = "master" - } + if (!branchExists(repo_url, base_branch)) { + base_branch = 'master' + } clone(repo_url, base_branch) } }} From 36848f6ce3e19400a37ea6c6167e201be2279c0f Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Tue, 26 Dec 2017 16:16:38 +0100 Subject: [PATCH 3/3] determine latest tag before checkout - fixes changelog display in jenkins --- vars/runPipeline.groovy | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/vars/runPipeline.groovy b/vars/runPipeline.groovy index a00b4898..9b8f659a 100644 --- a/vars/runPipeline.groovy +++ b/vars/runPipeline.groovy @@ -41,27 +41,24 @@ def cloneUpstream () { ]) } -/** - Checks out latest SemVer-compatible release tag available in specified repo. - Tags can be further filtered by passing a regex. +/** Checks out latest SemVer-compatible release tag available in specified repo, + or master if no tag can be found. **/ -def cloneLatestTag (repo_url, filter = '') { +def cloneLatestTag (repo_url) { + def LATEST = sh( + script: "git ls-remote --tags ${repo_url} | sed -n 's|.*refs/tags/\\(v\\?[0-9]*\\.[0-9]*\\.[0-9]*\$\\)|\\1|p' | sort --version-sort | tail -n 1", + returnStdout: true + ).trim() + checkout( poll: false, scm: [ $class: 'GitSCM', - branches: [[name: "master"]], + branches: [[name: LATEST ?: 'master']], extensions: [[$class: 'CleanBeforeCheckout']], userRemoteConfigs: [[url: repo_url]] ] ) - - def LATEST = sh ( - script: "git tag -l | egrep '^v?[0-9]+\\.[0-9]+\\.[0-9]+\$' | egrep '${filter}' | sort --version-sort | tail -n 1", - returnStdout: true - ).trim() - - sh "git checkout ${LATEST}" } /**