mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
aa3fb8adb4
* Add telemetry support * First round telemetry * Add telemetry flag * fix: move files to avoid import cycles * add noTelemetry as global config option * Respect telemetry configuration for reporting * add site id, swa endpoint * correct logger initialization * add http logic * rename init method * rename consts & types * convert struct to payload * convert data to payload string * move activation flag out of data structure * extract types to own file * build query using net/url * correct field mapping * extract notify coding to own file * cleanup parameter mapping * preare base data * fix codeclimate issue * correct test case * fill values from env * test all fields * untrack notify.go * ignore empty custom values * cleanup data.go * add test cases * cleanup * add usage reporting to karma step * add usage reporting to step generator * externalise siteID * correct custom field names * test env handling * simplify method signature * revert parameter negation * correct import * adjust golden file * inclease log level * ignore test case * Revert "inclease log level" This reverts commit 70cae0e0296afb2aa9e7d71e83ea70aa83d1a6d7. * add test case for envvars * remove duplicate reporting * remove duplicate reporting * correct format * regenerate checkmarx file * add log message on deactivation * rename function * add comments to understand SWA mapping Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
147 lines
5.6 KiB
Go
147 lines
5.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// GeneralConfigOptions contains all global configuration options for piper binary
|
|
type GeneralConfigOptions struct {
|
|
CustomConfig string
|
|
DefaultConfig []string //ordered list of Piper default configurations. Can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
|
ParametersJSON string
|
|
EnvRootPath string
|
|
NoTelemetry bool
|
|
StageName string
|
|
StepConfigJSON string
|
|
StepMetadata string //metadata to be considered, can be filePath or ENV containing JSON in format 'ENV:MY_ENV_VAR'
|
|
StepName string
|
|
Verbose bool
|
|
}
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "piper",
|
|
Short: "Executes CI/CD steps from project 'Piper' ",
|
|
Long: `
|
|
This project 'Piper' binary provides a CI/CD step libary.
|
|
It contains many steps which can be used within CI/CD systems as well as directly on e.g. a developer's machine.
|
|
`,
|
|
//ToDo: respect stageName to also come from parametersJSON -> first env.STAGE_NAME, second: parametersJSON, third: flag
|
|
}
|
|
|
|
// GeneralConfig contains global configuration flags for piper binary
|
|
var GeneralConfig GeneralConfigOptions
|
|
|
|
// Execute is the starting point of the piper command line tool
|
|
func Execute() {
|
|
|
|
rootCmd.AddCommand(ConfigCommand())
|
|
rootCmd.AddCommand(VersionCommand())
|
|
rootCmd.AddCommand(DetectExecuteScanCommand())
|
|
rootCmd.AddCommand(KarmaExecuteTestsCommand())
|
|
rootCmd.AddCommand(KubernetesDeployCommand())
|
|
rootCmd.AddCommand(XsDeployCommand())
|
|
rootCmd.AddCommand(GithubPublishReleaseCommand())
|
|
rootCmd.AddCommand(GithubCreatePullRequestCommand())
|
|
rootCmd.AddCommand(CheckmarxExecuteScanCommand())
|
|
|
|
addRootFlags(rootCmd)
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func addRootFlags(rootCmd *cobra.Command) {
|
|
|
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.CustomConfig, "customConfig", ".pipeline/config.yml", "Path to the pipeline configuration file")
|
|
rootCmd.PersistentFlags().StringSliceVar(&GeneralConfig.DefaultConfig, "defaultConfig", []string{".pipeline/defaults.yaml"}, "Default configurations, passed as path to yaml file")
|
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.ParametersJSON, "parametersJSON", os.Getenv("PIPER_parametersJSON"), "Parameters to be considered in JSON format")
|
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.EnvRootPath, "envRootPath", ".pipeline", "Root path to Piper pipeline shared environments")
|
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.StageName, "stageName", os.Getenv("STAGE_NAME"), "Name of the stage for which configuration should be included")
|
|
rootCmd.PersistentFlags().StringVar(&GeneralConfig.StepConfigJSON, "stepConfigJSON", os.Getenv("PIPER_stepConfigJSON"), "Step configuration in JSON format")
|
|
rootCmd.PersistentFlags().BoolVar(&GeneralConfig.NoTelemetry, "noTelemetry", false, "Disables telemetry reporting")
|
|
rootCmd.PersistentFlags().BoolVarP(&GeneralConfig.Verbose, "verbose", "v", false, "verbose output")
|
|
|
|
}
|
|
|
|
// PrepareConfig reads step configuration from various sources and merges it (defaults, config file, flags, ...)
|
|
func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName string, options interface{}, openFile func(s string) (io.ReadCloser, error)) error {
|
|
|
|
filters := metadata.GetParameterFilters()
|
|
|
|
// add telemetry parameter "collectTelemetryData" to ALL, GENERAL and PARAMETER filters
|
|
filters.All = append(filters.All, "collectTelemetryData")
|
|
filters.General = append(filters.General, "collectTelemetryData")
|
|
filters.Parameters = append(filters.Parameters, "collectTelemetryData")
|
|
|
|
resourceParams := metadata.GetResourceParameters(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
|
|
|
|
flagValues := config.AvailableFlagValues(cmd, &filters)
|
|
|
|
var myConfig config.Config
|
|
var stepConfig config.StepConfig
|
|
|
|
if len(GeneralConfig.StepConfigJSON) != 0 {
|
|
// ignore config & defaults in favor of passed stepConfigJSON
|
|
stepConfig = config.GetStepConfigWithJSON(flagValues, GeneralConfig.StepConfigJSON, filters)
|
|
} else {
|
|
// use config & defaults
|
|
var customConfig io.ReadCloser
|
|
var err error
|
|
//accept that config file and defaults cannot be loaded since both are not mandatory here
|
|
{
|
|
exists, e := piperutils.FileExists(GeneralConfig.CustomConfig)
|
|
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
if exists {
|
|
if customConfig, err = openFile(GeneralConfig.CustomConfig); err != nil {
|
|
errors.Wrapf(err, "Cannot read '%s'", GeneralConfig.CustomConfig)
|
|
}
|
|
} else {
|
|
log.Entry().Infof("Project config file '%s' does not exist. No project configuration available.", GeneralConfig.CustomConfig)
|
|
customConfig = nil
|
|
}
|
|
}
|
|
var defaultConfig []io.ReadCloser
|
|
for _, f := range GeneralConfig.DefaultConfig {
|
|
//ToDo: support also https as source
|
|
fc, _ := openFile(f)
|
|
defaultConfig = append(defaultConfig, fc)
|
|
}
|
|
|
|
stepConfig, err = myConfig.GetStepConfig(flagValues, GeneralConfig.ParametersJSON, customConfig, defaultConfig, filters, metadata.Spec.Inputs.Parameters, resourceParams, GeneralConfig.StageName, stepName)
|
|
if err != nil {
|
|
return errors.Wrap(err, "retrieving step configuration failed")
|
|
}
|
|
}
|
|
|
|
if fmt.Sprintf("%v", stepConfig.Config["collectTelemetryData"]) == "false" {
|
|
GeneralConfig.NoTelemetry = true
|
|
}
|
|
|
|
if !GeneralConfig.Verbose {
|
|
if stepConfig.Config["verbose"] != nil && stepConfig.Config["verbose"].(bool) {
|
|
log.SetVerbose(stepConfig.Config["verbose"].(bool))
|
|
}
|
|
}
|
|
|
|
confJSON, _ := json.Marshal(stepConfig.Config)
|
|
json.Unmarshal(confJSON, &options)
|
|
|
|
config.MarkFlagsWithValue(cmd, stepConfig)
|
|
|
|
return nil
|
|
}
|