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

how can I pass parameters to go build when exec ko publish #69

Closed
tsunhua opened this issue Aug 13, 2019 · 9 comments
Closed

how can I pass parameters to go build when exec ko publish #69

tsunhua opened this issue Aug 13, 2019 · 9 comments

Comments

@tsunhua
Copy link

tsunhua commented Aug 13, 2019

In my Go project, I use go build -tags 'dev' to build dev version, and use go build -tags 'prod' to build production version. When I called ko publish ., it told me that failed to publish images: importpath "git.xxx.com/my-project" is not supported

I know that ko will call go build, but how can I pass -tags 'dev' to go build when exec ko publish?

@jonjohnsonjr
Copy link
Collaborator

We don't do a great job of passing through flags to the underlying go build invocation.

Somewhat related: #54 and #52

I dug around for a bit looking for the go build -tags parsing and ran across golang/go@80e7832, which taught me two things:

  1. This tags flag is changing from space-separated to comma-separated in 1.13.
  2. GOFLAGS exists, which should let you work around this (for now).

Now, this isn't that great:

$ GOFLAGS="-tags=dev" ko publish .

Ideally you could just do this:

$ ko publish -tags=dev .

But that is going to be tough... we currently have another flag (--tags, -t) that allows you to specify how the image is tagged in your registry, so the way the above command is getting parsed right now is equivalent to:

$ ko publish -t "ags=dev" .

This fails because ags=dev isn't a valid image tag. If we want to support that, we'll need to change how we handle flag parsing a bit. It might be possible to maintain a go build FlagSet, and do a first pass over argv before handing off flag parsing to cobra, but I haven't looked into it much.

I'm a little worried that the intersection of our flags, go's flags, and kubectl's flags is going to be a maintenance nightmare, but I'm sure we can figure something out.

@dprotaso
Copy link
Contributor

What about an all encompassing flag for build args?

ie. -build-args "-tag 'dev'"

@dprotaso
Copy link
Contributor

The above being a catch-all while more common options could should promoted to first level flags.

@jonjohnsonjr
Copy link
Collaborator

That doesn't seem like a big improvement over GOFLAGS.

@dprotaso
Copy link
Contributor

dprotaso commented Aug 14, 2019 via email

@jonjohnsonjr
Copy link
Collaborator

I think GOFLAGS is sufficient for this for now, going to close this. Feel free to reopen if that doesn't work (or you have a better idea).

@cameronbraid
Copy link

GOFLAGS isnt sufficient for : GOFLAGS="-ldflags=-Xmain.version=1.2.3"

I get flag provided but not defined: -Xmain.version

@jonjohnsonjr
Copy link
Collaborator

GOFLAGS isnt sufficient for : GOFLAGS="-ldflags=-Xmain.version=1.2.3"

Does this work?

GOFLAGS="-ldflags=-X=main.version=1.2.3

@cameronbraid
Copy link

cameronbraid commented May 22, 2020

Yep, thanks heaps

cmd/server/main.go

package main
import (
	"fmt"
)
var Version = "dev"
func main() {
	fmt.Println("version " + Version)
}
go build ./cmd/server; ./server

prints version dev

go build -ldflags="-X=main.Version=1.2.3" ./cmd/server; ./server

printsversion 1.2.3

x=`KO_DOCKER_REPO=ko.local GOFLAGS="-ldflags=-X=main.Version=1.2.3" ko publish ./cmd/server; docker run --rm -it $x

prints
version 1.2.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants