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

Document setting ldflags to embed version into docker image / go binary? #167

Closed
StevenACoffman opened this issue May 8, 2020 · 6 comments · Fixed by #280
Closed

Document setting ldflags to embed version into docker image / go binary? #167

StevenACoffman opened this issue May 8, 2020 · 6 comments · Fixed by #280

Comments

@StevenACoffman
Copy link
Contributor

StevenACoffman commented May 8, 2020

Even more updated: It turns out that the update below never worked. GOFLAGS is the only way to set the ldflags at present. Still need to document this fact somewhere to show how to embed git commit SHA1 into docker image / go binary:

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

Update: This issue should now be to document how to set the ldflags to embed git commit SHA1 into docker image / go binary. ~~Use baseBuildOverrides as below.~~The below snippet does not work, and never has:

baseBuildOverrides:
  github.com/tektoncd/pipeline/cmd/controller:
    flags:
    - name: ldflags
      value: "-X github.com/tektoncd/pipeline/pkg/version.PipelineVersion=devel"

We are committed to embedding the SOURCE_COMMIT into both our Docker images and the go binaries built into them. How would we translate that to using ko?

Version information embedded in the software needs to be made deterministic... This makes Git commit ids good candidates as part of version information.

— From reproducible builds version information

Currently we export SOURCE_COMMIT=$(git rev-parse HEAD) and in our Dockerfile we do:

# expect build-time variables, but set some defaults
ARG PROJECT=my-project
ARG APP=mycoolapp
ARG BUILD_DATE
ARG COMMIT_SHA
ARG VERSION

# use the arg values to set the ENV var default
ENV APP=${APP} \
    SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \
    SOURCE_COMMIT=${SOURCE_COMMIT} \
    PROJECT=${PROJECT}

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath \
      -ldflags="-w -s \
      -X github.com/Khan/mycoolapp/version.AppName=${APP} \
      -X github.com/Khan/mycoolapp/version.Date=${SOURCE_DATE_EPOCH} \
      -X github.com/Khan/mycoolapp/version.GitCommit=${SOURCE_COMMIT} \
      -X github.com/Khan/mycoolapp/version.Project=${PROJECT} \
      -extldflags '-static'" -a \
      -o /go/bin/main ./cmd/main.go

The go apps standardly support -version flag to spit out the metadata so we can debug build pipeline problems.

@mattmoor
Copy link
Collaborator

I thought the reproducible builds env var was SOURCE_DATE_EPOCH. I thought we respected that, but I may be mistaken.

@StevenACoffman
Copy link
Contributor Author

StevenACoffman commented Jun 12, 2020

ko does respect SOURCE_DATE_EPOCH but provides no mechanism for injecting the git SHA1 as far as I can tell. Reproducible builds spec says to inject the commit as well, but does not specify the name of the environment variable. SOURCE_COMMIT is the most common choice.

@mattmoor
Copy link
Collaborator

This was added at some point and never properly documented. I'd bet it's exactly what you want:

https://github.com/tektoncd/pipeline/blob/078f08168b2cc3f60ccf7c4acc63c666350eb6b0/.ko.yaml#L12-L16

@StevenACoffman
Copy link
Contributor Author

Ah yes! That is exactly what I wanted! Oh thanks! I am updating the ticket to clarify that this issue should be for documenting that.

@StevenACoffman StevenACoffman changed the title How to embed version into docker image / go binary? Document setting ldflags to embed version into docker image / go binary? Jun 12, 2020
@jonjohnsonjr
Copy link
Collaborator

This was added at some point and never properly documented

Was it? This was discussed in #54 but I don't think anyone did anything with it. The only way I know how to accomplish this today is with GOFLAGS: #69

dlorenc added a commit to dlorenc/build-pipeline that referenced this issue Jul 20, 2020
This section never actually worked in ko. See ko-build/ko#167
We don't appear to be setting the ldflags anywhere else, so maybe we don't need them?
tekton-robot pushed a commit to tektoncd/pipeline that referenced this issue Jul 20, 2020
This section never actually worked in ko. See ko-build/ko#167
We don't appear to be setting the ldflags anywhere else, so maybe we don't need them?
@github-actions
Copy link

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Reopen the issue with /reopen. Mark the issue as
fresh by adding the comment /remove-lifecycle stale.

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

Successfully merging a pull request may close this issue.

3 participants