运行环境: go 1.19.1 windows/amd64
问题描述: go 调用jenkins api如何进行file类型参数
现有代码
```go
func BuildJob(fileName string,projectCode string){
//初始化邮箱
initMail()
// // 安全员邮箱账号设置
setSafeEmail(projectCode)
ctx := context.Background()
jenkins := gojenkins.CreateJenkins(nil, "http://" + jenkinsAddr, jenkinsUser, jenkinsPwd)
_, err := jenkins.Init(ctx)
if err != nil {
fmt.Println("********错误创建连接********")
return
}
fmt.Println("********创建jenkins连接成功********")
if jenkins != nil {
//如果参数完全一样,会当同一次构建
queueid, err := jenkins.BuildJob(ctx,jobName, map[string]string{"node":"dwglpt-zcy2","filename":fileName,"project_code":projectCode,"adminEmail":adminEmail,"adminUser":adminUser,"work_order_number":work_order_number})
if err != nil {
fmt.Println("********构建任务失败********")
return
}
fmt.Println("queueid:", queueid)
var buildNumber int64
//获取构建Num
for{
task,_ := jenkins.GetQueueItem(ctx,queueid)
if task.Raw.Executable.Number != 0 {
buildNumber = task.Raw.Executable.Number
break
}
time.Sleep(time.Second * 1)
}
fmt.Printf("buildNum:%v\n",buildNumber)
build, err := jenkins.GetBuild(ctx,jobName,buildNumber)
//正则规则,控制台去掉空白字符
pattern := regexp.MustCompile(`\s+`)
firstTip := true
//等待输入验证码
for {
if build.IsRunning(ctx) {
consoleOutput := pattern.ReplaceAllString(build.GetConsoleOutput(ctx),"")
time.Sleep(time.Second * 5)
if strings.HasSuffix(consoleOutput,"Inputrequested"){
//第一次输入验证码后后续输入验证码
if firstTip {
fmt.Println("###############请输入验证码###############")
firstTip = false
} else {
fmt.Println("###############验证码输入错误,请重新输入验证码###############")
}
fmt.Scanf("%s\n",&valiCode)
//调用接口进行构建
InvokRequest("POST","http://" + jenkinsUser + ":" + jenkinsToken + "@" + jenkinsAddr + "/job/dwglptyq/job/sc-cpjob/" + fmt.Sprintf("%v",buildNumber) + "/wfapi/inputSubmit?inputId=Inputap",valiCode)
}
}else {
break
}
}
//最终结果信息
fmt.Printf("********构建号: %d,构建结果: %v********\n", build.GetBuildNumber(), build.GetResult())
}
}
```
目前,传递的参数都是字符串类型,因为jenkins job的参数变化,需要将本地的文件通过file参数传到jenkins服务器,当前功能不能满足触发job的条件。
当前jenkins参数界面如图示
![image.png](https://static.golangjob.cn/230419/6e0ac63a5c510db69e5bea33c4d30d64.png)
对于红框中的文件参数,我的golang代码应该怎么调整。
有疑问加站长微信联系(非本文作者)