Skip to content

Commit 6c65096

Browse files
authored
Add support to trigger build and getting the build id (#434)
* Add support to trigger build and getting the build id * Add support on the job build command * Test pass for trigger and wait the job build * Add documentation for job build
1 parent e253b3e commit 6c65096

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

app/cmd/job_build.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ import (
1515
type JobBuildOption struct {
1616
common.BatchOption
1717
common.CommonOption
18+
common.OutputOption
1819

1920
Param string
2021
ParamArray []string
2122

2223
ParamFilePathArray []string
24+
25+
Wait bool
26+
WaitTime int
27+
Delay int
28+
Cause string
2329
}
2430

2531
var jobBuildOption JobBuildOption
@@ -31,14 +37,23 @@ func ResetJobBuildOption() {
3137

3238
func init() {
3339
jobCmd.AddCommand(jobBuildCmd)
34-
jobBuildOption.SetFlag(jobBuildCmd)
40+
jobBuildCmd.Flags().BoolVarP(&jobBuildOption.Batch, "batch", "b", false, "Batch mode, no need confirm")
3541
jobBuildCmd.Flags().StringVarP(&jobBuildOption.Param, "param", "", "",
3642
i18n.T("Parameters of the job which is JSON format"))
3743
jobBuildCmd.Flags().StringArrayVar(&jobBuildOption.ParamArray, "param-entry", nil,
3844
i18n.T("Parameters of the job which are the entry format, for example: --param-entry name=value"))
3945
jobBuildCmd.Flags().StringArrayVar(&jobBuildOption.ParamFilePathArray, "param-file", nil,
4046
i18n.T("Parameters of the job which is file path, for example: --param-file name=filename"))
41-
47+
jobBuildCmd.Flags().BoolVarP(&jobBuildOption.Wait, "wait", "", false,
48+
i18n.T("If you want to wait for the build ID from Jenkins. You need to install plugin pipeline-restful-api first"))
49+
jobBuildCmd.Flags().IntVarP(&jobBuildOption.WaitTime, "wait-timeout", "", 30,
50+
i18n.T("The timeout of seconds when you wait for the build ID"))
51+
jobBuildCmd.Flags().IntVarP(&jobBuildOption.Delay, "delay", "", 0,
52+
i18n.T("Delay when trigger a Jenkins job"))
53+
jobBuildCmd.Flags().StringVarP(&jobBuildOption.Cause, "cause", "", "triggered by jcli",
54+
i18n.T("The cause of a job build"))
55+
56+
jobBuildOption.SetFlagWithHeaders(jobBuildCmd, "Number,URL")
4257
jobBuildOption.BatchOption.Stdio = common.GetSystemStdio()
4358
jobBuildOption.CommonOption.Stdio = common.GetSystemStdio()
4459
}
@@ -87,7 +102,7 @@ You need to give the parameters if your pipeline has them. Learn more about it f
87102
}
88103
return
89104
},
90-
RunE: func(_ *cobra.Command, args []string) (err error) {
105+
RunE: func(cmd *cobra.Command, args []string) (err error) {
91106
name := args[0]
92107

93108
if !jobBuildOption.Confirm(fmt.Sprintf("Are you sure to build job %s", name)) {
@@ -136,6 +151,12 @@ You need to give the parameters if your pipeline has them. Learn more about it f
136151
if err == nil {
137152
if hasParam {
138153
err = jclient.BuildWithParams(name, paramDefs)
154+
} else if jobBuildOption.Wait {
155+
var build client.IdentityBuild
156+
if build, err = jclient.BuildAndReturn(name, jobBuildOption.Cause, jobBuildOption.WaitTime, jobBuildOption.Delay); err == nil {
157+
jobBuildOption.Writer = cmd.OutOrStdout()
158+
err = jobBuildOption.OutputV2([1]client.SimpleJobBuild{build.Build.SimpleJobBuild})
159+
}
139160
} else {
140161
err = jclient.Build(name)
141162
}

app/cmd/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ func init() {
196196

197197
if rootOptions.GetGitHubClient() == nil {
198198
rootOptions.SetGitHubClient(github.NewClient(nil))
199-
fmt.Println("setup a new gh client")
200199
} else {
201200
fmt.Println(rootOptions.GetGitHubClient())
202201
}

client/job.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,38 @@ func (q *JobClient) Build(jobName string) (err error) {
4949
return
5050
}
5151

52+
// IdentityBuild is the build which carry the identity cause
53+
type IdentityBuild struct {
54+
Build JobBuild
55+
Cause IdentityCause
56+
}
57+
58+
// IdentityCause carray a identity cause
59+
type IdentityCause struct {
60+
UUID string `json:"uuid"`
61+
ShortDescription string `json:"shortDescription"`
62+
Message string
63+
}
64+
65+
// BuildAndReturn trigger a job then returns the build info
66+
func (q *JobClient) BuildAndReturn(jobName, cause string, timeout, delay int) (build IdentityBuild, err error) {
67+
path := ParseJobPath(jobName)
68+
69+
api := fmt.Sprintf("%s/restFul/build?1=1", path)
70+
if timeout >= 0 {
71+
api += fmt.Sprintf("&timeout=%d", timeout)
72+
}
73+
if delay >= 0 {
74+
api += fmt.Sprintf("&delay=%d", delay)
75+
}
76+
if cause != "" {
77+
api += fmt.Sprintf("&identifyCause=%s", cause)
78+
}
79+
80+
err = q.RequestWithData(http.MethodPost, api, nil, nil, 200, &build)
81+
return
82+
}
83+
5284
// GetBuild get build information of a job
5385
func (q *JobClient) GetBuild(jobName string, id int) (job *JobBuild, err error) {
5486
path := ParseJobPath(jobName)

docs/book/zh/job.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ weight: 80
2121

2222
`jcli job build "jobName" -b`
2323

24+
当你需要触发任务构建,并等待对应任务的构建号的话,可以使用下面的命令
25+
(依赖插件[pipeline-restful-api-plugin](https://github.com/jenkinsci/pipeline-restful-api-plugin)):
26+
27+
`jcli job build job/test -b --wait`
28+
2429
## 交互式输入
2530

2631
执行到 Jenkins 流水线中的 `input` 指令时,会有交互式输入的提示。下面是一个样例:

0 commit comments

Comments
 (0)