8
8
"path/filepath"
9
9
"strings"
10
10
11
+ b64 "encoding/base64"
12
+
11
13
"github.com/SAP/jenkins-library/pkg/command"
12
14
"github.com/SAP/jenkins-library/pkg/log"
13
15
"github.com/SAP/jenkins-library/pkg/maven"
@@ -28,6 +30,7 @@ type nexusUploadUtils interface {
28
30
dirExists (path string ) (bool , error )
29
31
usesMta () bool
30
32
usesMaven () bool
33
+ usesNpm () bool
31
34
getEnvParameter (path , name string ) string
32
35
getExecRunner () execRunner
33
36
evaluate (pomFile , expression string ) (string , error )
@@ -94,6 +97,10 @@ func (u *utilsBundle) usesMaven() bool {
94
97
return u .projectStructure .UsesMaven ()
95
98
}
96
99
100
+ func (u * utilsBundle ) usesNpm () bool {
101
+ return u .projectStructure .UsesNpm ()
102
+ }
103
+
97
104
func (u * utilsBundle ) getEnvParameter (path , name string ) string {
98
105
return piperenv .GetParameter (path , name )
99
106
}
@@ -126,19 +133,50 @@ func nexusUpload(options nexusUploadOptions, _ *telemetry.CustomData) {
126
133
}
127
134
128
135
func runNexusUpload (utils nexusUploadUtils , uploader nexus.Uploader , options * nexusUploadOptions ) error {
129
- err := uploader .SetRepoURL (options .Url , options .Version , options .Repository )
136
+ performMavenUpload := len (options .MavenRepository ) > 0
137
+ performNpmUpload := len (options .NpmRepository ) > 0
138
+ err := uploader .SetRepoURL (options .Url , options .Version , options .MavenRepository , options .NpmRepository )
139
+ if err != nil {
140
+ return err
141
+ }
142
+
143
+ if utils .usesNpm () && performNpmUpload {
144
+ log .Entry ().Info ("NPM project structure detected" )
145
+ err = uploadNpmArtifacts (utils , uploader , options )
146
+ } else {
147
+ log .Entry ().Info ("Skipping npm upload because either no package json was found or NpmRepository option is not provided." )
148
+ }
130
149
if err != nil {
131
150
return err
132
151
}
133
- if utils .usesMta () {
134
- log .Entry ().Info ("MTA project structure detected" )
135
- return uploadMTA (utils , uploader , options )
136
- } else if utils .usesMaven () {
137
- log .Entry ().Info ("Maven project structure detected" )
138
- return uploadMaven (utils , uploader , options )
152
+
153
+ if performMavenUpload {
154
+ if utils .usesMta () {
155
+ log .Entry ().Info ("MTA project structure detected" )
156
+ return uploadMTA (utils , uploader , options )
157
+ } else if utils .usesMaven () {
158
+ log .Entry ().Info ("Maven project structure detected" )
159
+ return uploadMaven (utils , uploader , options )
160
+ }
139
161
} else {
140
- return fmt . Errorf ( "unsupported project structure " )
162
+ log . Entry (). Info ( "Skipping maven and mta upload because mavenRepository option is not provided. " )
141
163
}
164
+
165
+ return nil
166
+ }
167
+
168
+ func uploadNpmArtifacts (utils nexusUploadUtils , uploader nexus.Uploader , options * nexusUploadOptions ) error {
169
+ execRunner := utils .getExecRunner ()
170
+ environment := []
string {
"npm_config_registry=http://" + uploader .
GetNpmRepoURL (),
"[email protected] " }
171
+ if options .User != "" && options .Password != "" {
172
+ auth := b64 .StdEncoding .EncodeToString ([]byte (options .User + ":" + options .Password ))
173
+ environment = append (environment , "npm_config__auth=" + auth )
174
+ } else {
175
+ log .Entry ().Info ("No credentials provided for npm upload, trying to upload anonymously." )
176
+ }
177
+ execRunner .SetEnv (environment )
178
+ err := execRunner .RunExecutable ("npm" , "publish" )
179
+ return err
142
180
}
143
181
144
182
func uploadMTA (utils nexusUploadUtils , uploader nexus.Uploader , options * nexusUploadOptions ) error {
@@ -270,7 +308,7 @@ func uploadArtifacts(utils nexusUploadUtils, uploader nexus.Uploader, options *n
270
308
}
271
309
272
310
var defines []string
273
- defines = append (defines , "-Durl=http://" + uploader .GetRepoURL ())
311
+ defines = append (defines , "-Durl=http://" + uploader .GetMavenRepoURL ())
274
312
defines = append (defines , "-DgroupId=" + uploader .GetGroupID ())
275
313
defines = append (defines , "-Dversion=" + uploader .GetArtifactsVersion ())
276
314
defines = append (defines , "-DartifactId=" + uploader .GetArtifactsID ())
0 commit comments