diff --git a/README.md b/README.md index 2306140..1060455 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: **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 This action produces following build-artifacts. diff --git a/action.yml b/action.yml index f501d02..73b7443 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 or Disable CGO" + default: '' + required: false # action runner (golang:latest image) runs: diff --git a/entrypoint.go b/entrypoint.go index b7c921c..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) { +func build(packageName, destDir string, platform map[string]string, ldflags string, compress bool, cgo string) { // platform config platformKernel := platform["kernel"] @@ -85,10 +85,20 @@ func build(packageName, destDir string, platform map[string]string, ldflags stri buildCmd := exec.Command("go", buildOptions...) // set environment variables - buildCmd.Env = append(os.Environ(), []string{ + buildStrings := []string{ fmt.Sprintf("GOOS=%s", platformKernel), fmt.Sprintf("GOARCH=%s", platformArch), - }...) + } + + 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()) @@ -168,6 +178,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 +197,8 @@ func main() { compress = true } + cgo := inputCGO + // 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) } /*------------*/