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

Add npmExecute step (#491)

This commit is contained in:
yemengsap
2019-02-21 19:14:48 +01:00
committed by Christoph Szymanski
parent ada3ed909d
commit 38c5a0d779
9 changed files with 167 additions and 12 deletions

View File

@@ -2,7 +2,7 @@
## Description
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Prerequisites
@@ -10,11 +10,11 @@ Content here is generated from corresponnding step, see `vars`.
## Parameters
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Step configuration
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Exceptions

View File

@@ -2,11 +2,11 @@
## Description
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Parameters
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Kubernetes support
@@ -14,7 +14,7 @@ If the Jenkins is setup on a Kubernetes cluster, then you can execute the closur
## Step configuration
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Side effects

View File

@@ -2,7 +2,7 @@
## Description
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Prerequisites
@@ -13,11 +13,11 @@ Content here is generated from corresponnding step, see `vars`.
## Parameters
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Step configuration
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Side effects

View File

@@ -0,0 +1,23 @@
# npmExecute
## Description
Content here is generated from corresponding step, see `vars`.
## Parameters
Content here is generated from corresponding step, see `vars`.
## Step configuration
Content here is generated from corresponding step, see `vars`.
## Exceptions
None
## Examples
```groovy
npmExecute script: this, dockerImage: 'node:8-stretch', npmCommand: 'run build'
```

View File

@@ -2,17 +2,17 @@
## Description
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Prerequisites
## Parameters
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Step configuration
Content here is generated from corresponnding step, see `vars`.
Content here is generated from corresponding step, see `vars`.
## Exceptions

View File

@@ -24,6 +24,7 @@ nav:
- mtaBuild: steps/mtaBuild.md
- neoDeploy: steps/neoDeploy.md
- newmanExecute: steps/newmanExecute.md
- npmExecute: steps/npmExecute.md
- pipelineExecute: steps/pipelineExecute.md
- pipelineRestartSteps: steps/pipelineRestartSteps.md
- pipelineStashFiles: steps/pipelineStashFiles.md

View File

@@ -252,6 +252,8 @@ steps:
newmanRunCommand: "run '${config.newmanCollection}' --environment '${config.newmanEnvironment}' --globals '${config.newmanGlobals}' --reporters junit,html --reporter-junit-export 'target/newman/TEST-${collectionDisplayName}.xml' --reporter-html-export 'target/newman/TEST-${collectionDisplayName}.html'"
stashContent:
- 'tests'
npmExecute:
dockerImage: 'node:8-stretch'
pipelineRestartSteps:
sendMail: true
timeoutInSeconds: 900

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 NpmExecuteTest 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 == 'package.json' }
}
@Test
void testNpmExecute() {
stepRule.step.npmExecute(script: nullScript, dockerImage: 'node:8-stretch')
assertEquals 'node:8-stretch', dockerExecuteRule.dockerParams.dockerImage
}
@Test
void testNpmExecuteWithClosure() {
stepRule.step.npmExecute(script: nullScript, dockerImage: 'node:8-stretch', npmCommand: 'run build') { }
assert shellRule.shell.find { c -> c.contains('npm run build') }
}
@Test
void testNoPackageJson() {
helper.registerAllowedMethod 'fileExists', [String], { false }
thrown.expect AbortException
thrown.expectMessage '[npmExecute] package.json is not found.'
stepRule.step.npmExecute(script: nullScript, dockerImage: 'node:8-stretch', npmCommand: 'run build')
}
}

73
vars/npmExecute.groovy Normal file
View File

@@ -0,0 +1,73 @@
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 'node:8-stretch'.
*/
'dockerImage',
/**
* URL of default NPM registry
*/
'defaultNpmRegistry',
/**
* Which NPM command should be executed.
*/
'npmCommand']
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS + [
/**
* Docker options to be set when starting the container.
*/
'dockerOptions']
/**
* Executes NPM commands inside a docker container.
* Docker image, docker options and npm 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)
try {
if (!fileExists('package.json')) {
error "[${STEP_NAME}] package.json is not found."
}
dockerExecute(script: script, dockerImage: configuration.dockerImage, dockerOptions: configuration.dockerOptions) {
if (configuration.defaultNpmRegistry) {
sh "npm config set registry ${configuration.defaultNpmRegistry}"
}
if (configuration.npmCommand) {
sh "npm ${configuration.npmCommand}"
}
if (body) {
body()
}
}
} catch (Exception e) {
println "Error while executing npm. Here are the logs:"
sh "cat ~/.npm/_logs/*"
throw e
}
}
}