From e4ee46d2410ab04b6e580b452855d3e4ff9aba27 Mon Sep 17 00:00:00 2001 From: houmingtao Date: Sat, 29 Sep 2018 19:11:23 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0maven=20docker:build?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/build/maven_docker/Dockerfile | 37 ++++++ java/build/maven_docker/README.md | 32 +++++ java/build/maven_docker/builder.go | 188 +++++++++++++++++++++++++++++ java/build/maven_docker/main.go | 42 +++++++ 4 files changed, 299 insertions(+) create mode 100644 java/build/maven_docker/Dockerfile create mode 100644 java/build/maven_docker/README.md create mode 100644 java/build/maven_docker/builder.go create mode 100644 java/build/maven_docker/main.go diff --git a/java/build/maven_docker/Dockerfile b/java/build/maven_docker/Dockerfile new file mode 100644 index 0000000..fdf31f3 --- /dev/null +++ b/java/build/maven_docker/Dockerfile @@ -0,0 +1,37 @@ +FROM golang:1.10-alpine as builder + +MAINTAINER julywind@126.com +WORKDIR /go/src/component-maven + +COPY ./ /go/src/component-maven + +RUN set -ex && \ +go build -v -o /go/bin/component-maven \ +-gcflags '-N -l' \ +./*.go + + +FROM maven:3.5-jdk-8 +RUN mkdir -p /root/src +WORKDIR /root/src + +RUN apt-get update && apt-get install -y docker \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=builder /go/bin/component-maven /usr/bin/ +CMD ["component-maven"] + +LABEL TencentHubComponent='{\ + "description": "TencentHub官方组件(Java Build Maven Docker), 使用maven进行java项目构建,并且可执行docker相关的命令.",\ + "input": [\ + {"name": "GIT_CLONE_URL", "desc": "必填,源代码地址,如为私有仓库需要授权; 如需使用系统关联的git仓库, 可以从系统提供的全局环境变量中获取: ${_WORKFLOW_GIT_CLONE_URL}"},\ + {"name": "GIT_REF", "desc": "非必填,源代码目标提交号或者分支名, 默认为master"},\ + {"name": "M2_SETTINGS_XML", "desc": "非必填,$user/.m2/setting.xml配置文件内容,默认使用maven的全局配置"},\ + {"name": "GOALS", "desc": "非必填,maven 构建目标, 默认是package"},\ + {"name": "POM_PATH", "desc": "非必填,pom 文件相对路径, 默认`./pom.xml`"},\ + {"name": "HUB_REPO", "desc": "非必填,构建产物目标二进制仓库, 组件在构建完成后将尝试自动上传构建产物到此仓库, 如果此值为空将不会自动上传"},\ + {"name": "HUB_USER", "desc": "非必填,上传构建产物对应仓库的用户名, 如果想使用当前主账号, 可以直接设置_WORKFLOW_FLAG_HUB_TOKEN: true, 执行引擎将自动注入当前用户名和token"},\ + {"name": "HUB_TOKEN", "desc": "非必填,上传构建产物对应仓库的用户密码或者token, 同上, 如果设置了_WORKFLOW_FLAG_HUB_TOKEN: true, 此入参可以省略"},\ + {"name": "_WORKFLOW_FLAG_HUB_TOKEN", "default": "true", "desc": "非必填, 若为真, 工作流将根据用户名和密码自动填充HUB_USER和HUB_TOKEN"}\ + ]\ +}' diff --git a/java/build/maven_docker/README.md b/java/build/maven_docker/README.md new file mode 100644 index 0000000..dedbb78 --- /dev/null +++ b/java/build/maven_docker/README.md @@ -0,0 +1,32 @@ +## 组件名称:Java Build Maven + +使用 maven 进行 java 项目构建, 可选地将构建产物上传到指定的二进制仓库. + +### 组件参数 +#### 入参 +- `GIT_CLONE_URL` 必填,源代码地址,如为私有仓库需要授权; 如需使用系统关联的git仓库, 可以从系统提供的全局环境变量中获取: `${_WORKFLOW_GIT_CLONE_URL}` +- `GIT_REF` 非必填,源代码目标提交号或者分支名, 默认为master +- `M2_SETTINGS_XML` 非必填,`$user/.m2/setting.xml`配置文件内容,默认使用maven的全局配置 +- `GOALS` 非必填,maven 构建目标, 默认是`package` +- `POM_PATH` 非必填,pom 文件相对路径, 默认`./pom.xml` +- `HUB_REPO` 非必填,构建产物目标二进制仓库, 组件在构建完成后将尝试自动上传构建产物到此仓库, 如果此值为空将不会自动上传 +- `HUB_USER` 非必填,上传构建产物对应仓库的用户名, 如果想使用当前主账号, 可以直接设置`_WORKFLOW_FLAG_HUB_TOKEN: true`, 执行引擎将自动注入当前用户名和token +- `HUB_TOKEN` 非必填,上传构建产物对应仓库的用户密码或者token, 同上, 如果设置了`_WORKFLOW_FLAG_HUB_TOKEN: true`, 此入参可以省略 +- `ARTIFACT_PATH` 非必填,上传构建产物对应的仓库目录, 默认是仓库根目录 +- `ARTIFACT_TAG` 非必填,上传构建产物对应的tag, 默认是latest + +#### 出参 +- `ARTIFACT` 构建产物结果列表 +- `ARTIFACT_URL` 成功上传的构建产物url + +### Tag列表及其Dockerfile链接 + +* 3.5-jdk-8, latest: [Dockerfile](https://github.com/tencentyun/workflow-components/blob/c2d0c1ceb447694a092599858203d29dd877e6bb/java/build/maven/Dockerfile) + +### 源码地址 + +Java Maven Build: + +### 构建: + +`docker build -t hub.tencentyun.com/tencenthub/java_build_maven:latest .` diff --git a/java/build/maven_docker/builder.go b/java/build/maven_docker/builder.go new file mode 100644 index 0000000..aa5854d --- /dev/null +++ b/java/build/maven_docker/builder.go @@ -0,0 +1,188 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" +) + +const baseSpace = "/root/src" + +type Builder struct { + // 用户提供参数, 通过环境变量传入 + GitCloneURL string + GitRef string + Goals string + PomPath string + + HubRepo string + HubUser string + HubToken string + M2SettingXML string + + projectName string +} + +// NewBuilder is +func NewBuilder(envs map[string]string) (*Builder, error) { + b := &Builder{} + + if envs["GIT_CLONE_URL"] != "" { + b.GitCloneURL = envs["GIT_CLONE_URL"] + b.GitRef = envs["GIT_REF"] + } else if envs["_WORKFLOW_GIT_CLONE_URL"] != "" { + b.GitCloneURL = envs["_WORKFLOW_GIT_CLONE_URL"] + b.GitRef = envs["_WORKFLOW_GIT_REF"] + } else { + return nil, fmt.Errorf("envionment variable GIT_CLONE_URL is required") + } + + if b.GitRef == "" { + b.GitRef = "master" + } + + s := strings.TrimSuffix(strings.TrimSuffix(b.GitCloneURL, "/"), ".git") + b.projectName = s[strings.LastIndex(s, "/")+1:] + + if b.GitRef = envs["GIT_REF"]; b.GitRef == "" { + b.GitRef = "master" + } + + b.Goals = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(envs["GOALS"]), "mvn ")) + + if b.PomPath = envs["POM_PATH"]; b.PomPath == "" { + b.PomPath = "./pom.xml" + } + + b.HubUser = envs["HUB_USER"] + b.HubToken = envs["HUB_TOKEN"] + + if b.HubUser == "" && b.HubToken == "" { + b.HubUser = envs["_WORKFLOW_HUB_USER"] + b.HubToken = envs["_WORKFLOW_HUB_TOKEN"] + } + if b.HubUser == "" || b.HubToken == "" { + return nil, fmt.Errorf("envionment variable HUB_USER, HUB_TOKEN are required") + } + + b.HubRepo = envs["HUB_REPO"] + + b.M2SettingXML = envs["M2_SETTINGS_XML"] + + return b, nil +} + +func (b *Builder) run() error { + if err := os.Chdir(baseSpace); err != nil { + return fmt.Errorf("Chdir to baseSpace(%s) failed:%v", baseSpace, err) + } + + if err := b.gitPull(); err != nil { + return err + } + + if err := b.gitReset(); err != nil { + return err + } + + if err := b.setM2SettingXML(); err != nil { + return err + } + + if err := b.build(); err != nil { + return err + } + + // if err := b.handleArtifacts(); err != nil { + // return err + // } + + // err = b.doPush(b.Image) + // if err != nil { + // return + // } + return nil +} + +func (b *Builder) setM2SettingXML() error { + var command = []string{"cp", "/usr/share/maven/conf/settings.xml", "/root/.m2/settings.xml"} + if _, err := (CMD{Command: command}).Run(); err != nil { + fmt.Println("copy settings.xml failed:", err) + return err + } + if b.M2SettingXML != "" { + command = []string{"echo", b.M2SettingXML, ">", "/root/.m2/setting.xml"} + if _, err := (CMD{Command: command}).Run(); err != nil { + fmt.Println("write setting to .m2/settings.xml failed:", err) + return err + } + } + return nil +} + +func (b *Builder) build() error { + var command = strings.Fields(b.Goals) + + if len(command) == 0 { + command = append(command, "mvn", "package") + } + + if command[0] != "mvn" { + command = append([]string{"mvn"}, command...) + } + + command = append(command, "-f", b.PomPath) + + cwd, _ := os.Getwd() + if _, err := (CMD{command, filepath.Join(cwd, b.projectName)}).Run(); err != nil { + fmt.Println("Run mvn goals failed:", err) + return err + } + fmt.Println("Run mvn goals succeed.") + return nil +} + +func (b *Builder) gitPull() error { + var command = []string{"git", "clone", "--recurse-submodules", b.GitCloneURL, b.projectName} + if _, err := (CMD{Command: command}).Run(); err != nil { + fmt.Println("Clone project failed:", err) + return err + } + fmt.Println("Clone project", b.GitCloneURL, "succeed.") + return nil +} + +func (b *Builder) gitReset() error { + cwd, _ := os.Getwd() + var command = []string{"git", "checkout", b.GitRef, "--"} + if _, err := (CMD{command, filepath.Join(cwd, b.projectName)}).Run(); err != nil { + fmt.Println("Switch to commit", b.GitRef, "failed:", err) + return err + } + fmt.Println("Switch to", b.GitRef, "succeed.") + return nil +} + +type CMD struct { + Command []string // cmd with args + WorkDir string +} + +func (c CMD) Run() (string, error) { + fmt.Println("Run CMD: ", strings.Join(c.Command, " ")) + + cmd := exec.Command(c.Command[0], c.Command[1:]...) + if c.WorkDir != "" { + cmd.Dir = c.WorkDir + } + + data, err := cmd.CombinedOutput() + result := string(data) + if len(result) > 0 { + fmt.Println(result) + } + + return result, err +} diff --git a/java/build/maven_docker/main.go b/java/build/maven_docker/main.go new file mode 100644 index 0000000..4e62755 --- /dev/null +++ b/java/build/maven_docker/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "os" +) + +var envList = []string{ + "GIT_CLONE_URL", + "GIT_REF", + "_WORKFLOW_GIT_CLONE_URL", + "_WORKFLOW_GIT_REF", + + "GOALS", "POM_PATH", + "HUB_REPO", + "M2_SETTINGS_XML", + + "HUB_USER", + "HUB_TOKEN", + "_WORKFLOW_HUB_USER", + "_WORKFLOW_HUB_TOKEN", +} + +func main() { + envs := make(map[string]string) + for _, envName := range envList { + envs[envName] = os.Getenv(envName) + } + + builder, err := NewBuilder(envs) + if err != nil { + fmt.Println("BUILDER FAILED: ", err) + os.Exit(1) + } + + if err := builder.run(); err != nil { + fmt.Println("BUILD FAILED", err) + os.Exit(1) + } else { + fmt.Println("BUILD SUCCEED") + } +} From 5211f38a36adb5977cb50f4e7ac4425ca47bffea Mon Sep 17 00:00:00 2001 From: houmingtao Date: Fri, 5 Oct 2018 18:47:58 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/build/maven_docker/Dockerfile | 3 --- java/build/maven_docker/README.md | 9 --------- java/build/maven_docker/builder.go | 13 ------------- java/build/maven_docker/main.go | 9 ++------- 4 files changed, 2 insertions(+), 32 deletions(-) diff --git a/java/build/maven_docker/Dockerfile b/java/build/maven_docker/Dockerfile index fdf31f3..66136e3 100644 --- a/java/build/maven_docker/Dockerfile +++ b/java/build/maven_docker/Dockerfile @@ -29,9 +29,6 @@ LABEL TencentHubComponent='{\ {"name": "M2_SETTINGS_XML", "desc": "非必填,$user/.m2/setting.xml配置文件内容,默认使用maven的全局配置"},\ {"name": "GOALS", "desc": "非必填,maven 构建目标, 默认是package"},\ {"name": "POM_PATH", "desc": "非必填,pom 文件相对路径, 默认`./pom.xml`"},\ - {"name": "HUB_REPO", "desc": "非必填,构建产物目标二进制仓库, 组件在构建完成后将尝试自动上传构建产物到此仓库, 如果此值为空将不会自动上传"},\ - {"name": "HUB_USER", "desc": "非必填,上传构建产物对应仓库的用户名, 如果想使用当前主账号, 可以直接设置_WORKFLOW_FLAG_HUB_TOKEN: true, 执行引擎将自动注入当前用户名和token"},\ - {"name": "HUB_TOKEN", "desc": "非必填,上传构建产物对应仓库的用户密码或者token, 同上, 如果设置了_WORKFLOW_FLAG_HUB_TOKEN: true, 此入参可以省略"},\ {"name": "_WORKFLOW_FLAG_HUB_TOKEN", "default": "true", "desc": "非必填, 若为真, 工作流将根据用户名和密码自动填充HUB_USER和HUB_TOKEN"}\ ]\ }' diff --git a/java/build/maven_docker/README.md b/java/build/maven_docker/README.md index dedbb78..3088754 100644 --- a/java/build/maven_docker/README.md +++ b/java/build/maven_docker/README.md @@ -9,15 +9,6 @@ - `M2_SETTINGS_XML` 非必填,`$user/.m2/setting.xml`配置文件内容,默认使用maven的全局配置 - `GOALS` 非必填,maven 构建目标, 默认是`package` - `POM_PATH` 非必填,pom 文件相对路径, 默认`./pom.xml` -- `HUB_REPO` 非必填,构建产物目标二进制仓库, 组件在构建完成后将尝试自动上传构建产物到此仓库, 如果此值为空将不会自动上传 -- `HUB_USER` 非必填,上传构建产物对应仓库的用户名, 如果想使用当前主账号, 可以直接设置`_WORKFLOW_FLAG_HUB_TOKEN: true`, 执行引擎将自动注入当前用户名和token -- `HUB_TOKEN` 非必填,上传构建产物对应仓库的用户密码或者token, 同上, 如果设置了`_WORKFLOW_FLAG_HUB_TOKEN: true`, 此入参可以省略 -- `ARTIFACT_PATH` 非必填,上传构建产物对应的仓库目录, 默认是仓库根目录 -- `ARTIFACT_TAG` 非必填,上传构建产物对应的tag, 默认是latest - -#### 出参 -- `ARTIFACT` 构建产物结果列表 -- `ARTIFACT_URL` 成功上传的构建产物url ### Tag列表及其Dockerfile链接 diff --git a/java/build/maven_docker/builder.go b/java/build/maven_docker/builder.go index aa5854d..f20960a 100644 --- a/java/build/maven_docker/builder.go +++ b/java/build/maven_docker/builder.go @@ -56,19 +56,6 @@ func NewBuilder(envs map[string]string) (*Builder, error) { b.PomPath = "./pom.xml" } - b.HubUser = envs["HUB_USER"] - b.HubToken = envs["HUB_TOKEN"] - - if b.HubUser == "" && b.HubToken == "" { - b.HubUser = envs["_WORKFLOW_HUB_USER"] - b.HubToken = envs["_WORKFLOW_HUB_TOKEN"] - } - if b.HubUser == "" || b.HubToken == "" { - return nil, fmt.Errorf("envionment variable HUB_USER, HUB_TOKEN are required") - } - - b.HubRepo = envs["HUB_REPO"] - b.M2SettingXML = envs["M2_SETTINGS_XML"] return b, nil diff --git a/java/build/maven_docker/main.go b/java/build/maven_docker/main.go index 4e62755..842e7c9 100644 --- a/java/build/maven_docker/main.go +++ b/java/build/maven_docker/main.go @@ -11,14 +11,9 @@ var envList = []string{ "_WORKFLOW_GIT_CLONE_URL", "_WORKFLOW_GIT_REF", - "GOALS", "POM_PATH", - "HUB_REPO", + "GOALS", + "POM_PATH", "M2_SETTINGS_XML", - - "HUB_USER", - "HUB_TOKEN", - "_WORKFLOW_HUB_USER", - "_WORKFLOW_HUB_TOKEN", } func main() { From 858bfda26bd8a63233d54c1dc2e53d2fe9718d68 Mon Sep 17 00:00:00 2001 From: houmingtao Date: Fri, 5 Oct 2018 23:23:52 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/build/maven_docker/Dockerfile | 1 + java/build/maven_docker/README.md | 1 + java/build/maven_docker/builder.go | 30 +++++++++++++++++++++++++++--- java/build/maven_docker/main.go | 1 + 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/java/build/maven_docker/Dockerfile b/java/build/maven_docker/Dockerfile index 66136e3..8963526 100644 --- a/java/build/maven_docker/Dockerfile +++ b/java/build/maven_docker/Dockerfile @@ -29,6 +29,7 @@ LABEL TencentHubComponent='{\ {"name": "M2_SETTINGS_XML", "desc": "非必填,$user/.m2/setting.xml配置文件内容,默认使用maven的全局配置"},\ {"name": "GOALS", "desc": "非必填,maven 构建目标, 默认是package"},\ {"name": "POM_PATH", "desc": "非必填,pom 文件相对路径, 默认`./pom.xml`"},\ + {"name": "EXT_COMMAND", "desc": "非必填,GOALS之外的命令, 默认不执行"},\ {"name": "_WORKFLOW_FLAG_HUB_TOKEN", "default": "true", "desc": "非必填, 若为真, 工作流将根据用户名和密码自动填充HUB_USER和HUB_TOKEN"}\ ]\ }' diff --git a/java/build/maven_docker/README.md b/java/build/maven_docker/README.md index 3088754..9bcb3fa 100644 --- a/java/build/maven_docker/README.md +++ b/java/build/maven_docker/README.md @@ -9,6 +9,7 @@ - `M2_SETTINGS_XML` 非必填,`$user/.m2/setting.xml`配置文件内容,默认使用maven的全局配置 - `GOALS` 非必填,maven 构建目标, 默认是`package` - `POM_PATH` 非必填,pom 文件相对路径, 默认`./pom.xml` +- `EXT_COMMAND` 非必填,GOALS之外的命令, 默认不执行 ### Tag列表及其Dockerfile链接 diff --git a/java/build/maven_docker/builder.go b/java/build/maven_docker/builder.go index f20960a..4174fd5 100644 --- a/java/build/maven_docker/builder.go +++ b/java/build/maven_docker/builder.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "strings" + "bytes" ) const baseSpace = "/root/src" @@ -21,14 +22,16 @@ type Builder struct { HubUser string HubToken string M2SettingXML string - + ExtCommand string projectName string } // NewBuilder is func NewBuilder(envs map[string]string) (*Builder, error) { b := &Builder{} - + if envs["GIT_CLONE_URL"] != "" { + b.ExtCommand = envs["EXT_COMMAND"] + } if envs["GIT_CLONE_URL"] != "" { b.GitCloneURL = envs["GIT_CLONE_URL"] b.GitRef = envs["GIT_REF"] @@ -82,6 +85,10 @@ func (b *Builder) run() error { return err } + if err := b.execCommand(); err != nil { + return err + } + // if err := b.handleArtifacts(); err != nil { // return err // } @@ -152,6 +159,23 @@ func (b *Builder) gitReset() error { return nil } +func (b *Builder) execCommand() error{ + if(b.ExtCommand == ""){ + return nil + } + fmt.Printf("exec:", b.ExtCommand) + cmd := exec.Command("/bin/bash", "-c", b.ExtCommand) + var out bytes.Buffer + + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + fmt.Println("exec:", b.ExtCommand, "\nFailed:", err) + } + fmt.Printf(out.String()) + return err +} + type CMD struct { Command []string // cmd with args WorkDir string @@ -172,4 +196,4 @@ func (c CMD) Run() (string, error) { } return result, err -} +} \ No newline at end of file diff --git a/java/build/maven_docker/main.go b/java/build/maven_docker/main.go index 842e7c9..267551e 100644 --- a/java/build/maven_docker/main.go +++ b/java/build/maven_docker/main.go @@ -13,6 +13,7 @@ var envList = []string{ "GOALS", "POM_PATH", + "EXT_COMMAND", "M2_SETTINGS_XML", } From a72d8386c3b301bb2aad48bc6af6d76a2e2418ad Mon Sep 17 00:00:00 2001 From: houmingtao Date: Sat, 6 Oct 2018 00:41:39 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9extcommand=E7=9A=84?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/build/maven_docker/builder.go | 36 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/java/build/maven_docker/builder.go b/java/build/maven_docker/builder.go index 4174fd5..77ae4fd 100644 --- a/java/build/maven_docker/builder.go +++ b/java/build/maven_docker/builder.go @@ -85,10 +85,6 @@ func (b *Builder) run() error { return err } - if err := b.execCommand(); err != nil { - return err - } - // if err := b.handleArtifacts(); err != nil { // return err // } @@ -130,11 +126,17 @@ func (b *Builder) build() error { command = append(command, "-f", b.PomPath) cwd, _ := os.Getwd() - if _, err := (CMD{command, filepath.Join(cwd, b.projectName)}).Run(); err != nil { + if _, err := (CMD{command, filepath.Join(cwd, b.projectName),b.ExtCommand}).Run(); err != nil { fmt.Println("Run mvn goals failed:", err) return err } fmt.Println("Run mvn goals succeed.") + + if _, err := (CMD{command, filepath.Join(cwd, b.projectName),b.ExtCommand}).ExecCommand(); err != nil { + fmt.Println("Run extCommand failed:", err) + return err + } + fmt.Println("Run extCommand succeed.") return nil } @@ -159,12 +161,21 @@ func (b *Builder) gitReset() error { return nil } -func (b *Builder) execCommand() error{ - if(b.ExtCommand == ""){ +type CMD struct { + Command []string // cmd with args + WorkDir string + ExtCommand string +} + +func (c CMD) ExecCommand() (string, error){ + if(c.ExtCommand == ""){ return nil } - fmt.Printf("exec:", b.ExtCommand) - cmd := exec.Command("/bin/bash", "-c", b.ExtCommand) + fmt.Printf("exec:", c.ExtCommand) + cmd := exec.Command("/bin/bash", "-c", c.ExtCommand) + if c.WorkDir != "" { + cmd.Dir = c.WorkDir + } var out bytes.Buffer cmd.Stdout = &out @@ -173,12 +184,7 @@ func (b *Builder) execCommand() error{ fmt.Println("exec:", b.ExtCommand, "\nFailed:", err) } fmt.Printf(out.String()) - return err -} - -type CMD struct { - Command []string // cmd with args - WorkDir string + return out.String(),err } func (c CMD) Run() (string, error) { From 47f39187b6a736723734027aaffbe9ed049d3ef2 Mon Sep 17 00:00:00 2001 From: houmingtao Date: Sat, 6 Oct 2018 00:47:23 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/build/maven_docker/builder.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/build/maven_docker/builder.go b/java/build/maven_docker/builder.go index 77ae4fd..6cb6aaf 100644 --- a/java/build/maven_docker/builder.go +++ b/java/build/maven_docker/builder.go @@ -126,7 +126,7 @@ func (b *Builder) build() error { command = append(command, "-f", b.PomPath) cwd, _ := os.Getwd() - if _, err := (CMD{command, filepath.Join(cwd, b.projectName),b.ExtCommand}).Run(); err != nil { + if _, err := (CMD{Command: command, WorkDir: filepath.Join(cwd, b.projectName)}).Run(); err != nil { fmt.Println("Run mvn goals failed:", err) return err } @@ -153,7 +153,7 @@ func (b *Builder) gitPull() error { func (b *Builder) gitReset() error { cwd, _ := os.Getwd() var command = []string{"git", "checkout", b.GitRef, "--"} - if _, err := (CMD{command, filepath.Join(cwd, b.projectName)}).Run(); err != nil { + if _, err := (CMD{Command: command, WorkDir: filepath.Join(cwd, b.projectName)}).Run(); err != nil { fmt.Println("Switch to commit", b.GitRef, "failed:", err) return err } @@ -169,7 +169,7 @@ type CMD struct { func (c CMD) ExecCommand() (string, error){ if(c.ExtCommand == ""){ - return nil + return "",nil } fmt.Printf("exec:", c.ExtCommand) cmd := exec.Command("/bin/bash", "-c", c.ExtCommand) @@ -181,7 +181,7 @@ func (c CMD) ExecCommand() (string, error){ cmd.Stdout = &out err := cmd.Run() if err != nil { - fmt.Println("exec:", b.ExtCommand, "\nFailed:", err) + fmt.Println("exec:", c.ExtCommand, "\nFailed:", err) } fmt.Printf(out.String()) return out.String(),err