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

BuildMethod: makefile not passing Version parameter #3158

Open
kaihendry opened this issue Aug 10, 2021 · 9 comments
Open

BuildMethod: makefile not passing Version parameter #3158

kaihendry opened this issue Aug 10, 2021 · 9 comments
Labels

Comments

@kaihendry
Copy link

Description:

https://github.com/kaihendry/aws-sam-gateway-example/blob/master/Makefile#L14 does not appear to embed Version set by

VERSION = $(shell git rev-parse --abbrev-ref HEAD)-$(shell git rev-parse --short HEAD)

Steps to reproduce:

  1. git clone https://github.com/kaihendry/aws-sam-gateway-example.git
  2. edit Makefile with your AWS PROFILE
  3. make

Observed result:

Version appears unset
image

Expected result:

 go build -ldflags "-X main.Version=foobar" -o foo
 PORT=3000 ./foo &
 curl -s localhost:3000 | grep Version
 <h1>Version: foobar</h1>

Additional environment details (Ex: Arch Linux)

  1. OS: I use Arch btw
  2. If using SAM CLI, SAM CLI, version 1.27.2
  3. AWS region: ap-souteast-1

Add --debug flag to any SAM CLI commands you are running

2021-08-10 10:50:07,209 | Running workflow 'CustomMakeBuilder'
2021-08-10 10:50:07,209 | Running CustomMakeBuilder:CopySource
2021-08-10 10:50:07,217 | CustomMakeBuilder:CopySource succeeded
2021-08-10 10:50:07,217 | Running CustomMakeBuilder:MakeBuild
2021-08-10 10:50:07,217 | Current Artifacts Directory : /home/hendry/tmp/aws-sam-gateway-example/.aws-sam/build/MainFunction
2021-08-10 10:50:07,218 | executing Make: ['make', '--makefile', '/home/hendry/tmp/aws-sam-gateway-example/Makefile', 'build-MainFunction']
2021-08-10 10:50:07,835 | CustomMakeBuilder:MakeBuild succeeded
@xazhao
Copy link
Contributor

xazhao commented Aug 10, 2021

Hey @kaihendry, thanks for posting this issue. I was able to reproduce the issue. I tried to hard-code version number by assign the value directly or using shell echo and the version is showing correctly. I don't think it is a bug of SAM. It might be related to how makefile is executed.

@kaihendry
Copy link
Author

When I run make with the VERSION, it works. I think it's got to do something with the way make is executed by sam?

for example, no problem locally with make main:

main:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=$(VERSION)" -o ./main

Which leads me to think that when sam-build is run, it's not properly running GNU Make and evaluating the $(shell).

For example "-X main.Version=$(shell git rev-parse --short HEAD)" doesn't work!

@jfuss jfuss transferred this issue from aws/serverless-application-model Aug 11, 2021
@xazhao
Copy link
Contributor

xazhao commented Aug 11, 2021

Looked into the code and found the cause. When we added makefile build method feature we want to build it in isolation. So we're copying it to a temporary directory and building it there. This is why git commands won't work here (if you use shell pwd you can see where it is excuted).
The solution would be using $(shell git --git-dir your_repo). I verified it on my side and I can see the version.

@kaihendry
Copy link
Author

I'm not sure how you can reliably set your_repo, given this can be cloned out anywhere? I don't want to have to edit the Makefile with the PWD. Or am I missing something??

@kaihendry
Copy link
Author

Is there some env I can rely upon?

@xazhao
Copy link
Contributor

xazhao commented Aug 16, 2021

Using --git-dir is a walkaround I can think of. There is no env variable as far as I know.

I will sync up with the team and post updates here.

@xazhao xazhao added type/feature Feature request area/build sam build command labels Aug 16, 2021
kaihendry added a commit to kaihendry/ltabus that referenced this issue Jul 31, 2022
@super132
Copy link
Contributor

#3349

@joshriverscambia2019
Copy link

A variant of the following worked as a workaround for me:

VERSION = $(shell git --git-dir ${PWD}/.git rev-parse --abbrev-ref HEAD)-$(shell git --git-dir ${PWD}/.git rev-parse --short HEAD)

@kaihendry
Copy link
Author

kaihendry commented Sep 5, 2023

@xazhao ideally the go build happens with the .git directory, so that runtime/debug works. I noticed debug.ReadBuildInfo() is broken in SAM / makefile built go deployments. 🤦

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

No branches or pull requests

5 participants