github.com/xgoffin/jenkins-library@v1.154.0/cmd/gctsDeploy_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type gctsDeployOptions struct {
    19  	Username            string                 `json:"username,omitempty"`
    20  	Password            string                 `json:"password,omitempty"`
    21  	Repository          string                 `json:"repository,omitempty"`
    22  	Host                string                 `json:"host,omitempty"`
    23  	Client              string                 `json:"client,omitempty"`
    24  	Commit              string                 `json:"commit,omitempty"`
    25  	RemoteRepositoryURL string                 `json:"remoteRepositoryURL,omitempty"`
    26  	Role                string                 `json:"role,omitempty" validate:"possible-values=SOURCE TARGET"`
    27  	VSID                string                 `json:"vSID,omitempty"`
    28  	Type                string                 `json:"type,omitempty" validate:"possible-values=GIT GITHUB GITLAB"`
    29  	Branch              string                 `json:"branch,omitempty"`
    30  	Scope               string                 `json:"scope,omitempty"`
    31  	Rollback            bool                   `json:"rollback,omitempty"`
    32  	Configuration       map[string]interface{} `json:"configuration,omitempty"`
    33  }
    34  
    35  // GctsDeployCommand Deploys a Git Repository to a local Repository and then to an ABAP System
    36  func GctsDeployCommand() *cobra.Command {
    37  	const STEP_NAME = "gctsDeploy"
    38  
    39  	metadata := gctsDeployMetadata()
    40  	var stepConfig gctsDeployOptions
    41  	var startTime time.Time
    42  	var logCollector *log.CollectorHook
    43  	var splunkClient *splunk.Splunk
    44  	telemetryClient := &telemetry.Telemetry{}
    45  
    46  	var createGctsDeployCmd = &cobra.Command{
    47  		Use:   STEP_NAME,
    48  		Short: "Deploys a Git Repository to a local Repository and then to an ABAP System",
    49  		Long: `This step deploys a remote Git repository to a local repository on an ABAP system and imports the content in the ABAP database.
    50  If the repository does not yet exist in the system, this step also creates it.
    51  If the repository already exists on the ABAP system, this step executes the remaining actions of the step, depending on the parameters provided for the step.
    52  These actions include, for example, deploy a specific commit to the ABAP system, or deploy the current commit of a specific branch.
    53  You can use this step for gCTS as of SAP S/4HANA 2020.`,
    54  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    55  			startTime = time.Now()
    56  			log.SetStepName(STEP_NAME)
    57  			log.SetVerbose(GeneralConfig.Verbose)
    58  
    59  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    60  
    61  			path, _ := os.Getwd()
    62  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    63  			log.RegisterHook(fatalHook)
    64  
    65  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    66  			if err != nil {
    67  				log.SetErrorCategory(log.ErrorConfiguration)
    68  				return err
    69  			}
    70  			log.RegisterSecret(stepConfig.Username)
    71  			log.RegisterSecret(stepConfig.Password)
    72  
    73  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    74  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    75  				log.RegisterHook(&sentryHook)
    76  			}
    77  
    78  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    79  				splunkClient = &splunk.Splunk{}
    80  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    81  				log.RegisterHook(logCollector)
    82  			}
    83  
    84  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    85  			if err != nil {
    86  				return err
    87  			}
    88  			if err = validation.ValidateStruct(stepConfig); err != nil {
    89  				log.SetErrorCategory(log.ErrorConfiguration)
    90  				return err
    91  			}
    92  
    93  			return nil
    94  		},
    95  		Run: func(_ *cobra.Command, _ []string) {
    96  			stepTelemetryData := telemetry.CustomData{}
    97  			stepTelemetryData.ErrorCode = "1"
    98  			handler := func() {
    99  				config.RemoveVaultSecretFiles()
   100  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
   101  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   102  				stepTelemetryData.PiperCommitHash = GitCommit
   103  				telemetryClient.SetData(&stepTelemetryData)
   104  				telemetryClient.Send()
   105  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   106  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   107  				}
   108  			}
   109  			log.DeferExitHandler(handler)
   110  			defer handler()
   111  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   112  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   113  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   114  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   115  					GeneralConfig.HookConfig.SplunkConfig.Token,
   116  					GeneralConfig.HookConfig.SplunkConfig.Index,
   117  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   118  			}
   119  			gctsDeploy(stepConfig, &stepTelemetryData)
   120  			stepTelemetryData.ErrorCode = "0"
   121  			log.Entry().Info("SUCCESS")
   122  		},
   123  	}
   124  
   125  	addGctsDeployFlags(createGctsDeployCmd, &stepConfig)
   126  	return createGctsDeployCmd
   127  }
   128  
   129  func addGctsDeployFlags(cmd *cobra.Command, stepConfig *gctsDeployOptions) {
   130  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User that authenticates to the ABAP system. **Note** - Don't provide this parameter directly. Either set it in the environment, or in the Jenkins credentials store, and provide the ID as value of the `abapCredentialsId` parameter.")
   131  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password of the ABAP user that authenticates to the ABAP system. **Note** - DonĀ“t provide this parameter directly. Either set it in the environment, or in the Jenkins credentials store, and provide the ID as value of the `abapCredentialsId` parameter.")
   132  	cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Specifies the name (ID) of the local repsitory on the ABAP system")
   133  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Protocol and host of the ABAP system, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.")
   134  	cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Client of the ABAP system to which you want to deploy the repository")
   135  	cmd.Flags().StringVar(&stepConfig.Commit, "commit", os.Getenv("PIPER_commit"), "ID of a specific commit, if you want to deploy the content of the specified commit.")
   136  	cmd.Flags().StringVar(&stepConfig.RemoteRepositoryURL, "remoteRepositoryURL", os.Getenv("PIPER_remoteRepositoryURL"), "URL of the remote repository")
   137  	cmd.Flags().StringVar(&stepConfig.Role, "role", `SOURCE`, "Role of the local repository. Possible values are 'SOURCE' (for repositories on development systems - Default) and 'TARGET' (for repositories on target systems). Local repositories with a TARGET role cannot be the source of code changes.")
   138  	cmd.Flags().StringVar(&stepConfig.VSID, "vSID", os.Getenv("PIPER_vSID"), "Virtual SID of the local repository. The vSID corresponds to the transport route that delivers content to the remote Git repository. For more information, see [Background Information - vSID](https://help.sap.com/viewer/4a368c163b08418890a406d413933ba7/latest/en-US/8edc17edfc374908bd8a1615ea5ab7b7.html) on SAP Help Portal.")
   139  	cmd.Flags().StringVar(&stepConfig.Type, "type", `GIT`, "Type of the used source code management tool")
   140  	cmd.Flags().StringVar(&stepConfig.Branch, "branch", os.Getenv("PIPER_branch"), "Name of a branch, if you want to deploy the content of a specific branch to the ABAP system.")
   141  	cmd.Flags().StringVar(&stepConfig.Scope, "scope", os.Getenv("PIPER_scope"), "Scope of objects to be deployed. Possible values are CRNTCOMMIT (current commit - Default) and LASTACTION (last repository action). The default option deploys all objects that existed in the repository when the commit was created. LASTACTION only deploys the object difference of the last action in the repository.")
   142  	cmd.Flags().BoolVar(&stepConfig.Rollback, "rollback", false, "Indication whether you want to roll back to the last working state of the repository, if any of the step actions *switch branch* or *pull commit* fail.")
   143  
   144  	cmd.MarkFlagRequired("username")
   145  	cmd.MarkFlagRequired("password")
   146  	cmd.MarkFlagRequired("repository")
   147  	cmd.MarkFlagRequired("host")
   148  	cmd.MarkFlagRequired("client")
   149  	cmd.MarkFlagRequired("remoteRepositoryURL")
   150  }
   151  
   152  // retrieve step metadata
   153  func gctsDeployMetadata() config.StepData {
   154  	var theMetaData = config.StepData{
   155  		Metadata: config.StepMetadata{
   156  			Name:        "gctsDeploy",
   157  			Aliases:     []config.Alias{},
   158  			Description: "Deploys a Git Repository to a local Repository and then to an ABAP System",
   159  		},
   160  		Spec: config.StepSpec{
   161  			Inputs: config.StepInputs{
   162  				Secrets: []config.StepSecrets{
   163  					{Name: "abapCredentialsId", Description: "ID taken from the Jenkins credentials store containing the user name and password of the user that authenticates to the ABAP system.", Type: "jenkins"},
   164  				},
   165  				Parameters: []config.StepParameters{
   166  					{
   167  						Name: "username",
   168  						ResourceRef: []config.ResourceReference{
   169  							{
   170  								Name:  "abapCredentialsId",
   171  								Param: "username",
   172  								Type:  "secret",
   173  							},
   174  						},
   175  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   176  						Type:      "string",
   177  						Mandatory: true,
   178  						Aliases:   []config.Alias{},
   179  						Default:   os.Getenv("PIPER_username"),
   180  					},
   181  					{
   182  						Name: "password",
   183  						ResourceRef: []config.ResourceReference{
   184  							{
   185  								Name:  "abapCredentialsId",
   186  								Param: "password",
   187  								Type:  "secret",
   188  							},
   189  						},
   190  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   191  						Type:      "string",
   192  						Mandatory: true,
   193  						Aliases:   []config.Alias{},
   194  						Default:   os.Getenv("PIPER_password"),
   195  					},
   196  					{
   197  						Name:        "repository",
   198  						ResourceRef: []config.ResourceReference{},
   199  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   200  						Type:        "string",
   201  						Mandatory:   true,
   202  						Aliases:     []config.Alias{},
   203  						Default:     os.Getenv("PIPER_repository"),
   204  					},
   205  					{
   206  						Name:        "host",
   207  						ResourceRef: []config.ResourceReference{},
   208  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   209  						Type:        "string",
   210  						Mandatory:   true,
   211  						Aliases:     []config.Alias{},
   212  						Default:     os.Getenv("PIPER_host"),
   213  					},
   214  					{
   215  						Name:        "client",
   216  						ResourceRef: []config.ResourceReference{},
   217  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   218  						Type:        "string",
   219  						Mandatory:   true,
   220  						Aliases:     []config.Alias{},
   221  						Default:     os.Getenv("PIPER_client"),
   222  					},
   223  					{
   224  						Name:        "commit",
   225  						ResourceRef: []config.ResourceReference{},
   226  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   227  						Type:        "string",
   228  						Mandatory:   false,
   229  						Aliases:     []config.Alias{},
   230  						Default:     os.Getenv("PIPER_commit"),
   231  					},
   232  					{
   233  						Name:        "remoteRepositoryURL",
   234  						ResourceRef: []config.ResourceReference{},
   235  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   236  						Type:        "string",
   237  						Mandatory:   true,
   238  						Aliases:     []config.Alias{},
   239  						Default:     os.Getenv("PIPER_remoteRepositoryURL"),
   240  					},
   241  					{
   242  						Name:        "role",
   243  						ResourceRef: []config.ResourceReference{},
   244  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   245  						Type:        "string",
   246  						Mandatory:   false,
   247  						Aliases:     []config.Alias{},
   248  						Default:     `SOURCE`,
   249  					},
   250  					{
   251  						Name:        "vSID",
   252  						ResourceRef: []config.ResourceReference{},
   253  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   254  						Type:        "string",
   255  						Mandatory:   false,
   256  						Aliases:     []config.Alias{},
   257  						Default:     os.Getenv("PIPER_vSID"),
   258  					},
   259  					{
   260  						Name:        "type",
   261  						ResourceRef: []config.ResourceReference{},
   262  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   263  						Type:        "string",
   264  						Mandatory:   false,
   265  						Aliases:     []config.Alias{},
   266  						Default:     `GIT`,
   267  					},
   268  					{
   269  						Name:        "branch",
   270  						ResourceRef: []config.ResourceReference{},
   271  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   272  						Type:        "string",
   273  						Mandatory:   false,
   274  						Aliases:     []config.Alias{},
   275  						Default:     os.Getenv("PIPER_branch"),
   276  					},
   277  					{
   278  						Name:        "scope",
   279  						ResourceRef: []config.ResourceReference{},
   280  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   281  						Type:        "string",
   282  						Mandatory:   false,
   283  						Aliases:     []config.Alias{},
   284  						Default:     os.Getenv("PIPER_scope"),
   285  					},
   286  					{
   287  						Name:        "rollback",
   288  						ResourceRef: []config.ResourceReference{},
   289  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   290  						Type:        "bool",
   291  						Mandatory:   false,
   292  						Aliases:     []config.Alias{},
   293  						Default:     false,
   294  					},
   295  					{
   296  						Name:        "configuration",
   297  						ResourceRef: []config.ResourceReference{},
   298  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   299  						Type:        "map[string]interface{}",
   300  						Mandatory:   false,
   301  						Aliases:     []config.Alias{{Name: "gctsRepositoryConfigurations"}},
   302  					},
   303  				},
   304  			},
   305  		},
   306  	}
   307  	return theMetaData
   308  }