Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding cgo parameter flag #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
name: 'program'
compress: 'true'
dest: 'dist'
cgo: 'false'
```

#### ☉ option: **platforms**
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 17 additions & 4 deletions entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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")

Expand All @@ -186,6 +197,8 @@ func main() {
compress = true
}

cgo := inputCGO

// for each platform, execute `build` function
for _, platform := range platforms {

Expand All @@ -199,7 +212,7 @@ func main() {
}

// execute `build` function
build(packageName, destDir, platformMap, inputLdflags, compress)
build(packageName, destDir, platformMap, inputLdflags, compress, cgo)
}

/*------------*/
Expand Down