bndr / gojenkins

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

通过queueId获取Build相关信息报404 #322

Open joker-bai opened 7 months ago

joker-bai commented 7 months ago

通过如下方式执行Jenkins Build,无法拿到具体的Build信息.

func job_build(jenkins *gojenkins.Jenkins,ctx context.Context,jobname string) {
    jobarr := strings.Split(jobname, "/")
    newjobname := strings.Join(jobarr,"/job/")
    qid,err  := jenkins.BuildJob(ctx,newjobname,nil)
    if err != nil {panic(err)}
    build, err := jenkins.GetBuildFromQueueID(ctx, qid)
    if err != nil {panic(err)}
    for build.IsRunning(ctx) {
        time.Sleep(5000 * time.Millisecond)
        build.Poll(ctx)
    }
    fmt.Printf("build number %d with result: %v\n", build.GetBuildNumber(), 
    build.GetResult())

gojenkins的版本是v1.1.0 jenkins的版本是2.4.36

ningblue commented 3 months ago

GetBuildFromQueueID函数 已经获取了build内容,可以直接使用

func (j *Jenkins) GetBuildFromQueueID(ctx context.Context, job *Job, queueid int64) (*Build, error) {
    task, err := j.GetQueueItem(ctx, queueid)
    if err != nil {
        return nil, err
    }
    // Jenkins queue API has about 4.7second quiet period
    for task.Raw.Executable.Number == 0 {
        time.Sleep(1000 * time.Millisecond)
        _, err = task.Poll(ctx)
        if err != nil {
            return nil, err
        }
    }

    build, err := job.GetBuild(ctx, task.Raw.Executable.Number)
    if err != nil {
        return nil, err
    }
    return build, nil
}