1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge branch 'master' into CCFenner/newman

This commit is contained in:
Christopher Fenner 2018-08-09 10:34:00 +02:00 committed by GitHub
commit 059268459a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 420 additions and 54 deletions

View File

@ -23,19 +23,19 @@ none
| ----------|-----------|---------|-----------------| | ----------|-----------|---------|-----------------|
| script | no | empty `commonPipelineEnvironment` | | | script | no | empty `commonPipelineEnvironment` | |
| artifactType | no | | 'appContainer' | | artifactType | no | | 'appContainer' |
| buildTool | no | maven | maven, docker | | buildTool | no | maven | docker, dlang, golang, maven, mta, npm, pip, sbt |
| commitVersion | no | `true` | `true`, `false` | | commitVersion | no | `true` | `true`, `false` |
| dockerVersionSource | no | `''` | FROM, (ENV name),appVersion | | dockerVersionSource | no | `''` | FROM, (ENV name),appVersion |
| filePath | no | buildTool=`maven`: pom.xml <br />docker: Dockerfile | | | filePath | no | buildTool=`docker`: Dockerfile <br />buildTool=`dlang`: dub.json <br />buildTool=`golang`: VERSION <br />buildTool=`maven`: pom.xml <br />buildTool=`mta`: mta.yaml <br />buildTool=`npm`: package.json <br />buildTool=`pip`: version.txt <br />buildTool=`sbt`: sbtDescriptor.json| |
| gitCommitId | no | `GitUtils.getGitCommitId()` | | | gitCommitId | no | `GitUtils.getGitCommitId()` | |
| gitCredentialsId | If `commitVersion` is `true` | as defined in custom configuration | | | gitSshCredentialsId | If `commitVersion` is `true` | as defined in custom configuration | |
| gitUserEMail | no | | | | gitUserEMail | no | | |
| gitUserName | no | | | | gitUserName | no | | |
| gitSshUrl | If `commitVersion` is `true` | | | | gitSshUrl | If `commitVersion` is `true` | | |
| tagPrefix | no | 'build_' | | | tagPrefix | no | 'build_' | |
| timestamp | no | current time in format according to `timestampTemplate` | | | timestamp | no | current time in format according to `timestampTemplate` | |
| timestampTemplate | no | `%Y%m%d%H%M%S` | | | timestampTemplate | no | `%Y%m%d%H%M%S` | |
| versioningTemplate | no | depending on `buildTool`<br />maven: `${version}-${timestamp}${commitId?"_"+commitId:""}` | | | versioningTemplate | no |buildTool=`docker`: `${version}-${timestamp}${commitId?"_"+commitId:""}`<br> />buildTool=`dlang`: `${version}-${timestamp}${commitId?"+"+commitId:""}`<br />buildTool=`golang`:`${version}-${timestamp}${commitId?"+"+commitId:""}`<br />buildTool=`maven`: `${version}-${timestamp}${commitId?"_"+commitId:""}`<br />buildTool=`mta`: `${version}-${timestamp}${commitId?"+"+commitId:""}`<br />buildTool=`npm`: `${version}-${timestamp}${commitId?"+"+commitId:""}`<br />buildTool=`pip`: `${version}.${timestamp}${commitId?"."+commitId:""}`<br />buildTool=`sbt`: `${version}-${timestamp}${commitId?"+"+commitId:""}`| |
* `script` defines the global script environment of the Jenkinsfile run. Typically `this` is passed to this parameter. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving e.g. configuration parameters. * `script` defines the global script environment of the Jenkinsfile run. Typically `this` is passed to this parameter. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving e.g. configuration parameters.
* `artifactType` defines the type of the artifact. * `artifactType` defines the type of the artifact.
@ -49,7 +49,7 @@ none
* Using `filePath` you could define a custom path to the descriptor file. * Using `filePath` you could define a custom path to the descriptor file.
* `gitCommitId` defines the version prefix of the automatically generated version. By default it will take the long commitId hash. You could pass any other string (e.g. the short commitId hash) to be used. In case you don't want to have the gitCommitId added to the automatic versioning string you could set the value to an empty string: `''`. * `gitCommitId` defines the version prefix of the automatically generated version. By default it will take the long commitId hash. You could pass any other string (e.g. the short commitId hash) to be used. In case you don't want to have the gitCommitId added to the automatic versioning string you could set the value to an empty string: `''`.
* `gitCredentialsId`defines the ssh git credentials to be used for writing the tag. * `gitSshCredentialsId`defines the ssh git credentials to be used for writing the tag.
* The parameters `gitUserName` and `gitUserEMail` allow to overwrite the global git settings available on your Jenkins server * The parameters `gitUserName` and `gitUserEMail` allow to overwrite the global git settings available on your Jenkins server
* `gitSshUrl` defines the git ssh url to the source code repository. * `gitSshUrl` defines the git ssh url to the source code repository.
* `tagPrefix` defines the prefix wich is used for the git tag which is written during the versioning run. * `tagPrefix` defines the prefix wich is used for the git tag which is written during the versioning run.

View File

@ -11,21 +11,37 @@ general:
from: 'origin/master' from: 'origin/master'
to: 'HEAD' to: 'HEAD'
format: '%b' format: '%b'
gitSshKeyCredentialsId: '' #needed to allow sshagent to run with local ssh key
#Steps Specific Configuration #Steps Specific Configuration
steps: steps:
artifactSetVersion: artifactSetVersion:
timestampTemplate: '%Y%m%d%H%M%S' timestampTemplate: '%Y%m%d%H%M%S'
tagPrefix: 'build_' tagPrefix: 'build_'
commitVersion: true commitVersion: true
dlang:
filePath: 'dub.json'
versioningTemplate: '${version}-${timestamp}${commitId?"+"+commitId:""}'
docker: docker:
filePath: 'Dockerfile' filePath: 'Dockerfile'
versioningTemplate: '${version}-${timestamp}${commitId?"_"+commitId:""}' versioningTemplate: '${version}-${timestamp}${commitId?"_"+commitId:""}'
golang:
filePath: 'VERSION'
versioningTemplate: '${version}-${timestamp}${commitId?"+"+commitId:""}'
maven: maven:
filePath: 'pom.xml' filePath: 'pom.xml'
versioningTemplate: '${version}-${timestamp}${commitId?"_"+commitId:""}' versioningTemplate: '${version}-${timestamp}${commitId?"_"+commitId:""}'
mta: mta:
filePath: 'mta.yaml' filePath: 'mta.yaml'
versioningTemplate: '${version}-${timestamp}${commitId?"+"+commitId:""}' versioningTemplate: '${version}-${timestamp}${commitId?"+"+commitId:""}'
npm:
filePath: 'package.json'
versioningTemplate: '${version}-${timestamp}${commitId?"+"+commitId:""}'
pip:
filePath: 'version.txt'
versioningTemplate: '${version}.${timestamp}${commitId?"."+commitId:""}'
sbt:
filePath: 'sbtDescriptor.json'
versioningTemplate: '${version}-${timestamp}${commitId?"+"+commitId:""}'
checksPublishResults: checksPublishResults:
aggregation: aggregation:
active: true active: true

View File

@ -12,12 +12,22 @@ abstract class ArtifactVersioning implements Serializable {
public static getArtifactVersioning(buildTool, script, configuration) { public static getArtifactVersioning(buildTool, script, configuration) {
switch (buildTool) { switch (buildTool) {
case 'mta': case 'dlang':
return new MtaArtifactVersioning(script, configuration) return new DlangArtifactVersioning(script, configuration)
case 'maven':
return new MavenArtifactVersioning(script, configuration)
case 'docker': case 'docker':
return new DockerArtifactVersioning(script, configuration) return new DockerArtifactVersioning(script, configuration)
case 'golang':
return new GolangArtifactVersioning(script, configuration)
case 'maven':
return new MavenArtifactVersioning(script, configuration)
case 'mta':
return new MtaArtifactVersioning(script, configuration)
case 'npm':
return new NpmArtifactVersioning(script, configuration)
case 'pip':
return new PipArtifactVersioning(script, configuration)
case 'sbt':
return new SbtArtifactVersioning(script, configuration)
default: default:
throw new IllegalArgumentException("No versioning implementation for buildTool: ${buildTool} available.") throw new IllegalArgumentException("No versioning implementation for buildTool: ${buildTool} available.")
} }

View File

@ -0,0 +1,20 @@
package com.sap.piper.versioning
class DlangArtifactVersioning extends ArtifactVersioning {
protected DlangArtifactVersioning(script, configuration) {
super(script, configuration)
}
@Override
def getVersion() {
def descriptor = script.readJSON file: configuration.filePath
return descriptor.version
}
@Override
def setVersion(version) {
def descriptor = script.readJSON file: configuration.filePath
descriptor.version = new String(version)
script.writeJSON file: configuration.filePath, json: descriptor
}
}

View File

@ -5,13 +5,29 @@ class DockerArtifactVersioning extends ArtifactVersioning {
super(script, configuration) super(script, configuration)
} }
@Override
def getVersion() { def getVersion() {
if (configuration.dockerVersionSource == 'FROM') if(configuration.artifactType == 'appContainer' && configuration.dockerVersionSource == 'appVersion'){
//replace + sign if available since + is not allowed in a Docker tag
if (script.commonPipelineEnvironment.getArtifactVersion()){
return script.commonPipelineEnvironment.getArtifactVersion().replace('+', '_')
}else{
throw new IllegalArgumentException("No artifact version available for 'dockerVersionSource: appVersion' -> executeBuild needs to run for the application artifact first to set the appVersion attribute.'")
}
} else if (configuration.dockerVersionSource == 'FROM') {
def version = getVersionFromDockerBaseImageTag(configuration.filePath)
if (version) {
return getVersionFromDockerBaseImageTag(configuration.filePath) return getVersionFromDockerBaseImageTag(configuration.filePath)
else } else {
//standard assumption: version is assigned to an env variable throw new IllegalArgumentException("No version information available in FROM statement")
return getVersionFromDockerEnvVariable(configuration.filePath, configuration.dockerVersionSource) }
} else {
def version = getVersionFromDockerEnvVariable(configuration.filePath, configuration.dockerVersionSource)
if (version) {
return version
} else {
throw new IllegalArgumentException("ENV variable '${configuration.dockerVersionSource}' not found.")
}
}
} }
@Override @Override

View File

@ -0,0 +1,17 @@
package com.sap.piper.versioning
class GolangArtifactVersioning extends ArtifactVersioning {
protected GolangArtifactVersioning(script, configuration) {
super(script, configuration)
}
@Override
def getVersion() {
return script.readFile(configuration.filePath).split('\n')[0].trim()
}
@Override
def setVersion(version) {
script.writeFile file: configuration.filePath, text: version
}
}

View File

@ -0,0 +1,20 @@
package com.sap.piper.versioning
class NpmArtifactVersioning extends ArtifactVersioning {
protected NpmArtifactVersioning(script, configuration) {
super(script, configuration)
}
@Override
def getVersion() {
def packageJson = script.readJSON file: configuration.filePath
return packageJson.version
}
@Override
def setVersion(version) {
def packageJson = script.readJSON file: configuration.filePath
packageJson.version = new String(version)
script.writeJSON file: configuration.filePath, json: packageJson
}
}

View File

@ -0,0 +1,17 @@
package com.sap.piper.versioning
class PipArtifactVersioning extends ArtifactVersioning {
protected PipArtifactVersioning(script, configuration) {
super(script, configuration)
}
@Override
def getVersion() {
return script.readFile(configuration.filePath).split('\n')[0].trim()
}
@Override
def setVersion(version) {
script.writeFile file: configuration.filePath, text: version
}
}

View File

@ -0,0 +1,20 @@
package com.sap.piper.versioning
class SbtArtifactVersioning extends ArtifactVersioning {
protected SbtArtifactVersioning(script, configuration) {
super(script, configuration)
}
@Override
def getVersion() {
def sbtDescriptorJson = script.readJSON file: configuration.filePath
return sbtDescriptorJson.version
}
@Override
def setVersion(version) {
def sbtDescriptorJson = script.readJSON file: configuration.filePath
sbtDescriptorJson.version = new String(version)
script.writeJSON file: configuration.filePath, json: sbtDescriptorJson
}
}

View File

@ -47,6 +47,9 @@ class ArtifactSetVersionTest extends BasePiperTest {
void init() throws Throwable { void init() throws Throwable {
dockerParameters = [:] dockerParameters = [:]
nullScript.commonPipelineEnvironment.setArtifactVersion(null)
nullScript.commonPipelineEnvironment.setGitSshUrl('git@test.url')
helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure -> helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure ->
sshAgentList = list sshAgentList = list
return closure() return closure()
@ -64,7 +67,7 @@ class ArtifactSetVersionTest extends BasePiperTest {
@Test @Test
void testVersioning() { void testVersioning() {
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl') jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl')
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion()) assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
assertEquals('testCommitId', jer.env.getGitCommitId()) assertEquals('testCommitId', jer.env.getGitCommitId())
@ -79,7 +82,7 @@ class ArtifactSetVersionTest extends BasePiperTest {
@Test @Test
void testVersioningWithoutCommit() { void testVersioningWithoutCommit() {
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false) jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion()) assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId")) assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
@ -87,7 +90,7 @@ class ArtifactSetVersionTest extends BasePiperTest {
@Test @Test
void testVersioningWithoutScript() { void testVersioningWithoutScript() {
jsr.step.call(juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false) jsr.step.artifactSetVersion(juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion()) assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId")) assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId"))
@ -95,14 +98,14 @@ class ArtifactSetVersionTest extends BasePiperTest {
@Test @Test
void testVersioningCustomGitUserAndEMail() { void testVersioningCustomGitUserAndEMail() {
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test') jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test')
assertThat(jscr.shell, hasItem("git -c user.email=\"test@test.com\" -c user.name=\"test\" commit -m 'update version 1.2.3-20180101010203_testCommitId'")) assertThat(jscr.shell, hasItem("git -c user.email=\"test@test.com\" -c user.name=\"test\" commit -m 'update version 1.2.3-20180101010203_testCommitId'"))
} }
@Test @Test
void testVersioningWithTimestamp() { void testVersioningWithTimestamp() {
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', timestamp: '2018') jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', timestamp: '2018')
assertEquals('1.2.3-2018_testCommitId', jer.env.getArtifactVersion()) assertEquals('1.2.3-2018_testCommitId', jer.env.getArtifactVersion())
} }
@ -110,23 +113,35 @@ class ArtifactSetVersionTest extends BasePiperTest {
void testVersioningNoBuildTool() { void testVersioningNoBuildTool() {
thrown.expect(Exception) thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool') thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool')
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils) jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils)
} }
@Test @Test
void testVersioningWithCustomTemplate() { void testVersioningWithCustomTemplate() {
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', versioningTemplate: '${version}-xyz') jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', versioningTemplate: '${version}-xyz')
assertEquals('1.2.3-xyz', jer.env.getArtifactVersion()) assertEquals('1.2.3-xyz', jer.env.getArtifactVersion())
} }
@Test @Test
void testVersioningWithTypeAppContainer() { void testVersioningWithTypeAppContainer() {
nullScript.commonPipelineEnvironment.setAppContainerProperty('gitSshUrl', 'git@test.url')
jer.env.setArtifactVersion('1.2.3-xyz') jer.env.setArtifactVersion('1.2.3-xyz')
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'docker', artifactType: 'appContainer', dockerVersionSource: 'appVersion') jsr.step.artifactSetVersion(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'docker', artifactType: 'appContainer', dockerVersionSource: 'appVersion')
assertEquals('1.2.3-xyz', jer.env.getArtifactVersion()) assertEquals('1.2.3-xyz', jer.env.getArtifactVersion())
assertEquals('1.2.3-xyz', jwfr.files['VERSION']) assertEquals('1.2.3-xyz', jwfr.files['VERSION'])
} }
@Test
void testCredentialCompatibility() {
jsr.step.artifactSetVersion (
script: nullScript,
buildTool: 'maven',
gitCredentialsId: 'testCredentials',
juStabGitUtils: gitUtils
)
assertThat(sshAgentList, hasItem('testCredentials'))
}
void prepareObjectInterceptors(object) { void prepareObjectInterceptors(object) {
object.metaClass.invokeMethod = helper.getMethodInterceptor() object.metaClass.invokeMethod = helper.getMethodInterceptor()
object.metaClass.static.invokeMethod = helper.getMethodInterceptor() object.metaClass.static.invokeMethod = helper.getMethodInterceptor()

View File

@ -0,0 +1,32 @@
package com.sap.piper.versioning
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsReadJsonRule
import util.JenkinsWriteJsonRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
class DlangArtifactVersioningTest extends BasePiperTest{
JenkinsReadJsonRule jrjr = new JenkinsReadJsonRule(this, 'test/resources/versioning/DlangArtifactVersioning/')
JenkinsWriteJsonRule jwjr = new JenkinsWriteJsonRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(jrjr)
.around(jwjr)
@Test
void testVersioning() {
DlangArtifactVersioning av = new DlangArtifactVersioning(nullScript, [filePath: 'dub.json'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertTrue(jwjr.files['dub.json'].contains('1.2.3-20180101'))
}
}

View File

@ -0,0 +1,31 @@
package com.sap.piper.versioning
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsReadFileRule
import util.JenkinsWriteFileRule
import util.Rules
import static org.junit.Assert.assertEquals
class GolangArtifactVersioningTest extends BasePiperTest{
JenkinsReadFileRule jrfr = new JenkinsReadFileRule(this, 'test/resources/versioning/GolangArtifactVersioning/')
JenkinsWriteFileRule jwfr = new JenkinsWriteFileRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(jrfr)
.around(jwfr)
@Test
void testVersioning() {
GolangArtifactVersioning av = new GolangArtifactVersioning(nullScript, [filePath: 'VERSION'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertEquals('1.2.3-20180101', jwfr.files['VERSION'])
}
}

View File

@ -0,0 +1,32 @@
package com.sap.piper.versioning
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsReadJsonRule
import util.JenkinsWriteJsonRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
class NpmArtifactVersioningTest extends BasePiperTest{
JenkinsReadJsonRule jrjr = new JenkinsReadJsonRule(this, 'test/resources/versioning/NpmArtifactVersioning/')
JenkinsWriteJsonRule jwjr = new JenkinsWriteJsonRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(jrjr)
.around(jwjr)
@Test
void testVersioning() {
NpmArtifactVersioning av = new NpmArtifactVersioning(nullScript, [filePath: 'package.json'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertTrue(jwjr.files['package.json'].contains('1.2.3-20180101'))
}
}

View File

@ -0,0 +1,31 @@
package com.sap.piper.versioning
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsReadFileRule
import util.JenkinsWriteFileRule
import util.Rules
import static org.junit.Assert.assertEquals
class PipArtifactVersioningTest extends BasePiperTest{
JenkinsReadFileRule jrfr = new JenkinsReadFileRule(this, 'test/resources/versioning/PipArtifactVersioning/')
JenkinsWriteFileRule jwfr = new JenkinsWriteFileRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(jrfr)
.around(jwfr)
@Test
void testVersioning() {
PipArtifactVersioning av = new PipArtifactVersioning(nullScript, [filePath: 'version.txt'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertEquals('1.2.3-20180101', jwfr.files['version.txt'])
}
}

View File

@ -0,0 +1,32 @@
package com.sap.piper.versioning
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsReadJsonRule
import util.JenkinsWriteJsonRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
class SbtArtifactVersioningTest extends BasePiperTest{
JenkinsReadJsonRule jrjr = new JenkinsReadJsonRule(this, 'test/resources/versioning/SbtArtifactVersioning/')
JenkinsWriteJsonRule jwjr = new JenkinsWriteJsonRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(jrjr)
.around(jwjr)
@Test
void testVersioning() {
SbtArtifactVersioning av = new SbtArtifactVersioning(nullScript, [filePath: 'sbtDescriptor.json'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertTrue(jwjr.files['sbtDescriptor.json'].contains('1.2.3-20180101'))
}
}

View File

@ -0,0 +1,34 @@
package util
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
class JenkinsWriteJsonRule implements TestRule {
final BasePipelineTest testInstance
Map files = [:]
JenkinsWriteJsonRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
}
@Override
Statement apply(Statement base, Description description) {
return statement(base)
}
private Statement statement(final Statement base) {
return new Statement() {
@Override
void evaluate() throws Throwable {
testInstance.helper.registerAllowedMethod( 'writeJSON', [Map.class], {m -> files[m.file] = m.json.toString()})
base.evaluate()
}
}
}
}

View File

@ -0,0 +1,5 @@
{
"name": "sap-pipeline-test",
"version": "1.2.3",
"description": "package.json for testing",
}

View File

@ -0,0 +1 @@
1.2.3

View File

@ -0,0 +1,15 @@
{
"name": "sap-pipeline-test",
"version": "1.2.3",
"private": false,
"description": "package.json for testing",
"engines": {
"node": "6.x"
},
"dependencies": {
"apn": "1.7.8"
},
"devDependencies": {
"chai": "^3.4.1"
}
}

View File

@ -0,0 +1 @@
1.2.3

View File

@ -0,0 +1,4 @@
{
"name": "sap-pipeline-test",
"version": "1.2.3",
}

View File

@ -8,13 +8,14 @@ import groovy.text.SimpleTemplateEngine
@Field String STEP_NAME = 'artifactSetVersion' @Field String STEP_NAME = 'artifactSetVersion'
@Field Set GENERAL_CONFIG_KEYS = ['collectTelemetryData'] @Field Set GENERAL_CONFIG_KEYS = ['collectTelemetryData']
@Field Map CONFIG_KEY_COMPATIBILITY = [gitSshKeyCredentialsId: 'gitCredentialsId']
@Field Set STEP_CONFIG_KEYS = [ @Field Set STEP_CONFIG_KEYS = [
'artifactType', 'artifactType',
'buildTool', 'buildTool',
'commitVersion', 'commitVersion',
'dockerVersionSource', 'dockerVersionSource',
'filePath', 'filePath',
'gitCredentialsId', 'gitSshKeyCredentialsId',
'gitUserEMail', 'gitUserEMail',
'gitUserName', 'gitUserName',
'gitSshUrl', 'gitSshUrl',
@ -25,7 +26,7 @@ import groovy.text.SimpleTemplateEngine
] ]
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus('gitCommitId') @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus('gitCommitId')
def call(Map parameters = [:]) { def call(Map parameters = [:], Closure body = null) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) { handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
@ -43,49 +44,45 @@ def call(Map parameters = [:]) {
// load default & individual configuration // load default & individual configuration
Map config = ConfigurationHelper Map config = ConfigurationHelper
.loadStepDefaults(this) .loadStepDefaults(this)
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS) .mixinGeneralConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS, this, CONFIG_KEY_COMPATIBILITY)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS) .mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS, this, CONFIG_KEY_COMPATIBILITY)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS) .mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS, this, CONFIG_KEY_COMPATIBILITY)
.mixin(gitCommitId: gitUtils.getGitCommitIdOrNull()) .mixin(gitCommitId: gitUtils.getGitCommitIdOrNull())
.mixin(parameters, PARAMETER_KEYS) .mixin(parameters, PARAMETER_KEYS, this, CONFIG_KEY_COMPATIBILITY)
.withMandatoryProperty('buildTool') .withMandatoryProperty('buildTool')
.dependingOn('buildTool').mixin('filePath')
.dependingOn('buildTool').mixin('versioningTemplate')
.use()
config = new ConfigurationHelper(config)
.addIfEmpty('gitSshUrl', (config.buildTool == 'docker' && config.artifactType == 'appContainer')?script.commonPipelineEnvironment.getAppContainerProperty('gitSshUrl'):script.commonPipelineEnvironment.getGitSshUrl())
.addIfEmpty('timestamp', getTimestamp(config.timestampTemplate))
.withMandatoryProperty('gitSshUrl')
.use() .use()
new Utils().pushToSWA([step: STEP_NAME, stepParam1: config.buildTool], config) new Utils().pushToSWA([step: STEP_NAME, stepParam1: config.buildTool], config)
if (!config.filePath)
config.filePath = config[config.buildTool].filePath //use default configuration
def newVersion
def artifactVersioning = ArtifactVersioning.getArtifactVersioning(config.buildTool, script, config) def artifactVersioning = ArtifactVersioning.getArtifactVersioning(config.buildTool, script, config)
if(config.artifactType == 'appContainer' && config.dockerVersionSource == 'appVersion'){
if (script.commonPipelineEnvironment.getArtifactVersion())
//replace + sign if available since + is not allowed in a Docker tag
newVersion = script.commonPipelineEnvironment.getArtifactVersion().replace('+', '_')
else
error ("[${STEP_NAME}] No artifact version available for 'dockerVersionSource: appVersion' -> executeBuild needs to run for the application artifact first to set the artifactVersion for the application artifact.'")
} else {
def currentVersion = artifactVersioning.getVersion() def currentVersion = artifactVersioning.getVersion()
def timestamp = config.timestamp ? config.timestamp : getTimestamp(config.timestampTemplate) def newVersion
if (config.artifactType == 'appContainer' && config.dockerVersionSource == 'appVersion'){
def versioningTemplate = config.versioningTemplate ? config.versioningTemplate : config[config.buildTool].versioningTemplate newVersion = currentVersion
//defined in default configuration } else {
def binding = [version: currentVersion, timestamp: timestamp, commitId: config.gitCommitId] def binding = [version: currentVersion, timestamp: config.timestamp, commitId: config.gitCommitId]
def templatingEngine = new SimpleTemplateEngine() newVersion = new SimpleTemplateEngine().createTemplate(config.versioningTemplate).make(binding).toString()
def template = templatingEngine.createTemplate(versioningTemplate).make(binding)
newVersion = template.toString()
} }
artifactVersioning.setVersion(newVersion) artifactVersioning.setVersion(newVersion)
def gitCommitId if(body != null){
body(newVersion)
}
if (config.commitVersion) { if (config.commitVersion) {
sh 'git add .' sh 'git add .'
sshagent([config.gitCredentialsId]) { sshagent([config.gitSshKeyCredentialsId]) {
def gitUserMailConfig = '' def gitUserMailConfig = ''
if (config.gitUserName && config.gitUserEMail) if (config.gitUserName && config.gitUserEMail)
gitUserMailConfig = "-c user.email=\"${config.gitUserEMail}\" -c user.name=\"${config.gitUserName}\"" gitUserMailConfig = "-c user.email=\"${config.gitUserEMail}\" -c user.name=\"${config.gitUserName}\""
@ -99,17 +96,17 @@ def call(Map parameters = [:]) {
sh "git tag ${config.tagPrefix}${newVersion}" sh "git tag ${config.tagPrefix}${newVersion}"
sh "git push origin ${config.tagPrefix}${newVersion}" sh "git push origin ${config.tagPrefix}${newVersion}"
gitCommitId = gitUtils.getGitCommitIdOrNull() config.gitCommitId = gitUtils.getGitCommitIdOrNull()
} }
} }
if (config.buildTool == 'docker' && config.artifactType == 'appContainer') { if (config.buildTool == 'docker' && config.artifactType == 'appContainer') {
script.commonPipelineEnvironment.setAppContainerProperty('artifactVersion', newVersion) script.commonPipelineEnvironment.setAppContainerProperty('artifactVersion', newVersion)
script.commonPipelineEnvironment.setAppContainerProperty('gitCommitId', gitCommitId) script.commonPipelineEnvironment.setAppContainerProperty('gitCommitId', config.gitCommitId)
} else { } else {
//standard case //standard case
script.commonPipelineEnvironment.setArtifactVersion(newVersion) script.commonPipelineEnvironment.setArtifactVersion(newVersion)
script.commonPipelineEnvironment.setGitCommitId(gitCommitId) script.commonPipelineEnvironment.setGitCommitId(config.gitCommitId)
} }
echo "[${STEP_NAME}]New version: ${newVersion}" echo "[${STEP_NAME}]New version: ${newVersion}"