2020-02-25 15:33:34 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperenv"
2021-05-17 12:14:04 +02:00
"github.com/SAP/jenkins-library/pkg/splunk"
2020-02-25 15:33:34 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/spf13/cobra"
)
type mtaBuildOptions struct {
MtarName string ` json:"mtarName,omitempty" `
Extensions string ` json:"extensions,omitempty" `
Platform string ` json:"platform,omitempty" `
ApplicationName string ` json:"applicationName,omitempty" `
2021-06-14 16:06:47 +02:00
Source string ` json:"source,omitempty" `
Target string ` json:"target,omitempty" `
2020-02-25 15:33:34 +02:00
DefaultNpmRegistry string ` json:"defaultNpmRegistry,omitempty" `
ProjectSettingsFile string ` json:"projectSettingsFile,omitempty" `
GlobalSettingsFile string ` json:"globalSettingsFile,omitempty" `
2020-06-15 12:46:54 +02:00
M2Path string ` json:"m2Path,omitempty" `
2020-06-22 10:39:52 +02:00
InstallArtifacts bool ` json:"installArtifacts,omitempty" `
2020-02-25 15:33:34 +02:00
}
type mtaBuildCommonPipelineEnvironment struct {
mtarFilePath string
}
func ( p * mtaBuildCommonPipelineEnvironment ) persist ( path , resourceName string ) {
content := [ ] struct {
category string
name string
2020-10-05 15:33:28 +02:00
value interface { }
2020-02-25 15:33:34 +02:00
} {
{ category : "" , name : "mtarFilePath" , value : p . mtarFilePath } ,
}
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 {
2020-04-20 16:52:57 +02:00
log . Entry ( ) . Fatal ( "failed to persist Piper environment" )
2020-02-25 15:33:34 +02:00
}
}
// MtaBuildCommand Performs an mta build
func MtaBuildCommand ( ) * cobra . Command {
2020-04-28 07:42:02 +02:00
const STEP_NAME = "mtaBuild"
2020-02-25 15:33:34 +02:00
metadata := mtaBuildMetadata ( )
var stepConfig mtaBuildOptions
var startTime time . Time
var commonPipelineEnvironment mtaBuildCommonPipelineEnvironment
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-02-25 15:33:34 +02:00
var createMtaBuildCmd = & cobra . Command {
2020-04-28 07:42:02 +02:00
Use : STEP_NAME ,
2020-02-25 15:33:34 +02:00
Short : "Performs an mta build" ,
Long : ` Executes the SAP Multitarget Application Archive Builder to create an mtar archive of the application. ` ,
2020-06-23 18:05:21 +02:00
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-02-25 15:33:34 +02:00
startTime = time . Now ( )
2020-04-28 07:42:02 +02:00
log . SetStepName ( STEP_NAME )
2020-02-25 15:33:34 +02:00
log . SetVerbose ( GeneralConfig . Verbose )
2020-04-28 07:42:02 +02:00
path , _ := os . Getwd ( )
fatalHook := & log . FatalHook { CorrelationID : GeneralConfig . CorrelationID , Path : path }
log . RegisterHook ( fatalHook )
err := PrepareConfig ( cmd , & metadata , STEP_NAME , & stepConfig , config . OpenPiperFile )
2020-04-16 14:37:45 +02:00
if err != nil {
2020-06-24 10:04:05 +02:00
log . SetErrorCategory ( log . ErrorConfiguration )
2020-04-16 14:37:45 +02:00
return err
}
2020-05-05 08:36:24 +02:00
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
logCollector = & log . CollectorHook { CorrelationID : GeneralConfig . CorrelationID }
log . RegisterHook ( logCollector )
}
2020-04-16 14:37:45 +02:00
return nil
2020-02-25 15:33:34 +02:00
} ,
2020-06-23 18:05:21 +02:00
Run : func ( _ * cobra . Command , _ [ ] string ) {
2020-02-25 15:33:34 +02:00
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2020-02-25 15:33:34 +02:00
commonPipelineEnvironment . persist ( GeneralConfig . EnvRootPath , "commonPipelineEnvironment" )
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
2020-09-29 13:49:40 +02:00
telemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
2020-02-25 15:33:34 +02:00
telemetry . Send ( & telemetryData )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Send ( & telemetryData , logCollector )
}
2020-02-25 15:33:34 +02:00
}
log . DeferExitHandler ( handler )
defer handler ( )
2020-04-28 07:42:02 +02:00
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
}
2020-02-25 15:33:34 +02:00
mtaBuild ( stepConfig , & telemetryData , & commonPipelineEnvironment )
telemetryData . ErrorCode = "0"
2020-05-18 21:52:54 +02:00
log . Entry ( ) . Info ( "SUCCESS" )
2020-02-25 15:33:34 +02:00
} ,
}
addMtaBuildFlags ( createMtaBuildCmd , & stepConfig )
return createMtaBuildCmd
}
func addMtaBuildFlags ( cmd * cobra . Command , stepConfig * mtaBuildOptions ) {
cmd . Flags ( ) . StringVar ( & stepConfig . MtarName , "mtarName" , os . Getenv ( "PIPER_mtarName" ) , "The name of the generated mtar file including its extension." )
cmd . Flags ( ) . StringVar ( & stepConfig . Extensions , "extensions" , os . Getenv ( "PIPER_extensions" ) , "The path to the extension descriptor file." )
2021-05-19 08:45:38 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Platform , "platform" , ` CF ` , "The target platform to which the mtar can be deployed." )
2020-02-25 15:33:34 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ApplicationName , "applicationName" , os . Getenv ( "PIPER_applicationName" ) , "The name of the application which is being built. If the parameter has been provided and no `mta.yaml` exists, the `mta.yaml` will be automatically generated using this parameter and the information (`name` and `version`) from 'package.json` before the actual build starts." )
2021-06-14 16:06:47 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . Source , "source" , ` ./ ` , "The path to the MTA project." )
cmd . Flags ( ) . StringVar ( & stepConfig . Target , "target" , ` ./ ` , "The folder for the generated `MTAR` file. If the parameter has been provided, the `MTAR` file is saved in the root of the folder provided by the argument." )
2020-02-25 15:33:34 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . DefaultNpmRegistry , "defaultNpmRegistry" , os . Getenv ( "PIPER_defaultNpmRegistry" ) , "Url to the npm registry that should be used for installing npm dependencies." )
cmd . Flags ( ) . StringVar ( & stepConfig . ProjectSettingsFile , "projectSettingsFile" , os . Getenv ( "PIPER_projectSettingsFile" ) , "Path or url to the mvn settings file that should be used as project settings file." )
cmd . Flags ( ) . StringVar ( & stepConfig . GlobalSettingsFile , "globalSettingsFile" , os . Getenv ( "PIPER_globalSettingsFile" ) , "Path or url to the mvn settings file that should be used as global settings file" )
2020-06-15 12:46:54 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . M2Path , "m2Path" , os . Getenv ( "PIPER_m2Path" ) , "Path to the location of the local repository that should be used." )
2020-09-24 07:41:06 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . InstallArtifacts , "installArtifacts" , false , "If enabled, for npm packages this step will install all dependencies including dev dependencies. For maven it will install all artifacts to the local maven repository. Note: This happens _after_ mta build was done. The default mta build tool does not install dev-dependencies as part of the process. If you require dev-dependencies for building the mta, you will need to use a [custom builder](https://sap.github.io/cloud-mta-build-tool/configuration/#configuring-the-custom-builder)" )
2020-02-25 15:33:34 +02:00
}
// retrieve step metadata
func mtaBuildMetadata ( ) config . StepData {
var theMetaData = config . StepData {
2020-03-19 18:24:35 +02:00
Metadata : config . StepMetadata {
2020-11-20 09:13:59 +02:00
Name : "mtaBuild" ,
Aliases : [ ] config . Alias { } ,
Description : "Performs an mta build" ,
2020-03-19 18:24:35 +02:00
} ,
2020-02-25 15:33:34 +02:00
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{
Name : "mtarName" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_mtarName" ) ,
2020-02-25 15:33:34 +02:00
} ,
{
Name : "extensions" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
2020-05-19 17:05:11 +02:00
Aliases : [ ] config . Alias { { Name : "extension" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_extensions" ) ,
2020-02-25 15:33:34 +02:00
} ,
{
Name : "platform" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : ` CF ` ,
2020-02-25 15:33:34 +02:00
} ,
{
Name : "applicationName" ,
2021-06-14 16:06:47 +02:00
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_applicationName" ) ,
2021-06-14 16:06:47 +02:00
} ,
{
Name : "source" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : ` ./ ` ,
2021-06-14 16:06:47 +02:00
} ,
{
Name : "target" ,
2020-02-25 15:33:34 +02:00
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : ` ./ ` ,
2020-02-25 15:33:34 +02:00
} ,
{
Name : "defaultNpmRegistry" ,
ResourceRef : [ ] config . ResourceReference { } ,
2020-07-29 15:39:41 +02:00
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
2020-02-25 15:33:34 +02:00
Type : "string" ,
Mandatory : false ,
2020-07-29 15:39:41 +02:00
Aliases : [ ] config . Alias { { Name : "npm/defaultNpmRegistry" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_defaultNpmRegistry" ) ,
2020-02-25 15:33:34 +02:00
} ,
{
Name : "projectSettingsFile" ,
ResourceRef : [ ] config . ResourceReference { } ,
2020-07-29 15:39:41 +02:00
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
2020-02-25 15:33:34 +02:00
Type : "string" ,
Mandatory : false ,
2020-07-29 15:39:41 +02:00
Aliases : [ ] config . Alias { { Name : "maven/projectSettingsFile" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_projectSettingsFile" ) ,
2020-02-25 15:33:34 +02:00
} ,
{
Name : "globalSettingsFile" ,
ResourceRef : [ ] config . ResourceReference { } ,
2020-07-29 15:39:41 +02:00
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
2020-02-25 15:33:34 +02:00
Type : "string" ,
Mandatory : false ,
2020-07-29 15:39:41 +02:00
Aliases : [ ] config . Alias { { Name : "maven/globalSettingsFile" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_globalSettingsFile" ) ,
2020-02-25 15:33:34 +02:00
} ,
2020-06-15 12:46:54 +02:00
{
Name : "m2Path" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { { Name : "maven/m2Path" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_m2Path" ) ,
2020-06-15 12:46:54 +02:00
} ,
2020-06-22 10:39:52 +02:00
{
Name : "installArtifacts" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : false ,
2020-06-22 10:39:52 +02:00
} ,
2020-02-25 15:33:34 +02:00
} ,
} ,
2020-11-20 09:13:59 +02:00
Containers : [ ] config . Container {
2021-05-19 08:45:38 +02:00
{ Image : "devxci/mbtci:1.1.1" } ,
2020-11-20 09:13:59 +02:00
} ,
Outputs : config . StepOutputs {
Resources : [ ] config . StepResources {
{
Name : "commonPipelineEnvironment" ,
Type : "piperEnvironment" ,
Parameters : [ ] map [ string ] interface { } {
{ "Name" : "mtarFilePath" } ,
} ,
} ,
} ,
} ,
2020-02-25 15:33:34 +02:00
} ,
}
return theMetaData
}