gojenkins
gojenkins copied to clipboard
How to interact with multi-branch pipeline using gojenkins?
I am trying to work with multi-branch pipeline through gojenkins but how can we get details about them? As GetJob method only works on primary job name but however there can be many different jobs based on the branches in that multi-branch pipeline job.
I tried using getInnerJobs on the main pipeline job but no luck
if job.Raw.Name == "Build Application Service" {
//log.Info(string(jsonValue))
_, _ = job.GetPipelineRuns(ctx)
job1, _ := job.GetInnerJobs(ctx)
for _, test := range job1 {
print(test.Raw.Name)
}
//for _, pr := range prs {
// print(pr.Name)
//}
}
/cc @figo can you please help here 😕 ?
You would just need to pass in the job name correctly for the multibranch pipeline as below:
For below mutibranch pipeline the qualified jobname is "<multibranch_pipeline_name>/job/<branch_name>"
https://<jenkins_server_name>/job/<multibranch_pipeline_name>/job/<branch_name>/<build_id>
func main() {
httpClient := newDefaultHttpClient()
ctx := context.Background()
BaseUrl := "https://<jenkins_server_name>"
Username := <>
Password := <>
JobName := <multibranch_pipeline_name>/job/<branch_name>
jk := gojenkins.CreateJenkins(httpClient, BaseUrl, Username, Password)
job, err := jk.GetJob(ctx, JobName)
// process job
}
You can try the above for a quick test and verify. Hope this helps.