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

Add dubExecute step (#754)

* Add dubExecute step
* Fix test
This commit is contained in:
andre2007
2019-06-19 16:52:18 +02:00
committed by Oliver Nocon
parent 79894bb36f
commit 36c029b344
5 changed files with 147 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# ${docGenStepName}
## ${docGenParameters}
## ${docGenConfiguration}
## ${docJenkinsPluginDependencies}
## Exceptions
None
## Examples
```groovy
dubExecute script: this, dockerImage: 'dlang2/dmd-ubuntu:latest', dubCommand: 'build'
```

View File

@@ -14,6 +14,7 @@ nav:
- detectExecuteScan: steps/detectExecuteScan.md
- dockerExecute: steps/dockerExecute.md
- dockerExecuteOnKubernetes: steps/dockerExecuteOnKubernetes.md
- dubExecute: steps/dubExecute.md
- durationMeasure: steps/durationMeasure.md
- gaugeExecuteTests: steps/gaugeExecuteTests.md
- githubPublishRelease: steps/githubPublishRelease.md

View File

@@ -211,6 +211,8 @@ steps:
workspace: '**/*'
stashExcludes:
workspace: 'nohup.out'
dubExecute:
dockerImage: 'dlang2/dmd-ubuntu:latest'
githubPublishRelease:
addClosedIssues: false
addDeltaToLastRelease: false

View File

@@ -0,0 +1,56 @@
import static org.junit.Assert.assertEquals
import hudson.AbortException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsDockerExecuteRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.Rules
class DubExecuteTest extends BasePiperTest {
private ExpectedException thrown = new ExpectedException().none()
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsReadYamlRule yamlRule = new JenkinsReadYamlRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(thrown)
.around(yamlRule)
.around(dockerExecuteRule)
.around(shellRule)
.around(stepRule)
@Before
void init() {
helper.registerAllowedMethod 'fileExists', [String], { s -> s == 'dub.json' }
}
@Test
void testDubExecute() {
stepRule.step.dubExecute(script: nullScript, dockerImage: 'dlang2/dmd-ubuntu:latest')
assertEquals 'dlang2/dmd-ubuntu:latest', dockerExecuteRule.dockerParams.dockerImage
}
@Test
void testDubExecuteWithClosure() {
stepRule.step.dubExecute(script: nullScript, dockerImage: 'dlang2/dmd-ubuntu:latest', dubCommand: 'build') { }
assert shellRule.shell.find { c -> c.contains('dub build') }
}
@Test
void testNoDubJson() {
helper.registerAllowedMethod 'fileExists', [String], { false }
thrown.expect AbortException
thrown.expectMessage '[dubExecute] Neither dub.json nor dub.sdl was found.'
stepRule.step.dubExecute(script: nullScript, dockerImage: 'dlang2/dmd-ubuntu:latest', dubCommand: 'build')
}
}

70
vars/dubExecute.groovy Normal file
View File

@@ -0,0 +1,70 @@
import static com.sap.piper.Prerequisites.checkScript
import com.sap.piper.GenerateDocumentation
import com.sap.piper.ConfigurationHelper
import com.sap.piper.Utils
import groovy.transform.Field
@Field def STEP_NAME = getClass().getName()
@Field Set GENERAL_CONFIG_KEYS = []
@Field Set STEP_CONFIG_KEYS = [
/**
* Name of the docker image that should be used, in which node should be installed and configured. Default value is 'dlang2/dmd-ubuntu:latest'.
*/
'dockerImage',
/**
* URL of default DUB registry
*/
'defaultDubRegistry',
/**
* Which DUB command should be executed.
*/
'dubCommand']
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS + [
/**
* Docker options to be set when starting the container.
*/
'dockerOptions']
/**
* Executes DUB commands inside a docker container.
* Docker image, docker options and dub commands can be specified or configured.
*/
@GenerateDocumentation
void call(Map parameters = [:], body = null) {
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
final script = checkScript(this, parameters) ?: this
// load default & individual configuration
Map configuration = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.use()
new Utils().pushToSWA([
step: STEP_NAME,
stepParamKey1: 'scriptMissing',
stepParam1: parameters?.script == null
], configuration)
if (!fileExists('dub.json') && !fileExists('dub.sdl')) {
error "[${STEP_NAME}] Neither dub.json nor dub.sdl was found."
}
dockerExecute(script: script, dockerImage: configuration.dockerImage, dockerOptions: configuration.dockerOptions) {
if (configuration.defaultDubRegistry) {
sh """
mkdir ~/.dub
echo '{"skipRegistry": "standard", "registryUrls": ["${configuration.defaultDubRegistry}"]}' > ~/.dub/settings.json
"""
}
if (configuration.dubCommand) {
sh "dub ${configuration.dubCommand}"
}
if (body) {
body()
}
}
}
}