0

My overall goal is to create a build parameter in Jenkins which is a drop down for only release branches. I have been at this for some time and know there are many ways to do this. Let me run through a few that I have attempted and reason why particular methods will not work for my scenario and why the one I am attempting to do is likely the most suited for my application.

My Current Attempt

Right now I have Active Choice Parameter. (Others such as Extensible Choice and Extended Choice are also fine), and it has my groovy script in there. Which works exactly how I want it to, except I have to put in plain text my username and password for git.

def getBranches = ("git ls-remote https://<username>:<password>@github.com/<org>/<repo>.git").execute()

def branchNameList = getBranches.text.readLines().collect {
  it.split()[1]
}

def releaseBranchList = branchNameList.findAll { it =~ /refs\/heads\/release-candidate-20.*/ }

def humanReadableReleaseBranchList = releaseBranchList.collect {
  it.replaceAll('refs/heads/', '')
}

return humanReadableReleaseBranchList

I know this is not the prettiest, I'm new to groovy.

What I've tried

I have of course tried without the <username>:<password> and it fails. I also have tried user secret text or files option in the Build Environment section in order to try and get the environment variables of the github credentials. But after much fighting with this and reading the parameter plugin docs (active choice, extended choice, etc), apparently they do not have access to these environment variables. Those are apparently only accessible once it hits the build step. Unless I am misunderstanding. Which I did try just to make sure, but was unsuccessful. Here is an example of how I tried to get them.

  • Under Build Environment I selected the "User secret text or file
  • Gave it a name and selected my github creds
  • Then in the Groovy Active Parameter script I did something like the following (FYI: I tried several methods, not just this one. If you know of another way please let me know)
def envVars = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars() 
def GITHUB_USER = envVars['GITHUB_USER']
def GITHUB_PASS = envVars['GITHUB_PASS']
def getBranches = ("git ls-remote https://GITHUB_USER:GITHUB_PASS@github.com/<org>/<repo>.git").execute()

Again, I tried many ways. Maybe my syntax was wrong, again I'm very new to Groovy. I did trying something like def getBranches = ("git ls-remote https://" + GITHUB_USER + ":" + GITHUB_PASS + "@github.com/<org>/<repo>.git").execute() and def getBranches = ("git ls-remote https://${GITHUB_USER}:${GITHUB_PASS}@github.com/<org>/<repo>.git").execute()

Why I chose this method

I know there are other approaches such as Jenkinsfile's for pipelines. I originally tried that. I did not have much success but I'm happy to try again if I can get past some things that were not working for me such as:

  • The git ls-remote repo is different than the repo which will container the Jenkinsfile. I could but I really don't want to put the Jenkinsfile in the repo I'm trying to porform the git options on because of the struggle of having to go through a ton of QA steps because it is application code. I can move much faster if I have my Jenkinsfile in a devops repo separate from the application code.
  • Adding the dynamic parameter did not always seem to update
  • I also tried using the github api, and for some reason, I got different branch results. Maybe I can attempt this again and figure out why, but I sunk a lot of time into this with no avail.

I just wanted to check if there is possibly another solution I havn't found before turning to the Jenkinsfile Pipeline Multirepo checkout approach.

  • The Use secret text(s) or file(s) under BuildEnvironment -> Bindings is the right way to introduce secrets to a Jenkins Build. You should be able to access these secrets via environmental vars in your Jenkins script. Were you unable to access these environmental vars? – datUser May 17 at 17:34
  • @datUser Yes, this is how I attempted to do this. I updated my question to hopefully clarify that. But I've ether failed at retrieving environment variables in the active choice parameter groovy script (see updates) or it is not available to these parameter plugins. Please note I have tried this with multiple parameter plugins. If you know how to do this, that would be perfect. – Byron Mansfield May 17 at 19:02
  • I think you are close, can you try println build.getBuildVariables().get('GITHUB_USER')? – datUser May 17 at 20:40
  • For troubleshooting purposes I don't think that println helps for groovy scripts in active choice parameters. It will and does work in the build step. Though when I put def test = build.getBuildVariables().get("GITHUB_USER"); return test in the active parameter groovy script, it comes back blank. I also tried def test = build.getBuildVariables() to see if I could get it to return the entire list of environment variables, also returns blank. Is build a built in variable? Do I need to define it or import it first? – Byron Mansfield May 20 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.