diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index acea2bd33..7d00f7214 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -450,6 +450,9 @@ func runGolangBuildPerArchitecture(config *golangBuildOptions, utils golangBuild if len(ldflags) > 0 { buildOptions = append(buildOptions, "-ldflags", ldflags) } + if config.EntryPointPath != "" { + buildOptions = append(buildOptions, config.EntryPointPath) + } if err := utils.RunExecutable("go", buildOptions...); err != nil { log.Entry().Debugf("buildOptions: %v", buildOptions) diff --git a/cmd/golangBuild_generated.go b/cmd/golangBuild_generated.go index 654ebf5c8..2ce6077b8 100644 --- a/cmd/golangBuild_generated.go +++ b/cmd/golangBuild_generated.go @@ -45,6 +45,7 @@ type golangBuildOptions struct { PrivateModules string `json:"privateModules,omitempty"` PrivateModulesGitToken string `json:"privateModulesGitToken,omitempty"` ArtifactVersion string `json:"artifactVersion,omitempty"` + EntryPointPath string `json:"entryPointPath,omitempty"` } type golangBuildCommonPipelineEnvironment struct { @@ -236,6 +237,7 @@ func addGolangBuildFlags(cmd *cobra.Command, stepConfig *golangBuildOptions) { cmd.Flags().StringVar(&stepConfig.PrivateModules, "privateModules", os.Getenv("PIPER_privateModules"), "Tells go which modules shall be considered to be private (by setting [GOPRIVATE](https://pkg.go.dev/cmd/go#hdr-Configuration_for_downloading_non_public_code)).") cmd.Flags().StringVar(&stepConfig.PrivateModulesGitToken, "privateModulesGitToken", os.Getenv("PIPER_privateModulesGitToken"), "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.") cmd.Flags().StringVar(&stepConfig.ArtifactVersion, "artifactVersion", os.Getenv("PIPER_artifactVersion"), "Version of the artifact to be built.") + cmd.Flags().StringVar(&stepConfig.EntryPointPath, "entryPointPath", os.Getenv("PIPER_entryPointPath"), "Path to the file with main function. If empty, it is assumed that the main function is in the root of the project.") cmd.MarkFlagRequired("targetArchitectures") } @@ -497,6 +499,15 @@ func golangBuildMetadata() config.StepData { Aliases: []config.Alias{}, Default: os.Getenv("PIPER_artifactVersion"), }, + { + Name: "entryPointPath", + ResourceRef: []config.ResourceReference{}, + Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, + Type: "string", + Mandatory: false, + Aliases: []config.Alias{}, + Default: os.Getenv("PIPER_entryPointPath"), + }, }, }, Containers: []config.Container{ diff --git a/cmd/golangBuild_test.go b/cmd/golangBuild_test.go index eb937622b..da4bf8753 100644 --- a/cmd/golangBuild_test.go +++ b/cmd/golangBuild_test.go @@ -84,6 +84,7 @@ func TestRunGolangBuild(t *testing.T) { t.Run("success - no tests", func(t *testing.T) { config := golangBuildOptions{ TargetArchitectures: []string{"linux,amd64"}, + EntryPointPath: "cmd/server.go", } utils := newGolangBuildTestsUtils() telemetryData := telemetry.CustomData{} @@ -91,7 +92,7 @@ func TestRunGolangBuild(t *testing.T) { err := runGolangBuild(&config, &telemetryData, utils, &cpe) assert.NoError(t, err) assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec) - assert.Equal(t, []string{"build", "-trimpath"}, utils.ExecMockRunner.Calls[0].Params) + assert.Equal(t, []string{"build", "-trimpath", "cmd/server.go"}, utils.ExecMockRunner.Calls[0].Params) }) t.Run("success - tests & ldflags", func(t *testing.T) { diff --git a/resources/metadata/golangBuild.yaml b/resources/metadata/golangBuild.yaml index caf41f27f..359a31a7f 100644 --- a/resources/metadata/golangBuild.yaml +++ b/resources/metadata/golangBuild.yaml @@ -227,6 +227,13 @@ spec: resourceRef: - name: commonPipelineEnvironment param: artifactVersion + - name: entryPointPath + type: string + description: Path to the file with main function. If empty, it is assumed that the main function is in the root of the project. + scope: + - PARAMETERS + - STAGES + - STEPS outputs: resources: - name: commonPipelineEnvironment