From 16af09e86cccb8494dc946982adb4d448b083f01 Mon Sep 17 00:00:00 2001 From: Matthew Plachter Date: Tue, 27 Jul 2021 12:18:04 -0400 Subject: [PATCH 1/2] adding cgo parameter flag --- README.md | 4 ++++ action.yml | 4 ++++ entrypoint.go | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2306140..00e1837 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ jobs: name: 'program' compress: 'true' dest: 'dist' + cgo: 'false' ``` #### ☉ option: **platforms** @@ -50,6 +51,9 @@ The `name` option sets a prefix for the build filenames. In compression mode, th #### ☉ option: **dest** The `dest` option sets the output directory for the build files. This should be a relative directory without leading `./`. +#### ☉ option: **dest** +The `cgo` option sets if you want go to use cgo when building the binary. The default behavior of golang is to build with cgo. setting this option to `'false'` will disable cgo when building the binaries. + ## Build Artifacts This action produces following build-artifacts. diff --git a/action.yml b/action.yml index f501d02..5aafa20 100644 --- a/action.yml +++ b/action.yml @@ -33,6 +33,10 @@ inputs: description: 'Flags to pass to the Go linker.' default: '' required: false + cgo: + description: "Enable CGO" + default: 'true' + required: false # action runner (golang:latest image) runs: diff --git a/entrypoint.go b/entrypoint.go index b7c921c..7d28c98 100644 --- a/entrypoint.go +++ b/entrypoint.go @@ -39,7 +39,7 @@ func copyFile(src, dest string) { /*************************************/ // build the package for a platform -func build(packageName, destDir string, platform map[string]string, ldflags string, compress bool) { +func build(packageName, destDir string, platform map[string]string, ldflags string, compress bool, cgo bool) { // platform config platformKernel := platform["kernel"] @@ -84,10 +84,17 @@ func build(packageName, destDir string, platform map[string]string, ldflags stri // generate `go build` command buildCmd := exec.Command("go", buildOptions...) + cgoValue := 0 + + if cgo { + cgoValue = 1 + } + // set environment variables buildCmd.Env = append(os.Environ(), []string{ fmt.Sprintf("GOOS=%s", platformKernel), fmt.Sprintf("GOARCH=%s", platformArch), + fmt.Sprintf("CGO_ENABLED=%d", cgoValue), }...) // execute `go build` command @@ -168,6 +175,7 @@ func main() { inputPlatforms := os.Getenv("INPUT_PLATFORMS") inputPackage := os.Getenv("INPUT_PACKAGE") inputCompress := os.Getenv("INPUT_COMPRESS") + inputCGO := os.Getenv("INPUT_CGO") inputDest := os.Getenv("INPUT_DEST") inputLdflags := os.Getenv("INPUT_LDFLAGS") @@ -186,6 +194,11 @@ func main() { compress = true } + cgo := false + if strings.ToLower(inputCGO) == "true" { + compress = true + } + // for each platform, execute `build` function for _, platform := range platforms { @@ -199,7 +212,7 @@ func main() { } // execute `build` function - build(packageName, destDir, platformMap, inputLdflags, compress) + build(packageName, destDir, platformMap, inputLdflags, compress, cgo) } /*------------*/ From 45d0e4efd65c46cf9c720fabb03b569477c0906a Mon Sep 17 00:00:00 2001 From: Matthew Plachter Date: Tue, 27 Jul 2021 17:55:47 -0400 Subject: [PATCH 2/2] fix logic back to default behavior for go build. --- README.md | 4 ++-- action.yml | 4 ++-- entrypoint.go | 28 ++++++++++++++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 00e1837..1060455 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,8 @@ The `name` option sets a prefix for the build filenames. In compression mode, th #### ☉ option: **dest** The `dest` option sets the output directory for the build files. This should be a relative directory without leading `./`. -#### ☉ option: **dest** -The `cgo` option sets if you want go to use cgo when building the binary. The default behavior of golang is to build with cgo. setting this option to `'false'` will disable cgo when building the binaries. +#### ☉ option: **cgo** +The `cgo` option sets if you want go to use cgo when building the binary. The default behavior of golang is to build with [cgo on native systems](https://pkg.go.dev/cmd/cgo#:~:text=The%20cgo%20tool%20is%20enabled,to%200%20to%20disable%20it). setting this option to `'false'` will disable cgo when building the binaries, setting it to `'true'` will enable CGO when building the binaries. ## Build Artifacts diff --git a/action.yml b/action.yml index 5aafa20..73b7443 100644 --- a/action.yml +++ b/action.yml @@ -34,8 +34,8 @@ inputs: default: '' required: false cgo: - description: "Enable CGO" - default: 'true' + description: "Enable or Disable CGO" + default: '' required: false # action runner (golang:latest image) diff --git a/entrypoint.go b/entrypoint.go index 7d28c98..4d3c32b 100644 --- a/entrypoint.go +++ b/entrypoint.go @@ -39,7 +39,7 @@ func copyFile(src, dest string) { /*************************************/ // build the package for a platform -func build(packageName, destDir string, platform map[string]string, ldflags string, compress bool, cgo bool) { +func build(packageName, destDir string, platform map[string]string, ldflags string, compress bool, cgo string) { // platform config platformKernel := platform["kernel"] @@ -84,18 +84,21 @@ func build(packageName, destDir string, platform map[string]string, ldflags stri // generate `go build` command buildCmd := exec.Command("go", buildOptions...) - cgoValue := 0 - - if cgo { - cgoValue = 1 - } - // set environment variables - buildCmd.Env = append(os.Environ(), []string{ + buildStrings := []string{ fmt.Sprintf("GOOS=%s", platformKernel), fmt.Sprintf("GOARCH=%s", platformArch), - fmt.Sprintf("CGO_ENABLED=%d", cgoValue), - }...) + } + + switch strings.ToLower(cgo) { + case "true": + buildStrings = append(buildStrings, fmt.Sprintf("CGO_ENABLED=1")) + case "false": + buildStrings = append(buildStrings, fmt.Sprintf("CGO_ENABLED=0")) + default: + } + + buildCmd.Env = append(os.Environ(), buildStrings...) // execute `go build` command fmt.Println("Creating a build using :", buildCmd.String()) @@ -194,10 +197,7 @@ func main() { compress = true } - cgo := false - if strings.ToLower(inputCGO) == "true" { - compress = true - } + cgo := inputCGO // for each platform, execute `build` function for _, platform := range platforms {