Skip to content

Instantly share code, notes, and snippets.

@jseed
Created June 19, 2017 15:14
Show Gist options
  • Save jseed/ac0218e86c88751942c847b10637bb56 to your computer and use it in GitHub Desktop.
Save jseed/ac0218e86c88751942c847b10637bb56 to your computer and use it in GitHub Desktop.
Groovy script for dynamically listing git repository branches as a jenkins build parameter using the extended choice parameter plugin
/*
* Variable bindings:
* user - credentials plugin user
* url - git repository url (.git)
*/
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import jenkins.model.Jenkins
def creds = CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class,
Jenkins.instance
);
def c = creds.findResult { it.username == user ? it : null }
def pass = c.password;
def repo = "http://" + user + ":" + pass + "@" + url;
return ["/bin/bash", "-c", "git ls-remote -h " + repo + " | sed 's/.*refs\\/heads\\/\\(.*\\)/\\1/'"].execute().text.tokenize();
@kota65535
Copy link

you saved me 3 hours!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment