1

A member of our development team has written a function to use in our automation process, but he has since left. We are now trying to use it but I have little guidance in the realm of Jenkins.

The code our mystery dev provided:

function RunJenkinsBuild ($jenkinsHost, $jobName, $jobParams) {
$url = "$jenkinsUri/job/$($jobName)/api/json"
$job = Invoke-JenkinsCommand -uri $url -Command lastBuild
$nextBuildID = $job.nextBuildNumber
Invoke-JenkinsJob -Uri $jenkinsUri -Name $jobName -Parameters $jobParams
$url = "$jenkinsUri/job/$($jobName)/$($nextBuildID)/api/json"

The code I've tried to get to work

RunJenkinsBuild "jenkins:8080", "SetTag(Rollback)", "BRANCHVAR=$latestTag"

Basically, I'm trying to get the whole "BRANCHVAR=$latestTag" to work, but it doesn't pass in correctly.

Any help would be appreciated! Thanks!

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • Your 'code' seems incomplete? Is there some more you've not included, as at present the function isn't closed so it won't run. – henrycarteruk Dec 14 '18 at 16:42
  • 2
    The way you're running the function is incorrect, Powershell doesn't use commas in this way. it should be called either: 1) using positional parameters: `RunJenkinsBuild "jenkins:8080" "SetTag(Rollback)" "BRANCHVAR=$latestTag"` or 2) using named params `RunJenkinsBuild -jenkinsHost "jenkins:8080" -jobName "SetTag(Rollback)" -jobParams "BRANCHVAR=$latestTag"` – henrycarteruk Dec 14 '18 at 16:47
  • @JamesC. has it right. Check out the linked question which goes into detail about calling functions in PowerShell. It's easy to miss so it tends to be a common stumbling block when starting out with PowerShell, and it's not really obvious why things aren't working. Pay special attention to [Michael Soren's answer](https://stackoverflow.com/a/15883080/3905079) on that question, which covers using [`Set-StrictMode`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode?view=powershell-6) as a way to catch this type of error. Also welcome to SO! – briantist Dec 14 '18 at 16:53
  • @JamesC. there is proprietary code below the actual function, I didn't include the whole thing, probably should have included that, thank you for pointing it out though! – Justin Ferguson Dec 14 '18 at 16:53
  • @JamesC. but of your suggestions make sense, however I am getting an "invalid URI, hostname could not be parsed" – Justin Ferguson Dec 14 '18 at 16:56
  • 1
    @JustinFerguson you need to provide a protocol if you want to also use a port, like `http://jenkins:8080`. – briantist Dec 14 '18 at 16:59
  • @briantist good call. I just caught that as well. So we get down the the root of the problem now.. the problem lies in passing in the "BRANCHVAR=$latestTag" option, I have to do some special caseing as well as syntax, because this doesn't resolve correctly. I seem to remember reading something about 'object literal' – Justin Ferguson Dec 14 '18 at 17:06
  • The error that comes through: `Invoke-JenkinsJob : Cannot process argument transformation on parameter 'Parameters'. Cannot convert the "BRANCHVAR=1111-11-11" value of type "System.String" to type "System.Collections.Hashtable".` – Justin Ferguson Dec 14 '18 at 17:11
  • Rather than quoting it as a string, you just need to use syntax to make it a hashtable instead: `-jobParams @{BRANCHVAR=$latestTag}` See [about_hash_tables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables) for further details :) – henrycarteruk Dec 14 '18 at 17:29
  • @JamesC. that worked!! thank you so much! – Justin Ferguson Dec 14 '18 at 22:42

0 Answers0