1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-10-30 23:57:50 +02:00

Run nexusUpload in stage Promote (#2349)

This commit is contained in:
Stephan Aßmus
2020-11-11 16:32:23 +01:00
committed by GitHub
parent 0356f13853
commit 115cfb5bae
7 changed files with 83 additions and 9 deletions

View File

@@ -100,6 +100,11 @@ func nexusUpload(options nexusUploadOptions, _ *telemetry.CustomData) {
func runNexusUpload(utils nexusUploadUtils, uploader nexus.Uploader, options *nexusUploadOptions) error {
performMavenUpload := len(options.MavenRepository) > 0
performNpmUpload := len(options.NpmRepository) > 0
if !performMavenUpload && !performNpmUpload {
return fmt.Errorf("none of the parameters 'mavenRepository' and 'npmRepository' are configured")
}
err := uploader.SetRepoURL(options.Url, options.Version, options.MavenRepository, options.NpmRepository)
if err != nil {
return err

View File

@@ -25,7 +25,7 @@ type mockUtilsBundle struct {
}
func (m *mockUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
return errors.New("Test should not download files.")
return errors.New("test should not download files")
}
func newMockUtilsBundle(usesMta, usesMaven, usesNpm bool) *mockUtilsBundle {
@@ -148,6 +148,7 @@ var testPackageJson = []byte(`{
func TestUploadMTAProjects(t *testing.T) {
t.Parallel()
t.Run("Uploading MTA project without groupId parameter fails", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yaml", testMtaYml)
utils.cpe[".pipeline/commonPipelineEnvironment/mtarFilePath"] = "test.mtar"
@@ -161,6 +162,7 @@ func TestUploadMTAProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Uploading MTA project without artifactId parameter works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yaml", testMtaYml)
utils.AddFile("test.mtar", []byte("contentsOfMtar"))
@@ -176,6 +178,7 @@ func TestUploadMTAProjects(t *testing.T) {
}
})
t.Run("Uploading MTA project fails due to missing yaml file", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.cpe[".pipeline/commonPipelineEnvironment/mtarFilePath"] = "test.mtar"
uploader := mockUploader{}
@@ -187,6 +190,7 @@ func TestUploadMTAProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Uploading MTA project fails due to garbage YAML content", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yaml", []byte("garbage"))
utils.cpe[".pipeline/commonPipelineEnvironment/mtarFilePath"] = "test.mtar"
@@ -200,6 +204,7 @@ func TestUploadMTAProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Uploading MTA project fails due invalid version in YAML content", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yaml", []byte(testMtaYmlNoVersion))
utils.cpe[".pipeline/commonPipelineEnvironment/mtarFilePath"] = "test.mtar"
@@ -213,6 +218,7 @@ func TestUploadMTAProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Test uploading mta.yaml project fails due to missing mtar file", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yaml", testMtaYml)
utils.cpe[".pipeline/commonPipelineEnvironment/mtarFilePath"] = "test.mtar"
@@ -234,6 +240,7 @@ func TestUploadMTAProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Test uploading mta.yaml project works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yaml", testMtaYml)
utils.AddFile("test.mtar", []byte("contentsOfMtar"))
@@ -257,6 +264,7 @@ func TestUploadMTAProjects(t *testing.T) {
}
})
t.Run("Test uploading mta.yml project works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(true, false, false)
utils.AddFile("mta.yml", testMtaYml)
utils.AddFile("test.mtar", []byte("contentsOfMtar"))
@@ -284,6 +292,7 @@ func TestUploadMTAProjects(t *testing.T) {
func TestUploadArtifacts(t *testing.T) {
t.Parallel()
t.Run("Uploading MTA project fails without info", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
uploader := mockUploader{}
options := createOptions()
@@ -292,6 +301,7 @@ func TestUploadArtifacts(t *testing.T) {
assert.EqualError(t, err, "no group ID was provided, or could be established from project files")
})
t.Run("Uploading MTA project fails without any artifacts", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
uploader := mockUploader{}
options := createOptions()
@@ -302,6 +312,7 @@ func TestUploadArtifacts(t *testing.T) {
assert.EqualError(t, err, "no artifacts to upload")
})
t.Run("Uploading MTA project fails for unknown reasons", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
// Configure mocked execRunner to fail
@@ -324,6 +335,7 @@ func TestUploadArtifacts(t *testing.T) {
assert.EqualError(t, err, "uploading artifacts for ID 'some.id' failed: failed to run executable, command: '[mvn -Durl=http:// -DgroupId=my.group.id -Dversion=3.0 -DartifactId=some.id -Dfile=mta.yaml -Dpackaging=yaml -DgeneratePom=false -Dfiles=artifact.mtar -Dclassifiers= -Dtypes=yaml -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode "+deployGoal+"]', error: failed")
})
t.Run("Uploading bundle generates correct maven parameters", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
uploader := mockUploader{}
options := createOptions()
@@ -362,8 +374,22 @@ func TestUploadArtifacts(t *testing.T) {
})
}
func TestUploadNpmProjects(t *testing.T) {
func TestRunNexusUpload(t *testing.T) {
t.Parallel()
t.Run("uploading without any repos fails step", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, false, true)
utils.AddFile("package.json", testPackageJson)
uploader := mockUploader{}
options := nexusUploadOptions{
Url: "localhost:8081",
}
err := runNexusUpload(utils, &uploader, &options)
assert.EqualError(t, err, "none of the parameters 'mavenRepository' and 'npmRepository' are configured")
})
t.Run("Test uploading simple npm project", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, false, true)
utils.AddFile("package.json", testPackageJson)
uploader := mockUploader{}
@@ -384,6 +410,7 @@ func TestUploadNpmProjects(t *testing.T) {
func TestUploadMavenProjects(t *testing.T) {
t.Parallel()
t.Run("Uploading Maven project fails due to missing pom.xml", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
uploader := mockUploader{}
options := createOptions()
@@ -393,6 +420,7 @@ func TestUploadMavenProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Test uploading Maven project with POM packaging works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "com.mycompany.app")
@@ -415,6 +443,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
})
t.Run("Test uploading Maven project with JAR packaging fails without main target", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "com.mycompany.app")
@@ -431,6 +460,7 @@ func TestUploadMavenProjects(t *testing.T) {
assert.Equal(t, 0, len(uploader.uploadedArtifacts))
})
t.Run("Test uploading Maven project with JAR packaging works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "com.mycompany.app")
@@ -458,6 +488,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
})
t.Run("Test uploading Maven project with fall-back to JAR packaging works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "com.mycompany.app")
@@ -484,6 +515,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
})
t.Run("Test uploading Maven project with fall-back to group id from parameters works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.artifactId", "my-app")
@@ -509,6 +541,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
})
t.Run("Test uploading Maven project with fall-back for finalBuildName works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "awesome.group")
@@ -536,6 +569,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
})
t.Run("Test uploading Maven project with application module and finalName works", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "com.mycompany.app")
@@ -625,6 +659,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
})
t.Run("Write credentials settings", func(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
utils.setProperty("pom.xml", "project.version", "1.0")
utils.setProperty("pom.xml", "project.groupId", "com.mycompany.app")
@@ -667,6 +702,7 @@ func TestUploadMavenProjects(t *testing.T) {
}
func TestSetupNexusCredentialsSettingsFile(t *testing.T) {
t.Parallel()
utils := newMockUtilsBundle(false, true, false)
options := nexusUploadOptions{Username: "admin", Password: "admin123"}
mavenOptions := maven.ExecuteOptions{}

View File

@@ -111,6 +111,9 @@ stages:
artifactDeployment:
configKeys:
- 'nexus'
nexusUpload:
configKeys:
- 'url'
postPipelineHook:
stepConditions:
postPipelineHook:

View File

@@ -63,6 +63,9 @@ stages:
containerPushToRegistry:
configKeys:
- 'dockerRegistryUrl'
nexusUpload:
configKeys:
- 'url'
Release:
stepConditions:
multicloudDeploy:

View File

@@ -39,7 +39,6 @@ spec:
- PARAMETERS
- STAGES
- STEPS
mandatory: false
default: nexus3
possibleValues:
- nexus2
@@ -130,6 +129,11 @@ spec:
- name: nexusCredentialsId
type: secret
param: password
resources:
- name: buildDescriptor
type: stash
- name: buildResult
type: stash
containers:
# To allow both maven and mta we require an image that contains both tools. If the user configures an image for mavenExecute that also needs to contain both.
- name: mvn-npm

View File

@@ -6,13 +6,10 @@ import org.junit.Test
import org.junit.rules.RuleChain
import util.*
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.anyOf
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.hasItems
import static org.hamcrest.Matchers.hasItems
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.not
import static org.junit.Assert.assertThat
class PiperPipelineStagePromoteTest extends BasePiperTest {
@@ -42,6 +39,11 @@ class PiperPipelineStagePromoteTest extends BasePiperTest {
stepsCalled.add('containerPushToRegistry')
stepParameters.containerPushToRegistry = m
})
helper.registerAllowedMethod('nexusUpload', [Map.class], {m ->
stepsCalled.add('nexusUpload')
stepParameters.nexusUpload = m
})
}
@Test
@@ -51,7 +53,7 @@ class PiperPipelineStagePromoteTest extends BasePiperTest {
script: nullScript,
juStabUtils: utils,
)
assertThat(stepsCalled, not(hasItem('containerPushToRegistry')))
assertThat(stepsCalled, not(anyOf(hasItem('containerPushToRegistry'), hasItem('nexusUpload'))))
}
@@ -66,4 +68,16 @@ class PiperPipelineStagePromoteTest extends BasePiperTest {
assertThat(stepsCalled, hasItem('containerPushToRegistry'))
}
@Test
void testStagePromoteNexusUpload() {
jsr.step.piperPipelineStagePromote(
script: nullScript,
juStabUtils: utils,
nexusUpload: true
)
assertThat(stepsCalled, hasItem('nexusUpload'))
}
}

View File

@@ -1,5 +1,6 @@
import com.sap.piper.ConfigurationHelper
import com.sap.piper.GenerateStageDocumentation
import com.sap.piper.ReportAggregator
import com.sap.piper.StageNameProvider
import com.sap.piper.Utils
import groovy.transform.Field
@@ -13,6 +14,8 @@ import static com.sap.piper.Prerequisites.checkScript
@Field STAGE_STEP_KEYS = [
/** For Docker builds: pushes the Docker image to a container registry. */
'containerPushToRegistry',
/** For Maven/MTA builds: uploads artifacts to a Nexus repository manager. */
'nexusUpload',
]
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus(STAGE_STEP_KEYS)
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
@@ -34,6 +37,7 @@ void call(Map parameters = [:]) {
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.addIfEmpty('containerPushToRegistry', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.containerPushToRegistry)
.addIfEmpty('nexusUpload', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.nexusUpload)
.use()
piperStageWrapper (script: script, stageName: stageName) {
@@ -42,9 +46,14 @@ void call(Map parameters = [:]) {
utils.pushToSWA([step: STEP_NAME], config)
durationMeasure(script: script, measurementName: 'promote_duration') {
if(config.containerPushToRegistry) {
if (config.containerPushToRegistry) {
containerPushToRegistry script: script
}
if (config.nexusUpload) {
nexusUpload script: script
ReportAggregator.instance.reportDeploymentToNexus()
}
}
}
}