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 
			
		
		
		
	nexusUpload parameters reading from commonPipelineEnvironment (#2658)
* nexusUpload parameters reading from commonPipelineEnvironment * Cleaning code formatting * Fixing go fmt issues * Fixing go generate files * Fixing codeclimate issue * Fixing with PR feedback * Fixing unit test failure due to changed error message * Fixing codeclimate on punctuatin in error message * Fixing typo error in error message Co-authored-by: LECAROZ <louis.lecaroz@sap.com>
This commit is contained in:
		| @@ -102,7 +102,14 @@ func runNexusUpload(utils nexusUploadUtils, uploader nexus.Uploader, options *ne | ||||
| 	performNpmUpload := len(options.NpmRepository) > 0 | ||||
|  | ||||
| 	if !performMavenUpload && !performNpmUpload { | ||||
| 		return fmt.Errorf("none of the parameters 'mavenRepository' and 'npmRepository' are configured") | ||||
| 		if options.Format == "" { | ||||
| 			return fmt.Errorf("none of the parameters 'mavenRepository' and 'npmRepository' are configured, or 'format' should be set if the 'url' already contains the repository ID") | ||||
| 		} | ||||
| 		if options.Format == "maven" { | ||||
| 			performMavenUpload = true | ||||
| 		} else if options.Format == "npm" { | ||||
| 			performNpmUpload = true | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	err := uploader.SetRepoURL(options.Url, options.Version, options.MavenRepository, options.NpmRepository) | ||||
| @@ -136,7 +143,7 @@ func runNexusUpload(utils nexusUploadUtils, uploader nexus.Uploader, options *ne | ||||
| } | ||||
|  | ||||
| func uploadNpmArtifacts(utils nexusUploadUtils, uploader nexus.Uploader, options *nexusUploadOptions) error { | ||||
| 	environment := []string{"npm_config_registry=http://" + uploader.GetNpmRepoURL(), "npm_config_email=project-piper@no-reply.com"} | ||||
| 	environment := []string{"npm_config_registry=" + uploader.GetNexusURLProtocol() + "://" + uploader.GetNpmRepoURL(), "npm_config_email=project-piper@no-reply.com"} | ||||
| 	if options.Username != "" && options.Password != "" { | ||||
| 		auth := b64.StdEncoding.EncodeToString([]byte(options.Username + ":" + options.Password)) | ||||
| 		environment = append(environment, "npm_config__auth="+auth) | ||||
| @@ -277,7 +284,7 @@ func uploadArtifacts(utils nexusUploadUtils, uploader nexus.Uploader, options *n | ||||
| 	} | ||||
|  | ||||
| 	var defines []string | ||||
| 	defines = append(defines, "-Durl=http://"+uploader.GetMavenRepoURL()) | ||||
| 	defines = append(defines, "-Durl="+uploader.GetNexusURLProtocol()+"://"+uploader.GetMavenRepoURL()) | ||||
| 	defines = append(defines, "-DgroupId="+uploader.GetGroupID()) | ||||
| 	defines = append(defines, "-Dversion="+uploader.GetArtifactsVersion()) | ||||
| 	defines = append(defines, "-DartifactId="+uploader.GetArtifactsID()) | ||||
|   | ||||
| @@ -15,6 +15,7 @@ import ( | ||||
|  | ||||
| type nexusUploadOptions struct { | ||||
| 	Version            string `json:"version,omitempty"` | ||||
| 	Format             string `json:"format,omitempty"` | ||||
| 	Url                string `json:"url,omitempty"` | ||||
| 	MavenRepository    string `json:"mavenRepository,omitempty"` | ||||
| 	NpmRepository      string `json:"npmRepository,omitempty"` | ||||
| @@ -47,6 +48,8 @@ To upload Maven projects, you need a pom.xml in the project root and set the mav | ||||
| To upload MTA projects, you need a mta.yaml in the project root and set the mavenRepository option. | ||||
| To upload npm projects, you need a package.json in the project root and set the npmRepository option. | ||||
|  | ||||
| If the 'format' option is set, the 'URL' can contain the full path including the repository ID. Providing the 'npmRepository' or the 'mavenRepository' parameter(s) is not necessary. | ||||
|  | ||||
| npm: | ||||
| Publishing npm projects makes use of npm's "publish" command. | ||||
| It requires a "package.json" file in the project's root directory which has "version" set and is not delared as "private". | ||||
| @@ -103,7 +106,8 @@ If an image for mavenExecute is configured, and npm packages are to be published | ||||
|  | ||||
| func addNexusUploadFlags(cmd *cobra.Command, stepConfig *nexusUploadOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.Version, "version", `nexus3`, "The Nexus Repository Manager version. Currently supported are 'nexus2' and 'nexus3'.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Url, "url", os.Getenv("PIPER_url"), "URL of the nexus. The scheme part of the URL will not be considered, because only http is supported.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Format, "format", os.Getenv("PIPER_format"), "The format/registry type. Currently supported are 'maven' and 'npm'.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.Url, "url", os.Getenv("PIPER_url"), "URL of the nexus. The scheme part of the URL will not be considered, because only http is supported. If the 'format' option is set, the 'URL' can contain the full path including the repository ID and providing the 'npmRepository' or the 'mavenRepository' parameter(s) is not necessary.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.MavenRepository, "mavenRepository", os.Getenv("PIPER_mavenRepository"), "Name of the nexus repository for Maven and MTA deployments. If this is not provided, Maven and MTA deployment is implicitly disabled.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.NpmRepository, "npmRepository", os.Getenv("PIPER_npmRepository"), "Name of the nexus repository for npm deployments. If this is not provided, npm deployment is implicitly disabled.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.GroupID, "groupId", os.Getenv("PIPER_groupId"), "Group ID of the artifacts. Only used in MTA projects, ignored for Maven.") | ||||
| @@ -140,12 +144,30 @@ func nexusUploadMetadata() config.StepData { | ||||
| 						Aliases:     []config.Alias{{Name: "nexus/version"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "url", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "nexus/url"}}, | ||||
| 						Name: "format", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryFormat", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "url", | ||||
| 						ResourceRef: []config.ResourceReference{ | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryUrl", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{{Name: "nexus/url"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "mavenRepository", | ||||
| @@ -203,6 +225,11 @@ func nexusUploadMetadata() config.StepData { | ||||
| 								Param: "username", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
|  | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryUsername", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS"}, | ||||
| 						Type:      "string", | ||||
| @@ -217,6 +244,11 @@ func nexusUploadMetadata() config.StepData { | ||||
| 								Param: "password", | ||||
| 								Type:  "secret", | ||||
| 							}, | ||||
|  | ||||
| 							{ | ||||
| 								Name:  "commonPipelineEnvironment", | ||||
| 								Param: "custom/repositoryPassword", | ||||
| 							}, | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS"}, | ||||
| 						Type:      "string", | ||||
|   | ||||
| @@ -386,7 +386,7 @@ func TestRunNexusUpload(t *testing.T) { | ||||
| 		} | ||||
|  | ||||
| 		err := runNexusUpload(utils, &uploader, &options) | ||||
| 		assert.EqualError(t, err, "none of the parameters 'mavenRepository' and 'npmRepository' are configured") | ||||
| 		assert.EqualError(t, err, "none of the parameters 'mavenRepository' and 'npmRepository' are configured, or 'format' should be set if the 'url' already contains the repository ID") | ||||
| 	}) | ||||
| 	t.Run("Test uploading simple npm project", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|   | ||||
| @@ -18,6 +18,7 @@ type ArtifactDescription struct { | ||||
| // Upload combines information about an artifact and its sub-artifacts which are supposed to be uploaded together. | ||||
| // Call SetRepoURL(), SetArtifactsVersion(), SetArtifactID(), and add at least one artifact via AddArtifact(). | ||||
| type Upload struct { | ||||
| 	protocol     string | ||||
| 	mavenRepoURL string | ||||
| 	npmRepoURL   string | ||||
| 	groupID      string | ||||
| @@ -29,6 +30,7 @@ type Upload struct { | ||||
| // Uploader provides an interface for configuring the target Nexus Repository and adding artifacts. | ||||
| type Uploader interface { | ||||
| 	SetRepoURL(nexusURL, nexusVersion, mavenRepository, npmRepository string) error | ||||
| 	GetNexusURLProtocol() string | ||||
| 	GetMavenRepoURL() string | ||||
| 	GetNpmRepoURL() string | ||||
| 	SetInfo(groupID, artifactsID, version string) error | ||||
| @@ -42,6 +44,11 @@ type Uploader interface { | ||||
|  | ||||
| // SetRepoURL constructs the base URL to the Nexus repository. mavenRepository or npmRepository may be empty. | ||||
| func (nexusUpload *Upload) SetRepoURL(nexusURL, nexusVersion, mavenRepository, npmRepository string) error { | ||||
| 	protocol, err := _GetNexusURLProtocol(nexusURL) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	nexusUpload.protocol = protocol | ||||
| 	mavenRepoURL, err := getBaseURL(nexusURL, nexusVersion, mavenRepository) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| @@ -67,29 +74,54 @@ func getBaseURL(nexusURL, nexusVersion, repository string) (string, error) { | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
| 	if repository == "" { | ||||
| 		return "", nil | ||||
| 	} | ||||
|  | ||||
| 	baseURL := nexusURL | ||||
| 	switch nexusVersion { | ||||
| 	case "nexus2": | ||||
| 		baseURL += "/content/repositories/" | ||||
| 	case "nexus3": | ||||
| 		baseURL += "/repository/" | ||||
| 	default: | ||||
| 		return "", fmt.Errorf("unsupported Nexus version '%s', must be 'nexus2' or 'nexus3'", nexusVersion) | ||||
|  | ||||
| 	if repository != "" { | ||||
| 		switch nexusVersion { | ||||
| 		case "nexus2": | ||||
| 			baseURL += "/content/repositories/" | ||||
| 		case "nexus3": | ||||
| 			baseURL += "/repository/" | ||||
| 		default: | ||||
| 			return "", fmt.Errorf("unsupported Nexus version '%s', must be 'nexus2' or 'nexus3'", nexusVersion) | ||||
| 		} | ||||
| 		baseURL += repository + "/" | ||||
| 		// Replace any double slashes, as nexus does not like them | ||||
| 		baseURL = strings.ReplaceAll(baseURL, "//", "/") | ||||
| 	} | ||||
| 	baseURL += repository + "/" | ||||
| 	// Replace any double slashes, as nexus does not like them | ||||
| 	baseURL = strings.ReplaceAll(baseURL, "//", "/") | ||||
|  | ||||
| 	return baseURL, nil | ||||
| } | ||||
|  | ||||
| // _GetNexusURLProtocol returns the protocol specified in the nexusUrl which was set thru setNexusUrl (internal method) | ||||
| func _GetNexusURLProtocol(nexusURL string) (string, error) { | ||||
| 	if nexusURL == "" { | ||||
| 		return "", errors.New("nexusURL must not be empty") | ||||
| 	} | ||||
| 	nexusURL = strings.ToLower(nexusURL) | ||||
| 	var protocols = []string{"http://", "https://"} | ||||
| 	for _, protocol := range protocols { | ||||
| 		if strings.HasPrefix(nexusURL, protocol) { | ||||
| 			return strings.ReplaceAll(protocol, "://", ""), nil | ||||
| 		} | ||||
| 	} | ||||
| 	return "http", nil | ||||
| } | ||||
|  | ||||
| // GetMavenRepoURL returns the base URL for the nexus-maven repository. | ||||
| func (nexusUpload *Upload) GetMavenRepoURL() string { | ||||
| 	return nexusUpload.mavenRepoURL | ||||
| } | ||||
|  | ||||
| // GetNexusURLProtocol returns the protocol specified in the nexusUrl which was set thru setNexusUrl | ||||
| func (nexusUpload *Upload) GetNexusURLProtocol() string { | ||||
| 	if nexusUpload.protocol == "" { | ||||
| 		return "http" | ||||
| 	} | ||||
| 	return nexusUpload.protocol | ||||
| } | ||||
|  | ||||
| // GetNpmRepoURL returns the base URL for the nexus-npm repository. | ||||
| func (nexusUpload *Upload) GetNpmRepoURL() string { | ||||
| 	return nexusUpload.npmRepoURL | ||||
|   | ||||
| @@ -15,6 +15,8 @@ metadata: | ||||
|     To upload MTA projects, you need a mta.yaml in the project root and set the mavenRepository option. | ||||
|     To upload npm projects, you need a package.json in the project root and set the npmRepository option. | ||||
|  | ||||
|     If the 'format' option is set, the 'URL' can contain the full path including the repository ID. Providing the 'npmRepository' or the 'mavenRepository' parameter(s) is not necessary. | ||||
|  | ||||
|     npm: | ||||
|     Publishing npm projects makes use of npm's "publish" command. | ||||
|     It requires a "package.json" file in the project's root directory which has "version" set and is not delared as "private". | ||||
| @@ -45,9 +47,22 @@ spec: | ||||
|           - nexus3 | ||||
|         aliases: | ||||
|           - name: nexus/version | ||||
|       - name: format | ||||
|         type: string | ||||
|         description: The format/registry type. Currently supported are 'maven' and 'npm'. | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         possibleValues: | ||||
|           - maven | ||||
|           - npm | ||||
|         resourceRef: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryFormat | ||||
|       - name: url | ||||
|         type: string | ||||
|         description: URL of the nexus. The scheme part of the URL will not be considered, because only http is supported. | ||||
|         description: URL of the nexus. The scheme part of the URL will not be considered, because only http is supported. If the 'format' option is set, the 'URL' can contain the full path including the repository ID and providing the 'npmRepository' or the 'mavenRepository' parameter(s) is not necessary. | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
| @@ -55,6 +70,9 @@ spec: | ||||
|         mandatory: true | ||||
|         aliases: | ||||
|           - name: nexus/url | ||||
|         resourceRef: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryUrl | ||||
|       - name: mavenRepository | ||||
|         type: string | ||||
|         description: Name of the nexus repository for Maven and MTA deployments. If this is not provided, Maven and MTA deployment is implicitly disabled. | ||||
| @@ -119,6 +137,8 @@ spec: | ||||
|           - name: nexusCredentialsId | ||||
|             type: secret | ||||
|             param: username | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryUsername | ||||
|       - name: password | ||||
|         type: string | ||||
|         description: Password for accessing the Nexus endpoint. | ||||
| @@ -129,6 +149,8 @@ spec: | ||||
|           - name: nexusCredentialsId | ||||
|             type: secret | ||||
|             param: password | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/repositoryPassword | ||||
|     resources: | ||||
|       - name: buildDescriptor | ||||
|         type: stash | ||||
|   | ||||
		Reference in New Issue
	
	Block a user