bndr / gojenkins

Jenkins API Client in Go. Looking for maintainers to move this project forward.
Apache License 2.0
863 stars 443 forks source link

How to interact with multi-branch pipeline using gojenkins? #236

Open kushthedude opened 3 years ago

kushthedude commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

/cc @figo can you please help here 😕 ?

ovisek commented 3 years ago

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 "/job/"

https:///job//job//

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.