Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get current build number? #70

Open
Orocker opened this issue Oct 9, 2023 · 11 comments
Open

How to get current build number? #70

Orocker opened this issue Oct 9, 2023 · 11 comments

Comments

@Orocker
Copy link

Orocker commented Oct 9, 2023

No description provided.

@LinuxSuRen
Copy link
Member

Actually, there is no way to get the CURRENT build number. Instead we could get the last build number.

@Orocker
Copy link
Author

Orocker commented Oct 9, 2023

Actually, there is no way to get the CURRENT build number. Instead we could get the last build number.

Do you mean that after I create a job using BuildJobWithParams, I then use GetJob to get the last id of Builds[]Build as the build number of the current jobid?

@LinuxSuRen
Copy link
Member

It's true if there is only one build request in the short time.

By the way, Jenkins will create a queue job, then there is a worker will consume that queue item. So, we cannot get the build ID after the build request. Because it might does not have the ID yet in sometime.

@Orocker
Copy link
Author

Orocker commented Oct 9, 2023

Thank you, is there any way I can customize the job number?
this is my code:

func BuildJobWithParams(params []job.ParameterDefinition) (result bool, err error) {
	jenkins, err := GetJenkinsCore()
	if err != nil {
		return false, err
	}
	jobClient := job.Client{JenkinsCore: jenkins}

	err = jobClient.BuildWithParams(PHPMigrateScript, params)
	if err != nil {
		return false, err
	}
	result = true
	return
}

params := []job.ParameterDefinition{
		{
			Name:        "IMAGE_TAG",
			Value:       "v1.4.251",
			Description: "Docker image and tag to use",
		},
		{
			Name:        "ENV",
			Value:       "pre",
			Description: "prod or pre",
		},
		{
			Name:        "PROJECT",
			Value:       "xthk-financial-management-system",
			Description: "project name",
		},
		{
			Name:        "COMMAND",
			Value:       "php artisan  cancel_class_export test",
			Description: "command",
		},
	}

	result, err := utils.BuildJobWithParams(params)

I found that ParameterDefinition.DefaultParameterValue have a Number field

@Orocker Orocker closed this as completed Oct 9, 2023
@Orocker Orocker reopened this Oct 9, 2023
@LinuxSuRen
Copy link
Member

As far as I know, the job build number can only be generated by Jenkins itself.

@Orocker
Copy link
Author

Orocker commented Oct 9, 2023

As far as I know, the job build number can only be generated by Jenkins itself.

If it cannot be customized, then I can only add a unique identifier to distinguish it.

@LinuxSuRen
Copy link
Member

add a unique identifier to distinguish it

This is a good idea I believe.

@Orocker Orocker closed this as completed Oct 9, 2023
@Orocker Orocker reopened this Oct 9, 2023
@Orocker
Copy link
Author

Orocker commented Oct 10, 2023

hello, @LinuxSuRen

func BuildJobWithParams(params []job.ParameterDefinition) (build *job.Build, err error) {
	jenkins, err := GetJenkinsCore()
	if err != nil {
		return nil, err
	}
	jobClient := job.Client{JenkinsCore: jenkins}
	err = jobClient.BuildWithParams(PHPMigrateScript, params)
	if err != nil {
		return nil, err
	}
	build, err = jobClient.GetBuild(PHPMigrateScript, -1)

	return
}

2023/10/10 11:16:06 &{SimpleJobBuild:{Number:2931 URL:https://example.com/job/php-migrate-script/2931/} Building:true Description: DisplayName:#2931 Duration:0 EstimatedDuration:42531 FullDisplayName:php-migrate-script #2931 ID:2931 KeepLog:false QueueID:5057 Result: Timestamp:1696907748017 PreviousBuild:{Number:2930 URL:https://exmaple.com:443/job/php-migrate-script/2930/} NextBuild:{Number:0 URL:}}

problem:
The build number I am currently building is 2932, but the build number returned by jobClient.GetBuild(PHPMigrateScript, -1) is 2931, but when I use the browser to visit example.com/job/php-migrate-script/lastBuild/api/ json, the returned jobnumber is 2932, the browser result is correct, and the task is still under construction at this time.

@LinuxSuRen
Copy link
Member

It might be a bug if you only triggered the job build once.

@Orocker
Copy link
Author

Orocker commented Oct 11, 2023

It might be a bug if you only triggered the job build once.

Yes, I checked to make sure it was only executed once

@LinuxSuRen
Copy link
Member

Please feel free to fix this if you're interested in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants