You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	(feat) mtaBuild publish mtar artifacts to remote remote repo (#3104)
* mta build config * http request to upload mtar * adding basic auth * using put * test file name * hard coding the mta org and artifact is * new version * new version * mtar group * errors * better error message * log info * log info * correct mtar artifact name * adding teh correct name * test * name changes * clean up * changing mtarVersion to version * changing artifact name * forcing release build * forcing release build * force profile * force profile * force profile * force profile * force profile * force profile * force profile * force profile update * force profile update * profile update * debug * debug * debug * debug * rewrite xml update * rewrite xml update * unmarshal solution * unmarshal solution * unmarshal solution * unmarshal solution * unmarshal solution * unmarshal solution * unmarshal solution * unmarshal solution * unmarshal solution * outputin publish repo url * removing fetch coordinates condition * checking settings xml * fixing artifact id cpe * release artifact, package and group to cpe * including versioning type as a cpe * creating new settings xml file * creating parent folder * creating parent folder * creating parent folder * creating parent folder * creating parent folder * creating parent folder * creating parent folder * creating parent folder * creating parent folder * creating parent folder * changing to project settings * function name change * using glbl settings xml * modiying the npm settings * modiying the npm settings * modiying the npm settings * modiying the npm settings * using file path join for m2 settings file * generator * unit tests * hardening error message * removing versioningType * removing versioningType * new vault profile paths * error message improvement * unit test fixes Co-authored-by: Your Name <you@example.com>
This commit is contained in:
		| @@ -2,8 +2,10 @@ package cmd | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"encoding/base64" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| @@ -19,6 +21,7 @@ import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperutils" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/ghodss/yaml" | ||||
| 	"github.com/pkg/errors" | ||||
| ) | ||||
|  | ||||
| const templateMtaYml = `_schema-version: "3.1" | ||||
| @@ -148,6 +151,11 @@ func runMtaBuild(config mtaBuildOptions, | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	err = handleActiveProfileUpdate(config, utils) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	err = utils.SetNpmRegistries(config.DefaultNpmRegistry) | ||||
|  | ||||
| 	mtaYamlFile := "mta.yaml" | ||||
| @@ -233,9 +241,56 @@ func runMtaBuild(config mtaBuildOptions, | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if config.Publish { | ||||
| 		log.Entry().Infof("publish detected") | ||||
| 		if (len(config.MtaDeploymentRepositoryPassword) > 0) && (len(config.MtaDeploymentRepositoryUser) > 0) && | ||||
| 			(len(config.MtaDeploymentRepositoryURL) > 0) { | ||||
| 			if (len(config.MtarGroup) > 0) && (len(config.Version) > 0) { | ||||
| 				downloadClient := &piperhttp.Client{} | ||||
|  | ||||
| 				credentialsEncoded := "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", config.MtaDeploymentRepositoryUser, config.MtaDeploymentRepositoryPassword))) | ||||
| 				headers := http.Header{} | ||||
| 				headers.Add("Authorization", credentialsEncoded) | ||||
|  | ||||
| 				config.MtarGroup = strings.ReplaceAll(config.MtarGroup, ".", "/") | ||||
|  | ||||
| 				mtarArtifactName := mtarName | ||||
|  | ||||
| 				mtarArtifactName = strings.ReplaceAll(mtarArtifactName, ".mtar", "") | ||||
| 				mtarArtifactName = strings.ReplaceAll(mtarArtifactName, ".", "/") | ||||
|  | ||||
| 				config.MtaDeploymentRepositoryURL += config.MtarGroup + "/" + mtarArtifactName + "/" + config.Version + "/" + fmt.Sprintf("%v-%v.%v", mtarArtifactName, config.Version, "mtar") | ||||
|  | ||||
| 				commonPipelineEnvironment.custom.mtarPublishedURL = config.MtaDeploymentRepositoryURL | ||||
|  | ||||
| 				log.Entry().Infof("pushing mtar artifact to repository : %s", config.MtaDeploymentRepositoryURL) | ||||
|  | ||||
| 				_, httpErr := downloadClient.UploadRequest(http.MethodPut, config.MtaDeploymentRepositoryURL, mtarName, mtarName, headers, nil) | ||||
| 				if httpErr != nil { | ||||
| 					return errors.Wrap(err, "failed to upload mtar to repository") | ||||
| 				} | ||||
| 			} else { | ||||
| 				return errors.New("mtarGroup, version not found and must be present") | ||||
|  | ||||
| 			} | ||||
|  | ||||
| 		} else { | ||||
| 			return errors.New("mtaDeploymentRepositoryUser, mtaDeploymentRepositoryPassword and mtaDeploymentRepositoryURL not found , must be present") | ||||
| 		} | ||||
| 	} else { | ||||
| 		log.Entry().Infof("no publish detected, skipping upload of mtar artifact") | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func handleActiveProfileUpdate(config mtaBuildOptions, utils mtaBuildUtils) error { | ||||
| 	if len(config.Profiles) > 0 { | ||||
| 		return maven.UpdateActiveProfileInSettingsXML(config.Profiles, utils) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func installMavenArtifacts(utils mtaBuildUtils, config mtaBuildOptions) error { | ||||
| 	pomXMLExists, err := utils.FileExists("pom.xml") | ||||
| 	if err != nil { | ||||
|   | ||||
| @@ -17,21 +17,31 @@ import ( | ||||
| ) | ||||
|  | ||||
| type mtaBuildOptions struct { | ||||
| 	MtarName            string `json:"mtarName,omitempty"` | ||||
| 	Extensions          string `json:"extensions,omitempty"` | ||||
| 	Platform            string `json:"platform,omitempty"` | ||||
| 	ApplicationName     string `json:"applicationName,omitempty"` | ||||
| 	Source              string `json:"source,omitempty"` | ||||
| 	Target              string `json:"target,omitempty"` | ||||
| 	DefaultNpmRegistry  string `json:"defaultNpmRegistry,omitempty"` | ||||
| 	ProjectSettingsFile string `json:"projectSettingsFile,omitempty"` | ||||
| 	GlobalSettingsFile  string `json:"globalSettingsFile,omitempty"` | ||||
| 	M2Path              string `json:"m2Path,omitempty"` | ||||
| 	InstallArtifacts    bool   `json:"installArtifacts,omitempty"` | ||||
| 	MtarName                        string   `json:"mtarName,omitempty"` | ||||
| 	MtarGroup                       string   `json:"mtarGroup,omitempty"` | ||||
| 	Version                         string   `json:"version,omitempty"` | ||||
| 	Extensions                      string   `json:"extensions,omitempty"` | ||||
| 	Platform                        string   `json:"platform,omitempty"` | ||||
| 	ApplicationName                 string   `json:"applicationName,omitempty"` | ||||
| 	Source                          string   `json:"source,omitempty"` | ||||
| 	Target                          string   `json:"target,omitempty"` | ||||
| 	DefaultNpmRegistry              string   `json:"defaultNpmRegistry,omitempty"` | ||||
| 	ProjectSettingsFile             string   `json:"projectSettingsFile,omitempty"` | ||||
| 	GlobalSettingsFile              string   `json:"globalSettingsFile,omitempty"` | ||||
| 	M2Path                          string   `json:"m2Path,omitempty"` | ||||
| 	InstallArtifacts                bool     `json:"installArtifacts,omitempty"` | ||||
| 	MtaDeploymentRepositoryPassword string   `json:"mtaDeploymentRepositoryPassword,omitempty"` | ||||
| 	MtaDeploymentRepositoryUser     string   `json:"mtaDeploymentRepositoryUser,omitempty"` | ||||
| 	MtaDeploymentRepositoryURL      string   `json:"mtaDeploymentRepositoryUrl,omitempty"` | ||||
| 	Publish                         bool     `json:"publish,omitempty"` | ||||
| 	Profiles                        []string `json:"profiles,omitempty"` | ||||
| } | ||||
|  | ||||
| type mtaBuildCommonPipelineEnvironment struct { | ||||
| 	mtarFilePath string | ||||
| 	custom       struct { | ||||
| 		mtarPublishedURL string | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (p *mtaBuildCommonPipelineEnvironment) persist(path, resourceName string) { | ||||
| @@ -41,6 +51,7 @@ func (p *mtaBuildCommonPipelineEnvironment) persist(path, resourceName string) { | ||||
| 		value    interface{} | ||||
| 	}{ | ||||
| 		{category: "", name: "mtarFilePath", value: p.mtarFilePath}, | ||||
| 		{category: "custom", name: "mtarPublishedUrl", value: p.custom.mtarPublishedURL}, | ||||
| 	} | ||||
|  | ||||
| 	errCount := 0 | ||||
| @@ -86,6 +97,7 @@ func MtaBuildCommand() *cobra.Command { | ||||
| 				log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 				return err | ||||
| 			} | ||||
| 			log.RegisterSecret(stepConfig.MtaDeploymentRepositoryPassword) | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| @@ -134,6 +146,8 @@ func MtaBuildCommand() *cobra.Command { | ||||
|  | ||||
| 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.MtarGroup, "mtarGroup", os.Getenv("PIPER_mtarGroup"), "The group to which the mtar artifact will be uploaded. Required when publish is True.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Version, "version", os.Getenv("PIPER_version"), "Version of the mtar artifact") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Extensions, "extensions", os.Getenv("PIPER_extensions"), "The path to the extension descriptor file.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Platform, "platform", `CF`, "The target platform to which the mtar can be deployed.") | ||||
| 	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.") | ||||
| @@ -144,6 +158,11 @@ func addMtaBuildFlags(cmd *cobra.Command, stepConfig *mtaBuildOptions) { | ||||
| 	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") | ||||
| 	cmd.Flags().StringVar(&stepConfig.M2Path, "m2Path", os.Getenv("PIPER_m2Path"), "Path to the location of the local repository that should be used.") | ||||
| 	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)") | ||||
| 	cmd.Flags().StringVar(&stepConfig.MtaDeploymentRepositoryPassword, "mtaDeploymentRepositoryPassword", os.Getenv("PIPER_mtaDeploymentRepositoryPassword"), "Password for the alternative deployment repository to which mtar artifacts will be publised") | ||||
| 	cmd.Flags().StringVar(&stepConfig.MtaDeploymentRepositoryUser, "mtaDeploymentRepositoryUser", os.Getenv("PIPER_mtaDeploymentRepositoryUser"), "User for the alternative deployment repository to which which mtar artifacts will be publised") | ||||
| 	cmd.Flags().StringVar(&stepConfig.MtaDeploymentRepositoryURL, "mtaDeploymentRepositoryUrl", os.Getenv("PIPER_mtaDeploymentRepositoryUrl"), "Url for the alternative deployment repository to which mtar artifacts will be publised") | ||||
| 	cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "pushed mtar artifact to altDeploymentRepositoryUrl/altDeploymentRepositoryID when set to true") | ||||
| 	cmd.Flags().StringSliceVar(&stepConfig.Profiles, "profiles", []string{}, "Defines list of maven build profiles to be used. profiles will overwrite existing values in the global settings xml at $M2_HOME/conf/settings.xml") | ||||
|  | ||||
| } | ||||
|  | ||||
| @@ -167,6 +186,29 @@ func mtaBuildMetadata() config.StepData { | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_mtarName"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "mtarGroup", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     os.Getenv("PIPER_mtarGroup"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "version", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "artifactVersion", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Aliases:   []config.Alias{{Name: "artifactVersion"}}, | ||||
| 						Default:   os.Getenv("PIPER_version"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "extensions", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| @@ -257,6 +299,77 @@ func mtaBuildMetadata() config.StepData { | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     false, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "mtaDeploymentRepositoryPassword", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryPassword", | ||||
| 							}, | ||||
|  | ||||
| 							{ | ||||
| 								Name: "mtaDeploymentRepositoryPasswordId", | ||||
| 								Type: "secret", | ||||
| 							}, | ||||
|  | ||||
| 							{ | ||||
| 								Name:    "mtaDeploymentRepositoryPasswordFileVaultSecretName", | ||||
| 								Type:    "vaultSecretFile", | ||||
| 								Default: "mta-deployment-repository-passowrd", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 						Default:   os.Getenv("PIPER_mtaDeploymentRepositoryPassword"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "mtaDeploymentRepositoryUser", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryUsername", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 						Default:   os.Getenv("PIPER_mtaDeploymentRepositoryUser"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "mtaDeploymentRepositoryUrl", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryUrl", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 						Default:   os.Getenv("PIPER_mtaDeploymentRepositoryUrl"), | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "publish", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"}, | ||||
| 						Type:        "bool", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "mta/publish"}}, | ||||
| 						Default:     false, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "profiles", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "GENERAL", "STAGES", "STEPS"}, | ||||
| 						Type:        "[]string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     []string{}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Containers: []config.Container{ | ||||
| @@ -269,6 +382,7 @@ func mtaBuildMetadata() config.StepData { | ||||
| 						Type: "piperEnvironment", | ||||
| 						Parameters: []map[string]interface{}{ | ||||
| 							{"Name": "mtarFilePath"}, | ||||
| 							{"Name": "custom/mtarPublishedUrl"}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
|   | ||||
| @@ -249,4 +249,32 @@ func TestMarBuild(t *testing.T) { | ||||
| 			assert.Equal(t, "", utilsMock.globalSettingsFile) | ||||
| 		}) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("publish related tests", func(t *testing.T) { | ||||
|  | ||||
| 		t.Run("error when no repository url", func(t *testing.T) { | ||||
|  | ||||
| 			utilsMock := newMtaBuildTestUtilsBundle() | ||||
| 			utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\"")) | ||||
|  | ||||
| 			options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./", Publish: true} | ||||
|  | ||||
| 			err := runMtaBuild(options, &cpe, utilsMock) | ||||
|  | ||||
| 			assert.Equal(t, "mtaDeploymentRepositoryUser, mtaDeploymentRepositoryPassword and mtaDeploymentRepositoryURL not found , must be present", err.Error()) | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("error when no mtar group", func(t *testing.T) { | ||||
|  | ||||
| 			utilsMock := newMtaBuildTestUtilsBundle() | ||||
| 			utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\"")) | ||||
|  | ||||
| 			options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./", Publish: true, | ||||
| 				MtaDeploymentRepositoryURL: "dummy", MtaDeploymentRepositoryPassword: "dummy", MtaDeploymentRepositoryUser: "dummy"} | ||||
|  | ||||
| 			err := runMtaBuild(options, &cpe, utilsMock) | ||||
|  | ||||
| 			assert.Equal(t, "mtarGroup, version not found and must be present", err.Error()) | ||||
| 		}) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -85,6 +85,54 @@ func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func UpdateActiveProfileInSettingsXML(newActiveProfiles []string, utils SettingsDownloadUtils) error { | ||||
| 	settingsFile, err := getGlobalSettingsFileDest() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	settingsXMLContent, err := utils.FileRead(settingsFile) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error reading global settings xml file at %v , continuing without active profile update", settingsFile) | ||||
| 	} | ||||
|  | ||||
| 	var projectSettings Settings | ||||
| 	err = xml.Unmarshal([]byte(settingsXMLContent), &projectSettings) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("failed to unmarshal settings xml file '%v': %w", settingsFile, err) | ||||
| 	} | ||||
|  | ||||
| 	if len(projectSettings.ActiveProfiles.ActiveProfile) == 0 { | ||||
| 		log.Entry().Warnf("no active profile found to replace in settings xml %v , continuing without file edit", settingsFile) | ||||
| 	} else { | ||||
| 		projectSettings.Xsi = "http://www.w3.org/2001/XMLSchema-instance" | ||||
| 		projectSettings.SchemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd" | ||||
|  | ||||
| 		projectSettings.ActiveProfiles.ActiveProfile = []string{} | ||||
| 		projectSettings.ActiveProfiles.ActiveProfile = append(projectSettings.ActiveProfiles.ActiveProfile, newActiveProfiles...) | ||||
|  | ||||
| 		settingsXml, err := xml.MarshalIndent(projectSettings, "", "    ") | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("failed to marshal maven project settings xml: %w", err) | ||||
| 		} | ||||
|  | ||||
| 		settingsXmlString := string(settingsXml) | ||||
| 		Replacer := strings.NewReplacer("
", "", "	", "") | ||||
| 		settingsXmlString = Replacer.Replace(settingsXmlString) | ||||
| 		xmlstring := []byte(xml.Header + settingsXmlString) | ||||
|  | ||||
| 		err = utils.FileWrite(settingsFile, xmlstring, 0777) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("failed to write maven Settings during <activeProfile> update xml: %w", err) | ||||
| 		} | ||||
| 		log.Entry().Infof("Successfully updated <acitveProfile> details in maven settings file : '%s'", settingsFile) | ||||
|  | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func CreateNewProjectSettingsXML(altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils SettingsDownloadUtils) (string, error) { | ||||
| 	settingsXML := Settings{ | ||||
| 		XMLName:        xml.Name{Local: "settings"}, | ||||
| @@ -255,7 +303,7 @@ func getGlobalSettingsFileDest() (string, error) { | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return m2Home + "/conf/settings.xml", nil | ||||
| 	return filepath.Join(m2Home, "conf", "settings.xml"), nil | ||||
| } | ||||
|  | ||||
| func getProjectSettingsFileDest() (string, error) { | ||||
| @@ -263,7 +311,7 @@ func getProjectSettingsFileDest() (string, error) { | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return home + "/.m2/settings.xml", nil | ||||
| 	return filepath.Join(home, ".m2", "settings.xml"), nil | ||||
| } | ||||
|  | ||||
| func getEnvironmentVariable(name string) (string, error) { | ||||
|   | ||||
| @@ -19,10 +19,17 @@ type ServersType struct { | ||||
| 	ServerType []Server `xml:"server,omitempty"` | ||||
| } | ||||
|  | ||||
| type ActiveProfilesType struct { | ||||
| 	XMLName       xml.Name `xml:"activeProfiles"` | ||||
| 	Text          string   `xml:",chardata"` | ||||
| 	ActiveProfile []string `xml:"activeProfile"` | ||||
| } | ||||
|  | ||||
| type Settings struct { | ||||
| 	XMLName xml.Name `xml:"http://maven.apache.org/SETTINGS/1.0.0 settings"` | ||||
| 	Text    string   `xml:",chardata"` | ||||
| 	//Xmlns           xml.Attr `xml:"xmlns,attr"` | ||||
| 	// Xmlns           xml.Attr `xml:"xmlns,attr"` | ||||
| 	//Xmlns           string `xml:"xmlns,attr"` | ||||
| 	Xsi             string `xml:"xmlns:xsi,attr"` | ||||
| 	SchemaLocation  string `xml:"xsi:schemaLocation,attr"` | ||||
| 	LocalRepository string `xml:"localRepository,omitempty"` | ||||
| @@ -60,30 +67,30 @@ type Settings struct { | ||||
| 	Profiles struct { | ||||
| 		Text    string `xml:",chardata"` | ||||
| 		Profile []struct { | ||||
| 			Text       string `xml:",chardata"` | ||||
| 			ID         string `xml:"id,omitempty"` | ||||
| 			Activation struct { | ||||
| 				Text            string `xml:",chardata"` | ||||
| 				ActiveByDefault string `xml:"activeByDefault,omitempty"` | ||||
| 				Jdk             string `xml:"jdk,omitempty"` | ||||
| 				Os              struct { | ||||
| 					Text    string `xml:",chardata"` | ||||
| 					Name    string `xml:"name,omitempty"` | ||||
| 					Family  string `xml:"family,omitempty"` | ||||
| 					Arch    string `xml:"arch,omitempty"` | ||||
| 					Version string `xml:"version,omitempty"` | ||||
| 				} `xml:"os,omitempty"` | ||||
| 				Property struct { | ||||
| 					Text  string `xml:",chardata"` | ||||
| 					Name  string `xml:"name,omitempty"` | ||||
| 					Value string `xml:"value,omitempty"` | ||||
| 				} `xml:"property,omitempty"` | ||||
| 				File struct { | ||||
| 					Text    string `xml:",chardata"` | ||||
| 					Exists  string `xml:"exists,omitempty"` | ||||
| 					Missing string `xml:"missing,omitempty"` | ||||
| 				} `xml:"file,omitempty"` | ||||
| 			} `xml:"activation,omitempty"` | ||||
| 			Text string `xml:",chardata"` | ||||
| 			ID   string `xml:"id,omitempty"` | ||||
| 			// Activation struct { | ||||
| 			// 	Text            string `xml:",chardata"` | ||||
| 			// 	ActiveByDefault string `xml:"activeByDefault,omitempty"` | ||||
| 			// 	Jdk             string `xml:"jdk,omitempty"` | ||||
| 			// 	Os              struct { | ||||
| 			// 		Text    string `xml:",chardata"` | ||||
| 			// 		Name    string `xml:"name,omitempty"` | ||||
| 			// 		Family  string `xml:"family,omitempty"` | ||||
| 			// 		Arch    string `xml:"arch,omitempty"` | ||||
| 			// 		Version string `xml:"version,omitempty"` | ||||
| 			// 	} `xml:"os,omitempty"` | ||||
| 			// 	Property struct { | ||||
| 			// 		Text  string `xml:",chardata"` | ||||
| 			// 		Name  string `xml:"name,omitempty"` | ||||
| 			// 		Value string `xml:"value,omitempty"` | ||||
| 			// 	} `xml:"property,omitempty"` | ||||
| 			// 	File struct { | ||||
| 			// 		Text    string `xml:",chardata"` | ||||
| 			// 		Exists  string `xml:"exists,omitempty"` | ||||
| 			// 		Missing string `xml:"missing,omitempty"` | ||||
| 			// 	} `xml:"file,omitempty"` | ||||
| 			// } `xml:"activation,omitempty"` | ||||
| 			Repositories struct { | ||||
| 				Text       string `xml:",chardata"` | ||||
| 				Repository []struct { | ||||
| @@ -125,8 +132,5 @@ type Settings struct { | ||||
| 			} `xml:"pluginRepositories,omitempty"` | ||||
| 		} `xml:"profile,omitempty"` | ||||
| 	} `xml:"profiles,omitempty"` | ||||
| 	ActiveProfiles struct { | ||||
| 		Text          string   `xml:",chardata"` | ||||
| 		ActiveProfile []string `xml:"activeProfile,omitempty"` | ||||
| 	} `xml:"activeProfiles,omitempty"` | ||||
| 	ActiveProfiles ActiveProfilesType `xml:"activeProfiles,omitempty"` | ||||
| } | ||||
|   | ||||
| @@ -154,6 +154,39 @@ func TestSettings(t *testing.T) { | ||||
|  | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("update active profile tag in existing settings file", func(t *testing.T) { | ||||
|  | ||||
| 		utilsMock := newSettingsDownloadTestUtilsBundle() | ||||
| 		var projectSettings Settings | ||||
| 		projectSettings.Xsi = "http://www.w3.org/2001/XMLSchema-instance" | ||||
| 		projectSettings.SchemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd" | ||||
| 		projectSettings.ActiveProfiles.ActiveProfile = []string{"dummyProfile"} | ||||
| 		settingsXml, err := xml.MarshalIndent(projectSettings, "", "    ") | ||||
| 		settingsXmlString := string(settingsXml) | ||||
| 		Replacer := strings.NewReplacer("
", "", "	", "") | ||||
| 		settingsXmlString = Replacer.Replace(settingsXmlString) | ||||
|  | ||||
| 		xmlstring := []byte(xml.Header + settingsXmlString) | ||||
|  | ||||
| 		destination, _ := getGlobalSettingsFileDest() | ||||
|  | ||||
| 		utilsMock.FileWrite("/usr/share/maven/conf/settings.xml", xmlstring, 0777) | ||||
|  | ||||
| 		err = UpdateActiveProfileInSettingsXML([]string{"newProfile"}, utilsMock) | ||||
|  | ||||
| 		if assert.NoError(t, err) { | ||||
| 			projectSettingsContent, _ := utilsMock.FileRead(destination) | ||||
| 			var projectSettings Settings | ||||
|  | ||||
| 			err = xml.Unmarshal(projectSettingsContent, &projectSettings) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, projectSettings.ActiveProfiles.ActiveProfile[0], "newProfile") | ||||
| 			} | ||||
|  | ||||
| 		} | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func newSettingsDownloadTestUtilsBundle() *settingsDownloadTestUtils { | ||||
|   | ||||
| @@ -15,6 +15,27 @@ spec: | ||||
|           - STEPS | ||||
|         mandatory: false | ||||
|         default: | ||||
|       - name: mtarGroup | ||||
|         type: string | ||||
|         description: "The group to which the mtar artifact will be uploaded. Required when publish is True." | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         mandatory: false | ||||
|         default: | ||||
|       - name: version | ||||
|         aliases: | ||||
|           - name: artifactVersion | ||||
|         type: string | ||||
|         description: Version of the mtar artifact | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         resourceRef: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: artifactVersion | ||||
|       - name: extensions | ||||
|         type: string | ||||
|         description: "The path to the extension descriptor file." | ||||
| @@ -119,11 +140,69 @@ spec: | ||||
|           - STEPS | ||||
|           - STAGES | ||||
|           - PARAMETERS | ||||
|       - name: mtaDeploymentRepositoryPassword | ||||
|         type: string | ||||
|         description: Password for the alternative deployment repository to which mtar artifacts will be publised | ||||
|         scope: | ||||
|           - GENERAL | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         secret: true | ||||
|         resourceRef: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryPassword | ||||
|           - name: mtaDeploymentRepositoryPasswordId | ||||
|             type: secret | ||||
|           - type: vaultSecretFile | ||||
|             name: mtaDeploymentRepositoryPasswordFileVaultSecretName | ||||
|             default: mta-deployment-repository-passowrd | ||||
|       - name: mtaDeploymentRepositoryUser | ||||
|         type: string | ||||
|         description: User for the alternative deployment repository to which which mtar artifacts will be publised | ||||
|         scope: | ||||
|           - GENERAL | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         resourceRef: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryUsername | ||||
|       - name: mtaDeploymentRepositoryUrl | ||||
|         type: string | ||||
|         description: Url for the alternative deployment repository to which mtar artifacts will be publised | ||||
|         scope: | ||||
|           - GENERAL | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         resourceRef: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryUrl | ||||
|       - name: publish | ||||
|         type: bool | ||||
|         description: pushed mtar artifact to altDeploymentRepositoryUrl/altDeploymentRepositoryID when set to true | ||||
|         scope: | ||||
|           - STEPS | ||||
|           - STAGES | ||||
|           - PARAMETERS | ||||
|         default: false | ||||
|         aliases: | ||||
|           - name: mta/publish | ||||
|       - name: profiles | ||||
|         type: "[]string" | ||||
|         description: Defines list of maven build profiles to be used. profiles will overwrite existing values in the global settings xml at $M2_HOME/conf/settings.xml | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - GENERAL | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|   outputs: | ||||
|     resources: | ||||
|       - name: commonPipelineEnvironment | ||||
|         type: piperEnvironment | ||||
|         params: | ||||
|           - name: mtarFilePath | ||||
|           - name: custom/mtarPublishedUrl | ||||
|   containers: | ||||
|     - image: devxci/mbtci:1.1.1 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user