-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
raojinlin
committed
Sep 11, 2023
0 parents
commit 9df5a33
Showing
8 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
build: | ||
GOOS=linux GOARCH=amd64 go build -o ./bin/gitlab-jenkins-runner . | ||
GOOS=linux GOARCH=arm64 go build -o ./bin/gitlab-jenkins-runner-arm64 . | ||
gzip: | ||
gzip ./bin/gitlab-jenkins-runner-arm64 | ||
gzip ./bin/gitlab-jenkins-runner | ||
|
||
.phony: build gzip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# GitLab Jenkins Runner | ||
|
||
GitLab Jenkins Runner 是一个用于在 GitLab CI/CD 流水线中触发 Jenkins 作业的命令行工具。它允许你轻松地将 GitLab 与 Jenkins 集成,以实现自动化构建和部署工作流程。 | ||
|
||
## 安装 | ||
|
||
在开始之前,请确保你的系统上已安装了 Go 编程语言。 | ||
|
||
使用以下命令获取和构建 `gitlab-jenkins-runner` 工具: | ||
|
||
```bash | ||
go install github.com/yourusername/gitlab-jenkins-runner | ||
``` | ||
|
||
## 使用 | ||
|
||
使用以下命令来触发 Jenkins 作业: | ||
|
||
```bash | ||
gitlab-jenkins-runner -base <your-jenkins-url> -job <job-name> -params PARAM1=Value1,PARAM2=Value2 -user <jenkins-user> -token <jenkins-token> -build | ||
``` | ||
|
||
### 参数说明 | ||
|
||
- `-base <your-jenkins-url>`:Jenkins 服务器的基本 URL。 | ||
- `-job <job-name>`:要触发的 Jenkins 作业的名称。 | ||
- `-params PARAM1=Value1,PARAM2=Value2`:要传递给 Jenkins 作业的参数,以逗号分隔的键值对。 | ||
- `-user <jenkins-user>`:Jenkins 用户名。 | ||
- `-token <jenkins-token>`:Jenkins 用户令牌或密码。 | ||
- `-build`:触发 Jenkins 作业的构建。 | ||
|
||
## 示例 | ||
|
||
以下是一个示例命令,演示如何使用 `gitlab-jenkins-runner` 工具: | ||
|
||
```bash | ||
gitlab-jenkins-runner -base https://your-jenkins-url.com -job my-build-job -params BRANCH=main,ENV=prod -user jenkinsuser -token myapitoken -build | ||
``` | ||
|
||
## 许可证 | ||
|
||
本项目基于 MIT 许可证。有关详细信息,请参阅 [LICENSE.md](LICENSE.md)。 | ||
|
||
## 支持 | ||
|
||
如果你需要帮助或有其他相关问题,请联系我们。 | ||
|
||
## 致谢 | ||
|
||
感谢使用 GitLab Jenkins Runner! |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/raojinlin/gitlab-jenkins-runner | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/bndr/gojenkins v1.1.0 // indirect | ||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
github.com/bndr/gojenkins v1.1.0 h1:TWyJI6ST1qDAfH33DQb3G4mD8KkrBfyfSUoZBHQAvPI= | ||
github.com/bndr/gojenkins v1.1.0/go.mod h1:QeskxN9F/Csz0XV/01IC8y37CapKKWvOHa0UHLLX1fM= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= | ||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/bndr/gojenkins" | ||
) | ||
|
||
var ( | ||
triggerJobBuild bool | ||
baseUrl string | ||
params string | ||
jobName string | ||
jenkinsUser string | ||
jenkinsToken string | ||
) | ||
|
||
func waitJobStart(job *gojenkins.Job) { | ||
ctx := context.Background() | ||
for { | ||
isQueued, err := job.IsQueued(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
if isQueued { | ||
fmt.Println("waiting... job", jobName, "queued") | ||
} else { | ||
break | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
} | ||
|
||
func ParseParams(params string) map[string]string { | ||
labelArr := strings.Split(params, ",") | ||
result := make(map[string]string, 0) | ||
|
||
for _, label := range labelArr { | ||
param := strings.SplitN(label, "=", 2) | ||
result[param[0]] = param[1] | ||
} | ||
|
||
return result | ||
} | ||
|
||
func main() { | ||
flag.StringVar(&jobName, "job", jobName, "Jenkins job name") | ||
flag.StringVar(&baseUrl, "base", baseUrl, "Jenkins base url") | ||
flag.StringVar(¶ms, "params", params, "Gitlab labels variable value") | ||
flag.StringVar(&jenkinsUser, "user", jenkinsUser, "Jenkins user") | ||
flag.StringVar(&jenkinsToken, "token", jenkinsUser, "Jenkins user token") | ||
flag.BoolVar(&triggerJobBuild, "build", triggerJobBuild, "Trigger jenkins job build") | ||
flag.Parse() | ||
|
||
ctx := context.Background() | ||
jenkins := gojenkins.CreateJenkins(nil, baseUrl, jenkinsUser, jenkinsToken) | ||
_, err := jenkins.Init(ctx) | ||
if err != nil { | ||
fmt.Println("init jenkins error:", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
job, err := jenkins.GetJob(ctx, jobName) | ||
if err != nil { | ||
fmt.Printf("get job %s error:%s\n", jobName, err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
waitJobStart(job) | ||
nextBuildNumber := job.GetDetails().NextBuildNumber | ||
var build *gojenkins.Build | ||
for { | ||
build, err = job.GetBuild(ctx, nextBuildNumber) | ||
if build != nil { | ||
break | ||
} | ||
|
||
if err != nil { | ||
if triggerJobBuild && strings.Contains(err.Error(), "404") { | ||
_, err = job.InvokeSimple(ctx, ParseParams(params)) | ||
if err != nil { | ||
fmt.Println("invoke error", err.Error()) | ||
os.Exit(2) | ||
} | ||
|
||
waitJobStart(job) | ||
} else { | ||
fmt.Println(err.Error()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
consoleOutput, err := build.GetConsoleOutputFromIndex(ctx, 0) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
for consoleOutput.HasMoreText { | ||
fmt.Print(consoleOutput.Content) | ||
consoleOutput, err = build.GetConsoleOutputFromIndex(ctx, consoleOutput.Offset) | ||
if err != nil { | ||
fmt.Println("get console output from index", consoleOutput.Offset, err.Error()) | ||
return | ||
} | ||
} | ||
|
||
build, err = job.GetBuild(ctx, nextBuildNumber) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(2) | ||
} | ||
|
||
fmt.Printf("build.Info().Result: %v\n", build.Info().Result) | ||
if build.Info().Result != "SUCCESS" { | ||
os.Exit(127) | ||
} | ||
} |