Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Expand mage and add build options #73

Merged
merged 38 commits into from
Aug 23, 2022
Merged

Expand mage and add build options #73

merged 38 commits into from
Aug 23, 2022

Conversation

narph
Copy link
Contributor

@narph narph commented Jul 6, 2022

Implement build solution using goreleaser (https://goreleaser.com/intro/)

  • align to stack version
  • define in .goreleaser.yaml build options

image

Expand .CI

  • in progress

Expand mage and add build options

  • Run mage to list targets
Targets:
  build:all             builds binaries for the all os/arch.
  build:binary          will create the project binaries found in /build/binaries (use `mage build`) ENV PLATFORM = all, darwin, linux, windows, darwin/amd64, darwin/arm64, linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64 ENV SNAPSHOT = true/false
  build:clean           removes the build directory.
  build:testBinaries    checks if the binaries are generated (for now).
  check                 runs all the checks including licence, notice, gomod, git changes
  checkLicense          checks the license headers
  installGoReleaser     target installs goreleaser
  license               should generate the license headers
  notice                generates a NOTICE.txt file for the module.
  test:all              runs all the tests.
  test:integration      runs all the integration tests (use alias `mage integrationtest`).
  test:unit             runs all the unit tests (use alias `mage unitTest`).

Ex:
mage build:all = ENV PLATFORMS=all mage build builds all binaries
mage build builds binary for host machine
env PLATFORMS=windows mage build to build all binaries for windows
mage unitTest to run all unit tests

@narph narph requested a review from a team as a code owner July 6, 2022 13:24
@narph narph requested review from cmacknz and fearful-symmetry and removed request for a team July 6, 2022 13:24
@narph narph self-assigned this Jul 6, 2022
@narph narph marked this pull request as draft July 6, 2022 13:24
@narph narph requested a review from v1v July 6, 2022 13:25
@elasticmachine
Copy link
Collaborator

elasticmachine commented Jul 6, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-08-23T13:10:42.640+0000

  • Duration: 12 min 54 sec

❕ Flaky test report

No test was executed to be analysed.

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

@narph narph added the Team:Elastic-Agent Label for the Agent team label Jul 6, 2022
.goreleaser.yaml Outdated Show resolved Hide resolved
.goreleaser.yaml Outdated Show resolved Hide resolved
.goreleaser.yaml Outdated Show resolved Hide resolved
.goreleaser.yaml Outdated Show resolved Hide resolved
magefile.go Outdated Show resolved Hide resolved
@v1v
Copy link
Member

v1v commented Jul 6, 2022

/test

magefile.go Outdated Show resolved Hide resolved
Copy link

@ph ph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few comments, but @narph this looks really good thanks for pushing theses changes!

withGoEnv(){
goTestJUnit(options: '-v ./...', output: 'junit-report.xml')
withMageEnv(){
cmd(label: 'Go unitTest', script: 'mage unitTest')
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not familiar with the withMageEnv call but I assume it generates the junit-report.xml ?

- amd64
- arm64
flags:
- -buildmode=pie
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have more flags than that? like -s?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Does goreleaser support creating universal binary or we have to have a post build hook to merge both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- '{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}'
changelog:
skip: true
dist: build/binaries
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be simple to share with different projects.

func GoUnitTest(ctx context.Context) error {
mg.Deps(InstallGoTestTools)

fmt.Println(">> go test:", "Unit Testing") //nolint:forbidigo // just for tests
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you are using a Print* methods and a writer, it should not mark any usage.

out := os.stdout
fmt.Fprintln(out, >> go test:", "Unit Testing) 

The above allow you to enable the output or not.

@cmacknz cmacknz requested review from rdner and faec August 16, 2022 15:22
@cmacknz
Copy link
Member

cmacknz commented Aug 16, 2022

Running mage build:all now fails on my M1 Mac, trying to figure out if it's my machine or something in the build itself:

❯ mage build:all
>> build: Building binary for all
   • building...
   • loading config file       file=.goreleaser.yaml
   • loading environment variables
   • getting and validating git state
      • building...               commit=bdadcedcd64c77502e7b575303f836e8208e6b38 latest tag=v0.2.0
      • pipe skipped              error=validation is disabled
   • parsing tag
   • running before hooks
      • running                   hook=go mod tidy
      • running                   hook=go generate ./...
   • setting defaults
   • checking distribution directory
      • --rm-dist is set, cleaning it up
   • loading go mod information
   • build prerequisites
   • writing effective config file
      • writing                   config=build/binaries/config.yaml
   • building binaries
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-darwin-aarch64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-darwin-x86_64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-linux-x86_64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-linux-arm64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-linux-x86/elastic-agent-shipper
   ⨯ build failed after 2.76s error=failed to build for linux_386: exit status 2: # github.com/elastic/elastic-agent-shipper
loadinternal: cannot find runtime/cgo
/opt/homebrew/Cellar/go/1.18.4/libexec/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]
ld: unknown/unsupported architecture name for: -arch armv4t
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@cmacknz
Copy link
Member

cmacknz commented Aug 16, 2022

failed to build for linux_386 do we need to build for the 32-bit i386 arch? I don't think the agent supports 32 bit binaries right now.

@ph
Copy link

ph commented Aug 16, 2022

Afaik we don't need to support 32bits https://www.elastic.co/support/matrix

@narph
Copy link
Contributor Author

narph commented Aug 22, 2022

Running mage build:all now fails on my M1 Mac, trying to figure out if it's my machine or something in the build itself:

❯ mage build:all
>> build: Building binary for all
   • building...
   • loading config file       file=.goreleaser.yaml
   • loading environment variables
   • getting and validating git state
      • building...               commit=bdadcedcd64c77502e7b575303f836e8208e6b38 latest tag=v0.2.0
      • pipe skipped              error=validation is disabled
   • parsing tag
   • running before hooks
      • running                   hook=go mod tidy
      • running                   hook=go generate ./...
   • setting defaults
   • checking distribution directory
      • --rm-dist is set, cleaning it up
   • loading go mod information
   • build prerequisites
   • writing effective config file
      • writing                   config=build/binaries/config.yaml
   • building binaries
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-darwin-aarch64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-darwin-x86_64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-linux-x86_64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-linux-arm64/elastic-agent-shipper
      • building                  binary=build/binaries/elastic-agent-shipper-8.5.0-linux-x86/elastic-agent-shipper
   ⨯ build failed after 2.76s error=failed to build for linux_386: exit status 2: # github.com/elastic/elastic-agent-shipper
loadinternal: cannot find runtime/cgo
/opt/homebrew/Cellar/go/1.18.4/libexec/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
clang: warning: argument unused during compilation: '-pie' [-Wunused-command-line-argument]
ld: unknown/unsupported architecture name for: -arch armv4t
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@cmacknz , the build has cgo disabled (implicitly enabling static linking) and uses Go's native cross compilation which simplifies the builds.
This requires disabling -buildmode=pie for 386, which cannot be done except when using external linking, we do the same for fleet server builds (https://github.com/elastic/fleet-server/blob/main/Makefile#L8 build mode missing for linux/386), I've removed the flag so it should work now.



# VSCode
/.vscode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why viscose disappeared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's still there, in #directories

@cmacknz
Copy link
Member

cmacknz commented Aug 22, 2022

mage build
>> build: Building binary for
   • debug logs enabled
   • building...
   • loading config file       file=.goreleaser.yaml
   • loaded config file        config={ [] {   false false   [] []    } [] [] [] [] [] { {   }  { }      []  [] []} [{darwin [darwin] [amd64 arm64] [] [] [] []   {{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "arm64" }}aarch64{{ end }}/{{ .ProjectName }} {[] []} []   false  true   {[{{ if eq .Env.DEV "false" }}-s -w{{ end }} -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}}] [] [-buildmode=pie] [] [{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}]} []} {linux [linux] [amd64 arm64 386] [] [] [] []   {{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "386" }}x86{{ end }}{{ if eq .Arch "arm64" }}{{ .Arch }}{{ end }}/{{ .ProjectName }} {[] []} []   false  true   {[{{ if eq .Env.DEV "false" }}-s -w{{end}} -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}}] [] [] [] [{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}]} [{linux amd64   {[] [] [-buildmode=pie] [] []}} {linux arm   {[] [] [-buildmode=pie] [] []}}]} {windows [windows] [amd64 386] [] [] [] []   {{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "386" }}x86{{ end }}/{{ .ProjectName }} {[] []} []   false  true   {[{{ if eq .Env.DEV "false" }}-s -w{{ end }} -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}}] [] [-buildmode=pie] [] [{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}]} []}] [] [] [] {} {  [] false []} [] [] [] [] [] [] {{[]}  true  []} build/binaries [] [] {  } {[go mod tidy go generate ./...]} {  false } {false [] } { {false } {false     } {false     } {false    } {false    } {false  0   []   false} {false       } {false } {false  0} {false false   map[] }} [] [] { [] [] [] [] [] []    {[] []} []   false  false   {[] [] [] [] []} []} {   false} {  false false} {  false}}
   ⨯ build failed after 0.00s error=yaml: unmarshal errors:
  line 39: field goamd64 not found in type config.BuildDetailsOverride
  line 46: field goamd64 not found in type config.BuildDetailsOverride

The build is still failing for me, let me know if you need me to investigate anything.

if val := os.Getenv(buildEnv); val != "" {
boolVal, err := strconv.ParseBool(val)
if err != nil {
return defaultValue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we shoot a warning here? maybe you want to know you provided incorrect value, may be a typo

)
}

testArgs = append(testArgs, []string{"./..."}...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is windows ok with /?

var (
goLicenserRepo = "github.com/elastic/go-licenser@v0.4.1"
const (
GoreleaserRepo = "github.com/goreleaser/goreleaser@v1.6.3"
Copy link
Contributor

@michalpristas michalpristas Aug 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please ignore this if you don't like it, but I would separate .1.6.3 to a variable ending with Version so in case I forgot where it is defined I can guess based on full text search

Copy link
Contributor

@michalpristas michalpristas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few comments along the way, but overall it looks good

.goreleaser.yaml Show resolved Hide resolved
}
if isSnapshot {
args = append(args, "--snapshot")
env["DEFAULT_VERSION"] += fmt.Sprintf("-%s", "SNAPSHOT")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion]
the fmt.Sprintf is unnecessary

Suggested change
env["DEFAULT_VERSION"] += fmt.Sprintf("-%s", "SNAPSHOT")
env["DEFAULT_VERSION"] += "-SNAPSHOT"

return devtools.GoUnitTest(ctx, testCoverage)
}

//CHECKS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT / Consistency]

Suggested change
//CHECKS
// CHECKS

magefile.go Outdated
}

// CheckLicense checks the license headers
// License should generate the license headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion]
Personally, I find better to have a function description to be assertive, it does either A or B, not it "should" do something.

Suggested change
// License should generate the license headers
// License generates the license headers or returns an error

@narph
Copy link
Contributor Author

narph commented Aug 23, 2022

mage build
>> build: Building binary for
   • debug logs enabled
   • building...
   • loading config file       file=.goreleaser.yaml
   • loaded config file        config={ [] {   false false   [] []    } [] [] [] [] [] { {   }  { }      []  [] []} [{darwin [darwin] [amd64 arm64] [] [] [] []   {{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "arm64" }}aarch64{{ end }}/{{ .ProjectName }} {[] []} []   false  true   {[{{ if eq .Env.DEV "false" }}-s -w{{ end }} -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}}] [] [-buildmode=pie] [] [{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}]} []} {linux [linux] [amd64 arm64 386] [] [] [] []   {{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "386" }}x86{{ end }}{{ if eq .Arch "arm64" }}{{ .Arch }}{{ end }}/{{ .ProjectName }} {[] []} []   false  true   {[{{ if eq .Env.DEV "false" }}-s -w{{end}} -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}}] [] [] [] [{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}]} [{linux amd64   {[] [] [-buildmode=pie] [] []}} {linux arm   {[] [] [-buildmode=pie] [] []}}]} {windows [windows] [amd64 386] [] [] [] []   {{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "386" }}x86{{ end }}/{{ .ProjectName }} {[] []} []   false  true   {[{{ if eq .Env.DEV "false" }}-s -w{{ end }} -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}}] [] [-buildmode=pie] [] [{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}]} []}] [] [] [] {} {  [] false []} [] [] [] [] [] [] {{[]}  true  []} build/binaries [] [] {  } {[go mod tidy go generate ./...]} {  false } {false [] } { {false } {false     } {false     } {false    } {false    } {false  0   []   false} {false       } {false } {false  0} {false false   map[] }} [] [] { [] [] [] [] [] []    {[] []} []   false  false   {[] [] [] [] []} []} {   false} {  false false} {  false}}
   ⨯ build failed after 0.00s error=yaml: unmarshal errors:
  line 39: field goamd64 not found in type config.BuildDetailsOverride
  line 46: field goamd64 not found in type config.BuildDetailsOverride

The build is still failing for me, let me know if you need me to investigate anything.

I ran locally with a recent version of goreleaser which contains goamd64 field in BuildDetailsOverride schema, removed it as it made no difference to the logic, can you try again?

@cmacknz
Copy link
Member

cmacknz commented Aug 23, 2022

Build is fixed for me, thanks!

@narph narph merged commit b855ef3 into elastic:main Aug 23, 2022
@narph narph deleted the add-build branch August 23, 2022 14:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants