From 64c6a6a8763fbd16051cd2147532d064cb9b3739 Mon Sep 17 00:00:00 2001 From: Eric Ball Date: Tue, 24 Aug 2021 12:48:22 -0700 Subject: [PATCH] Feat: Initial NodeJS workflow Issue: RELENG-2796 Signed-off-by: Eric Ball Change-Id: Ifa2955450c741265d6c10a1b0cacd438f5605256 --- Jenkinsfile.example.groovy | 11 ++++- docs/vars/lfNode.rst | 18 ++++++++ .../notes/node-workflow-7091235ee494af64.yaml | 5 +++ src/test/groovy/LFNodeSpec.groovy | 50 ++++++++++++++++++++++ vars/lfDefaults.groovy | 3 ++ vars/lfNode.groovy | 50 ++++++++++++++++++++++ 6 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 docs/vars/lfNode.rst create mode 100644 releasenotes/notes/node-workflow-7091235ee494af64.yaml create mode 100644 src/test/groovy/LFNodeSpec.groovy create mode 100644 vars/lfNode.groovy diff --git a/Jenkinsfile.example.groovy b/Jenkinsfile.example.groovy index ac7c5e7..6906075 100644 --- a/Jenkinsfile.example.groovy +++ b/Jenkinsfile.example.groovy @@ -19,8 +19,10 @@ loadGlobalLibrary() pipeline { agent { - // This label should match an agent available on the target system - label "centos7-docker-4c-2g" + node { + // This label should match an agent available on the target system + label "centos7-docker-4c-2g" + } } options { @@ -39,6 +41,11 @@ pipeline { lfJava(mvnSettings=env.mvnSettings) } } + stage("Node Verify") { + steps { + lfNode() + } + } stage("Parallel Testing") { parallel { stage("amd") { diff --git a/docs/vars/lfNode.rst b/docs/vars/lfNode.rst new file mode 100644 index 0000000..8835f5f --- /dev/null +++ b/docs/vars/lfNode.rst @@ -0,0 +1,18 @@ +###### +lfNode +###### + +Parameters +========== + +:Optional Parameters: + + :nodeDir: Root of NodeJS project. + :nodeVersion: The version of NodeJS to use. This must be installed in + Jenkins' global tools. + +Usage +===== + +Calling lfNode will execute a simple ``npm install`` followed by ``npm test`` in +order to validate the code. diff --git a/releasenotes/notes/node-workflow-7091235ee494af64.yaml b/releasenotes/notes/node-workflow-7091235ee494af64.yaml new file mode 100644 index 0000000..3a28bcc --- /dev/null +++ b/releasenotes/notes/node-workflow-7091235ee494af64.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add the lfNode job, which replicates the functionality of the global-jjb + ``node-verify`` job. diff --git a/src/test/groovy/LFNodeSpec.groovy b/src/test/groovy/LFNodeSpec.groovy new file mode 100644 index 0000000..9cfcb2c --- /dev/null +++ b/src/test/groovy/LFNodeSpec.groovy @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright (c) 2021 The Linux Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification + +public class LFNodeSpec extends JenkinsPipelineSpecification { + + def lfNode = null + def defaults = [ + nodeDir: "", + nodeVersion: "14.17.5", + ] + + def setup() { + lfNode = loadPipelineScriptForTest("vars/lfNode.groovy") + explicitlyMockPipelineVariable("lfDefaults") + } + + def "Test lfNode [Should] build NodeJS [When] called" () { + setup: + explicitlyMockPipelineStep("dir") + explicitlyMockPipelineStep("nodejs") + getPipelineMock("lfDefaults.call")() >> { + return defaults + } + when: + lfNode() + then: + 1 * getPipelineMock("dir").call(_) + 1 * getPipelineMock("nodejs").call(_) >> { _arguments -> + assert _arguments[0][0]["nodeJSInstallationName"] == defaults["nodeVersion"] + assert _arguments[0][0]["configId"] == "npmrc" + } + 1 * getPipelineMock("sh").call("npm install") + 1 * getPipelineMock("sh").call("npm test") + } +} diff --git a/vars/lfDefaults.groovy b/vars/lfDefaults.groovy index 1540a67..edd116a 100644 --- a/vars/lfDefaults.groovy +++ b/vars/lfDefaults.groovy @@ -31,6 +31,9 @@ def call(body) { mvnGoals: "clean install", mvnVersion: "mvn35", mvnGlobalSettings: "global-settings", + + nodeDir: "", + nodeVersion: "14.17.5", ] return defaults } diff --git a/vars/lfNode.groovy b/vars/lfNode.groovy new file mode 100644 index 0000000..8cf8288 --- /dev/null +++ b/vars/lfNode.groovy @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright (c) 2021 The Linux Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * Method to run Node jobs. + * Optional body values (should be defined in lfDefaults): + * * nodeDir + * * nodeVersion + * + * @param body Config values to be provided in the form "key = value". + */ +def call(body) { + // Evaluate the body block and collect configuration into the object + def defaults = lfDefaults() + def config = [:] + + if (body) { + body.resolveStrategy = Closure.DELEGATE_FIRST + body.delegate = config + body() + } + + // For duplicate keys, Groovy will use the right hand map's values. + config = defaults + config + + /////////////////// + // Verify NodeJS // + /////////////////// + dir(config.nodeDir) { + // The nodeJS installation configured in Jenkins' Global Tools should + // be named simply with the version. + nodejs(nodeJSInstallationName: config.nodeVersion, configId: "npmrc") { + sh "npm install" + sh "npm test" + } + } +} -- 2.16.6