1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Add gcs upload to mavenExecuteIntegration step (#3432)

* Add gcs upload to mavenExecuteIntegration step

* go generate

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Siarhei Pazdniakou 2022-01-20 10:18:57 +03:00 committed by GitHub
parent c93f1f861d
commit 198fbbeb8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 1 deletions

View File

@ -5,13 +5,17 @@ package cmd
import (
"fmt"
"os"
"reflect"
"strings"
"time"
"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/splunk"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/validation"
"github.com/bmatcuk/doublestar"
"github.com/spf13/cobra"
)
@ -26,6 +30,44 @@ type mavenExecuteIntegrationOptions struct {
LogSuccessfulMavenTransfers bool `json:"logSuccessfulMavenTransfers,omitempty"`
}
type mavenExecuteIntegrationReports struct {
}
func (p *mavenExecuteIntegrationReports) persist(stepConfig mavenExecuteIntegrationOptions, gcpJsonKeyFilePath string, gcsBucketId string, gcsFolderPath string, gcsSubFolder string) {
if gcsBucketId == "" {
log.Entry().Info("persisting reports to GCS is disabled, because gcsBucketId is empty")
return
}
log.Entry().Info("Uploading reports to Google Cloud Storage...")
content := []gcs.ReportOutputParam{
{FilePattern: "**/requirement.mapping", ParamRef: "", StepResultType: "requirement-mapping"},
{FilePattern: "**/TEST-*.xml", ParamRef: "", StepResultType: "junit"},
{FilePattern: "**/integration-test/*.xml", ParamRef: "", StepResultType: "integration-test"},
{FilePattern: "**/jacoco.xml", ParamRef: "", StepResultType: "jacoco-coverage"},
}
envVars := []gcs.EnvVar{
{Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: gcpJsonKeyFilePath, Modified: false},
}
gcsClient, err := gcs.NewClient(gcs.WithEnvVars(envVars))
if err != nil {
log.Entry().Errorf("creation of GCS client failed: %v", err)
}
defer gcsClient.Close()
structVal := reflect.ValueOf(&stepConfig).Elem()
inputParameters := map[string]string{}
for i := 0; i < structVal.NumField(); i++ {
field := structVal.Type().Field(i)
if field.Type.String() == "string" {
paramName := strings.Split(field.Tag.Get("json"), ",")
paramValue, _ := structVal.Field(i).Interface().(string)
inputParameters[paramName[0]] = paramValue
}
}
if err := gcs.PersistReportsToGCS(gcsClient, content, inputParameters, gcsFolderPath, gcsBucketId, gcsSubFolder, doublestar.Glob, os.Stat); err != nil {
log.Entry().Errorf("failed to persist reports: %v", err)
}
}
// MavenExecuteIntegrationCommand This step will execute backend integration tests via the Jacoco Maven-plugin.
func MavenExecuteIntegrationCommand() *cobra.Command {
const STEP_NAME = "mavenExecuteIntegration"
@ -33,6 +75,7 @@ func MavenExecuteIntegrationCommand() *cobra.Command {
metadata := mavenExecuteIntegrationMetadata()
var stepConfig mavenExecuteIntegrationOptions
var startTime time.Time
var reports mavenExecuteIntegrationReports
var logCollector *log.CollectorHook
var splunkClient *splunk.Splunk
telemetryClient := &telemetry.Telemetry{}
@ -85,6 +128,7 @@ the integration tests via the Jacoco Maven-plugin.`,
stepTelemetryData := telemetry.CustomData{}
stepTelemetryData.ErrorCode = "1"
handler := func() {
reports.persist(stepConfig, GeneralConfig.GCPJsonKeyFilePath, GeneralConfig.GCSBucketId, GeneralConfig.GCSFolderPath, GeneralConfig.GCSSubFolder)
config.RemoveVaultSecretFiles()
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
@ -218,6 +262,20 @@ func mavenExecuteIntegrationMetadata() config.StepData {
Sidecars: []config.Container{
{},
},
Outputs: config.StepOutputs{
Resources: []config.StepResources{
{
Name: "reports",
Type: "reports",
Parameters: []map[string]interface{}{
{"filePattern": "**/requirement.mapping", "type": "requirement-mapping"},
{"filePattern": "**/TEST-*.xml", "type": "junit"},
{"filePattern": "**/integration-test/*.xml", "type": "integration-test"},
{"filePattern": "**/jacoco.xml", "type": "jacoco-coverage"},
},
},
},
},
},
}
return theMetaData

View File

@ -95,7 +95,19 @@ spec:
default: false
aliases:
- name: maven/logSuccessfulMavenTransfers
outputs:
resources:
- name: reports
type: reports
params:
- filePattern: "**/requirement.mapping"
type: requirement-mapping
- filePattern: "**/TEST-*.xml"
type: junit
- filePattern: "**/integration-test/*.xml"
type: integration-test
- filePattern: "**/jacoco.xml"
type: jacoco-coverage
containers:
- name: mvn
image: maven:3.6-jdk-8