gojenkins icon indicating copy to clipboard operation
gojenkins copied to clipboard

How to interact with multi-branch pipeline using gojenkins?

Open kushthedude opened this issue 3 years ago • 3 comments

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.

kushthedude avatar Apr 07 '21 20:04 kushthedude

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)
			//}
		}

kushthedude avatar Apr 07 '21 20:04 kushthedude

/cc @figo can you please help here 😕 ?

kushthedude avatar Apr 07 '21 20:04 kushthedude

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.

ovisek avatar Apr 24 '21 13:04 ovisek