mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
feat(buildsettings): golang and future tools (#3561)
Co-authored-by: Christian Volk <christian.volk@sap.com>
This commit is contained in:
parent
61a6309f02
commit
6247c5dddd
@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/buildsettings"
|
||||
"github.com/SAP/jenkins-library/pkg/certutils"
|
||||
"github.com/SAP/jenkins-library/pkg/command"
|
||||
"github.com/SAP/jenkins-library/pkg/goget"
|
||||
@ -45,6 +46,7 @@ type golangBuildUtils interface {
|
||||
piperhttp.Uploader
|
||||
|
||||
DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error
|
||||
getDockerImageValue(stepName string) (string, error)
|
||||
|
||||
// Add more methods here, or embed additional interfaces, or remove/replace as required.
|
||||
// The golangBuildUtils interface should be descriptive of your runtime dependencies,
|
||||
@ -65,10 +67,14 @@ type golangBuildUtilsBundle struct {
|
||||
// golangBuildUtilsBundle and forward to the implementation of the dependency.
|
||||
}
|
||||
|
||||
func (utils golangBuildUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
||||
func (g *golangBuildUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (g *golangBuildUtilsBundle) getDockerImageValue(stepName string) (string, error) {
|
||||
return getDockerImageValue(stepName)
|
||||
}
|
||||
|
||||
func newGolangBuildUtils(config golangBuildOptions) golangBuildUtils {
|
||||
httpClientOptions := piperhttp.ClientOptions{}
|
||||
|
||||
@ -94,20 +100,20 @@ func newGolangBuildUtils(config golangBuildOptions) golangBuildUtils {
|
||||
return &utils
|
||||
}
|
||||
|
||||
func golangBuild(config golangBuildOptions, telemetryData *telemetry.CustomData) {
|
||||
func golangBuild(config golangBuildOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *golangBuildCommonPipelineEnvironment) {
|
||||
// Utils can be used wherever the command.ExecRunner interface is expected.
|
||||
// It can also be used for example as a mavenExecRunner.
|
||||
utils := newGolangBuildUtils(config)
|
||||
|
||||
// Error situations will be bubbled up until they reach the line below which will then stop execution
|
||||
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
|
||||
err := runGolangBuild(&config, telemetryData, utils)
|
||||
err := runGolangBuild(&config, telemetryData, utils, commonPipelineEnvironment)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatal("execution of golang build failed")
|
||||
}
|
||||
}
|
||||
|
||||
func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomData, utils golangBuildUtils) error {
|
||||
func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomData, utils golangBuildUtils, commonPipelineEnvironment *golangBuildCommonPipelineEnvironment) error {
|
||||
goModFile, err := readGoModFile(utils) // returns nil if go.mod doesnt exist
|
||||
if err != nil {
|
||||
return err
|
||||
@ -196,6 +202,25 @@ func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomD
|
||||
}
|
||||
}
|
||||
|
||||
log.Entry().Debugf("creating build settings information...")
|
||||
stepName := "golangBuild"
|
||||
dockerImage, err := utils.getDockerImageValue(stepName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buildConfig := buildsettings.BuildOptions{
|
||||
CreateBOM: config.CreateBOM,
|
||||
Publish: config.Publish,
|
||||
BuildSettingsInfo: config.BuildSettingsInfo,
|
||||
DockerImage: dockerImage,
|
||||
}
|
||||
buildSettingsInfo, err := buildsettings.CreateBuildSettingsInfo(&buildConfig, stepName)
|
||||
if err != nil {
|
||||
log.Entry().Warnf("failed to create build settings info: %v", err)
|
||||
}
|
||||
commonPipelineEnvironment.custom.buildSettingsInfo = buildSettingsInfo
|
||||
|
||||
if config.Publish {
|
||||
if len(config.TargetRepositoryURL) == 0 {
|
||||
return fmt.Errorf("there's no target repository for binary publishing configured")
|
||||
|
@ -5,6 +5,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
@ -12,6 +13,7 @@ import (
|
||||
"github.com/SAP/jenkins-library/pkg/config"
|
||||
"github.com/SAP/jenkins-library/pkg/gcs"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/piperenv"
|
||||
"github.com/SAP/jenkins-library/pkg/splunk"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
"github.com/SAP/jenkins-library/pkg/validation"
|
||||
@ -21,6 +23,7 @@ import (
|
||||
|
||||
type golangBuildOptions struct {
|
||||
BuildFlags []string `json:"buildFlags,omitempty"`
|
||||
BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"`
|
||||
CgoEnabled bool `json:"cgoEnabled,omitempty"`
|
||||
CoverageFormat string `json:"coverageFormat,omitempty" validate:"possible-values=cobertura html"`
|
||||
CreateBOM bool `json:"createBOM,omitempty"`
|
||||
@ -44,6 +47,34 @@ type golangBuildOptions struct {
|
||||
ArtifactVersion string `json:"artifactVersion,omitempty"`
|
||||
}
|
||||
|
||||
type golangBuildCommonPipelineEnvironment struct {
|
||||
custom struct {
|
||||
buildSettingsInfo string
|
||||
}
|
||||
}
|
||||
|
||||
func (p *golangBuildCommonPipelineEnvironment) persist(path, resourceName string) {
|
||||
content := []struct {
|
||||
category string
|
||||
name string
|
||||
value interface{}
|
||||
}{
|
||||
{category: "custom", name: "buildSettingsInfo", value: p.custom.buildSettingsInfo},
|
||||
}
|
||||
|
||||
errCount := 0
|
||||
for _, param := range content {
|
||||
err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Error("Error persisting piper environment.")
|
||||
errCount++
|
||||
}
|
||||
}
|
||||
if errCount > 0 {
|
||||
log.Entry().Error("failed to persist Piper environment")
|
||||
}
|
||||
}
|
||||
|
||||
type golangBuildReports struct {
|
||||
}
|
||||
|
||||
@ -88,6 +119,7 @@ func GolangBuildCommand() *cobra.Command {
|
||||
metadata := golangBuildMetadata()
|
||||
var stepConfig golangBuildOptions
|
||||
var startTime time.Time
|
||||
var commonPipelineEnvironment golangBuildCommonPipelineEnvironment
|
||||
var reports golangBuildReports
|
||||
var logCollector *log.CollectorHook
|
||||
var splunkClient *splunk.Splunk
|
||||
@ -148,6 +180,7 @@ If the build is successful the resulting artifact can be uploaded to e.g. a bina
|
||||
stepTelemetryData := telemetry.CustomData{}
|
||||
stepTelemetryData.ErrorCode = "1"
|
||||
handler := func() {
|
||||
commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
|
||||
reports.persist(stepConfig, GeneralConfig.GCPJsonKeyFilePath, GeneralConfig.GCSBucketId, GeneralConfig.GCSFolderPath, GeneralConfig.GCSSubFolder)
|
||||
config.RemoveVaultSecretFiles()
|
||||
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
||||
@ -169,7 +202,7 @@ If the build is successful the resulting artifact can be uploaded to e.g. a bina
|
||||
GeneralConfig.HookConfig.SplunkConfig.Index,
|
||||
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
|
||||
}
|
||||
golangBuild(stepConfig, &stepTelemetryData)
|
||||
golangBuild(stepConfig, &stepTelemetryData, &commonPipelineEnvironment)
|
||||
stepTelemetryData.ErrorCode = "0"
|
||||
log.Entry().Info("SUCCESS")
|
||||
},
|
||||
@ -181,6 +214,7 @@ If the build is successful the resulting artifact can be uploaded to e.g. a bina
|
||||
|
||||
func addGolangBuildFlags(cmd *cobra.Command, stepConfig *golangBuildOptions) {
|
||||
cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags to be used.")
|
||||
cmd.Flags().StringVar(&stepConfig.BuildSettingsInfo, "buildSettingsInfo", os.Getenv("PIPER_buildSettingsInfo"), "build settings info is typically filled by the step automatically to create information about the build settings that were used during the maven build . This information is typically used for compliance related processes.")
|
||||
cmd.Flags().BoolVar(&stepConfig.CgoEnabled, "cgoEnabled", false, "If active: enables the creation of Go packages that call C code.")
|
||||
cmd.Flags().StringVar(&stepConfig.CoverageFormat, "coverageFormat", `html`, "Defines the format of the coverage repository.")
|
||||
cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin. It requires Go 1.17 or newer.")
|
||||
@ -229,6 +263,20 @@ func golangBuildMetadata() config.StepData {
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "buildSettingsInfo",
|
||||
ResourceRef: []config.ResourceReference{
|
||||
{
|
||||
Name: "commonPipelineEnvironment",
|
||||
Param: "custom/buildSettingsInfo",
|
||||
},
|
||||
},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_buildSettingsInfo"),
|
||||
},
|
||||
{
|
||||
Name: "cgoEnabled",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
@ -456,6 +504,13 @@ func golangBuildMetadata() config.StepData {
|
||||
},
|
||||
Outputs: config.StepOutputs{
|
||||
Resources: []config.StepResources{
|
||||
{
|
||||
Name: "commonPipelineEnvironment",
|
||||
Type: "piperEnvironment",
|
||||
Parameters: []map[string]interface{}{
|
||||
{"name": "custom/buildSettingsInfo"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "reports",
|
||||
Type: "reports",
|
||||
|
@ -30,51 +30,57 @@ type golangBuildMockUtils struct {
|
||||
fileUploads map[string]string // set by mock
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
||||
func (g *golangBuildMockUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) GetRepositoryURL(module string) (string, error) {
|
||||
func (g *golangBuildMockUtils) GetRepositoryURL(module string) (string, error) {
|
||||
return fmt.Sprintf("https://%s.git", module), nil
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
|
||||
func (g *golangBuildMockUtils) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) SetOptions(options piperhttp.ClientOptions) {
|
||||
utils.clientOptions = append(utils.clientOptions, options)
|
||||
func (g *golangBuildMockUtils) SetOptions(options piperhttp.ClientOptions) {
|
||||
g.clientOptions = append(g.clientOptions, options)
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
|
||||
utils.fileUploads[file] = url
|
||||
func (g *golangBuildMockUtils) UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
|
||||
g.fileUploads[file] = url
|
||||
|
||||
response := http.Response{
|
||||
StatusCode: utils.returnFileUploadStatus,
|
||||
StatusCode: g.returnFileUploadStatus,
|
||||
}
|
||||
|
||||
return &response, utils.returnFileUploadError
|
||||
return &response, g.returnFileUploadError
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
|
||||
return utils.UploadRequest(http.MethodPut, url, file, fieldName, header, cookies, uploadType)
|
||||
func (g *golangBuildMockUtils) UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
|
||||
return g.UploadRequest(http.MethodPut, url, file, fieldName, header, cookies, uploadType)
|
||||
}
|
||||
|
||||
func (utils golangBuildMockUtils) Upload(data piperhttp.UploadRequestData) (*http.Response, error) {
|
||||
func (g *golangBuildMockUtils) Upload(data piperhttp.UploadRequestData) (*http.Response, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func newGolangBuildTestsUtils() golangBuildMockUtils {
|
||||
func (g *golangBuildMockUtils) getDockerImageValue(stepName string) (string, error) {
|
||||
return "golang:latest", nil
|
||||
}
|
||||
|
||||
func newGolangBuildTestsUtils() *golangBuildMockUtils {
|
||||
utils := golangBuildMockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{},
|
||||
FilesMock: &mock.FilesMock{},
|
||||
//clientOptions: []piperhttp.ClientOptions{},
|
||||
fileUploads: map[string]string{},
|
||||
}
|
||||
return utils
|
||||
return &utils
|
||||
}
|
||||
|
||||
func TestRunGolangBuild(t *testing.T) {
|
||||
cpe := golangBuildCommonPipelineEnvironment{}
|
||||
|
||||
t.Run("success - no tests", func(t *testing.T) {
|
||||
config := golangBuildOptions{
|
||||
TargetArchitectures: []string{"linux,amd64"},
|
||||
@ -82,7 +88,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
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)
|
||||
@ -97,7 +103,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
||||
assert.Equal(t, []string{"install", "gotest.tools/gotestsum@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
||||
@ -116,7 +122,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
|
||||
assert.Equal(t, []string{"tool", "cover", "-html", coverageFile, "-o", "coverage.html"}, utils.ExecMockRunner.Calls[2].Params)
|
||||
@ -130,7 +136,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
||||
assert.Equal(t, []string{"install", "gotest.tools/gotestsum@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
||||
@ -155,7 +161,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
||||
assert.Equal(t, []string{"build", "-trimpath", "-o", "testBin-linux.amd64"}, utils.ExecMockRunner.Calls[0].Params)
|
||||
@ -180,7 +186,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
||||
assert.Equal(t, []string{"build", "-trimpath", "-o", "testBin-linux.amd64"}, utils.ExecMockRunner.Calls[0].Params)
|
||||
@ -198,7 +204,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 3, len(utils.ExecMockRunner.Calls))
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
||||
@ -217,7 +223,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.ShouldFailOnCommand = map[string]error{"go install gotest.tools/gotestsum": fmt.Errorf("install failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "failed to install pre-requisite: install failure")
|
||||
})
|
||||
|
||||
@ -229,7 +235,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.ShouldFailOnCommand = map[string]error{"go install github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@latest": fmt.Errorf("install failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "failed to install pre-requisite: install failure")
|
||||
})
|
||||
|
||||
@ -241,7 +247,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.ShouldFailOnCommand = map[string]error{"gotestsum --junitfile": fmt.Errorf("test failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "running tests failed - junit result missing: test failure")
|
||||
})
|
||||
|
||||
@ -255,7 +261,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.AddFile(coverageFile, []byte("some content"))
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "some tests failed")
|
||||
})
|
||||
|
||||
@ -268,7 +274,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.Contains(t, fmt.Sprint(err), "failed to parse ldflagsTemplate")
|
||||
})
|
||||
|
||||
@ -281,7 +287,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.ShouldFailOnCommand = map[string]error{"go build": fmt.Errorf("build failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "failed to run build for linux.amd64: build failure")
|
||||
})
|
||||
|
||||
@ -294,7 +300,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "there's no target repository for binary publishing configured")
|
||||
})
|
||||
|
||||
@ -311,7 +317,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "go.mod file not found")
|
||||
})
|
||||
|
||||
@ -329,7 +335,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.FilesMock.AddFile("go.mod", []byte(""))
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "go.mod doesn't declare a module path")
|
||||
})
|
||||
|
||||
@ -346,7 +352,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "no build descriptor available, supported: [VERSION version.txt go.mod]")
|
||||
})
|
||||
|
||||
@ -365,7 +371,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "couldn't upload artifact, received status code 500")
|
||||
})
|
||||
|
||||
@ -378,7 +384,7 @@ func TestRunGolangBuild(t *testing.T) {
|
||||
utils.ShouldFailOnCommand = map[string]error{"cyclonedx-gomod mod -licenses -test -output bom.xml": fmt.Errorf("BOM creation failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
||||
assert.EqualError(t, err, "BOM creation failed: BOM creation failure")
|
||||
})
|
||||
}
|
||||
@ -718,7 +724,7 @@ go 1.17`
|
||||
config.PrivateModules = tt.globPattern
|
||||
config.PrivateModulesGitToken = tt.gitToken
|
||||
|
||||
err := prepareGolangEnvironment(&config, goModFile, &utils)
|
||||
err := prepareGolangEnvironment(&config, goModFile, utils)
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
assert.Subset(t, os.Environ(), tt.expect.envVars)
|
||||
|
@ -9,10 +9,14 @@ import (
|
||||
)
|
||||
|
||||
type BuildSettings struct {
|
||||
MavenBuild []BuildOptions `json:"mavenBuild,omitempty"`
|
||||
NpmExecuteScripts []BuildOptions `json:"npmExecuteScripts,omitempty"`
|
||||
GolangBuild []BuildOptions `json:"golangBuild,omitempty"`
|
||||
GradleBuild []BuildOptions `json:"gradleBuild,omitempty"`
|
||||
HelmExecute []BuildOptions `json:"helmExecute,omitempty"`
|
||||
KanikoExecute []BuildOptions `json:"kanikoExecute,omitempty"`
|
||||
MavenBuild []BuildOptions `json:"mavenBuild,omitempty"`
|
||||
MtaBuild []BuildOptions `json:"mtaBuild,omitempty"`
|
||||
PythonBuild []BuildOptions `json:"pythonBuild,omitempty"`
|
||||
NpmExecuteScripts []BuildOptions `json:"npmExecuteScripts,omitempty"`
|
||||
}
|
||||
|
||||
type BuildOptions struct {
|
||||
@ -65,24 +69,41 @@ func CreateBuildSettingsInfo(config *BuildOptions, buildTool string) (string, er
|
||||
settings = append(settings, currentBuildSettingsInfo)
|
||||
var err error
|
||||
switch buildTool {
|
||||
case "mavenBuild":
|
||||
case "golangBuild":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
MavenBuild: settings,
|
||||
GolangBuild: settings,
|
||||
})
|
||||
case "npmExecuteScripts":
|
||||
case "gradleBuild":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
NpmExecuteScripts: settings,
|
||||
GradleBuild: settings,
|
||||
})
|
||||
case "helmExecute":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
HelmExecute: settings,
|
||||
})
|
||||
case "kanikoExecute":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
KanikoExecute: settings,
|
||||
})
|
||||
case "mavenBuild":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
MavenBuild: settings,
|
||||
})
|
||||
case "mtaBuild":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
MtaBuild: settings,
|
||||
})
|
||||
case "pythonBuild":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
PythonBuild: settings,
|
||||
})
|
||||
case "npmExecuteScripts":
|
||||
jsonResult, err = json.Marshal(BuildSettings{
|
||||
NpmExecuteScripts: settings,
|
||||
})
|
||||
default:
|
||||
return "", errors.Wrapf(err, "invalid buildTool '%s' for native build - '%s' not supported", buildTool, buildTool)
|
||||
log.Entry().Warningf("buildTool '%s' not supported for creation of build settings", buildTool)
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "Creating build settings failed with json marshalling")
|
||||
|
@ -14,6 +14,31 @@ func TestCreateBuildSettingsInfo(t *testing.T) {
|
||||
buildTool string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
config: BuildOptions{CreateBOM: true},
|
||||
buildTool: "golangBuild",
|
||||
expected: "{\"golangBuild\":[{\"createBOM\":true}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{DockerImage: "golang:latest"},
|
||||
buildTool: "golangBuild",
|
||||
expected: "{\"golangBuild\":[{\"dockerImage\":\"golang:latest\"}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{CreateBOM: true},
|
||||
buildTool: "gradleBuild",
|
||||
expected: "{\"gradleBuild\":[{\"createBOM\":true}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{Publish: true},
|
||||
buildTool: "helmExecute",
|
||||
expected: "{\"helmExecute\":[{\"publish\":true}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{Publish: true},
|
||||
buildTool: "kanikoExecute",
|
||||
expected: "{\"kanikoExecute\":[{\"publish\":true}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{Profiles: []string{"profile1", "profile2"}, CreateBOM: true},
|
||||
buildTool: "mavenBuild",
|
||||
@ -29,12 +54,22 @@ func TestCreateBuildSettingsInfo(t *testing.T) {
|
||||
buildTool: "mtaBuild",
|
||||
expected: "{\"mtaBuild\":[{\"profiles\":[\"release.build\"],\"publish\":true,\"globalSettingsFile\":\"http://nexus.test:8081/nexus/\"}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{CreateBOM: true},
|
||||
buildTool: "pythonBuild",
|
||||
expected: "{\"pythonBuild\":[{\"createBOM\":true}]}",
|
||||
},
|
||||
{
|
||||
config: BuildOptions{CreateBOM: true},
|
||||
buildTool: "npmExecuteScripts",
|
||||
expected: "{\"npmExecuteScripts\":[{\"createBOM\":true}]}",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testTableConfig {
|
||||
builSettings, err := CreateBuildSettingsInfo(&testCase.config, testCase.buildTool)
|
||||
buildSettings, err := CreateBuildSettingsInfo(&testCase.config, testCase.buildTool)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, builSettings, testCase.expected)
|
||||
assert.Equal(t, buildSettings, testCase.expected)
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -22,6 +22,16 @@ spec:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- name: buildSettingsInfo
|
||||
type: string
|
||||
description: build settings info is typically filled by the step automatically to create information about the build settings that were used during the maven build . This information is typically used for compliance related processes.
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
resourceRef:
|
||||
- name: commonPipelineEnvironment
|
||||
param: custom/buildSettingsInfo
|
||||
- name: cgoEnabled
|
||||
type: bool
|
||||
description: "If active: enables the creation of Go packages that call C code."
|
||||
@ -219,6 +229,10 @@ spec:
|
||||
param: artifactVersion
|
||||
outputs:
|
||||
resources:
|
||||
- name: commonPipelineEnvironment
|
||||
type: piperEnvironment
|
||||
params:
|
||||
- name: custom/buildSettingsInfo
|
||||
- name: reports
|
||||
type: reports
|
||||
params:
|
||||
|
Loading…
x
Reference in New Issue
Block a user