Skip to content

Instantly share code, notes, and snippets.

@michaelsauter
Last active May 20, 2020 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelsauter/16bd497a2052b014f31fb40da8ad33d7 to your computer and use it in GitHub Desktop.
Save michaelsauter/16bd497a2052b014f31fb40da8ad33d7 to your computer and use it in GitHub Desktop.
import org.ods.service.ServiceRegistry
import org.ods.util.GitUtil
import org.ods.util.Project
def runOnAgentPod(Project project, boolean condition, Closure block) {
if (condition) {
def git = ServiceRegistry.instance.get(GitUtil)
dir(env.WORKSPACE) {
stash(name: 'wholeWorkspace', includes: '**/*,**/.git', useDefaultExcludes: false)
}
def bitbucketHost = env.BITBUCKET_HOST
def podLabel = "mro-jenkins-agent-${env.BUILD_NUMBER}"
node(podLabel) {
git.configureUser()
unstash("wholeWorkspace")
withCredentials([usernamePassword(credentialsId: project.services.bitbucket.credentials.id, usernameVariable: 'BITBUCKET_USER', passwordVariable: 'BITBUCKET_PW')]) {
def urlWithCredentials = "https://${java.net.URLEncoder.encode(env.BITBUCKET_USER, "UTF-8")}:${java.net.URLEncoder.encode(env.BITBUCKET_PW, "UTF-8")}@${bitbucketHost}"
writeFile(file: "${env.HOME}/.git-credentials", text: urlWithCredentials)
sh(script: "git config --global credential.helper store", label : "setup credential helper")
}
block()
}
} else {
block()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment