Jenkins基础:使用NPM构建前端应用2:在DSL中使用NodeJS

上一篇文章介绍了NodeJS插件的使用方式,这一篇中继续介绍如何在Jenkinsfile中使用NodeJS。

NodeJS插件安装

NodeJS插件安装可参看如下内容:

  • https://liumiaocn.blog.csdn.net/article/details/102618269

事前准备

步骤1: 创建一个pipeline方式的job

在Jenkins里创建一个名为nodejs-pipeline-job的Pipeline方式的Job.

Scripted Pipeline方式使用NodeJS插件

步骤2: 设定Pipeline脚本

接下来以Scripted Pipeline的方式来介绍以下如何使用NodeJS插件。使用如下的Jenkinsfile

node {
    env.NODEJS_HOME = "${tool 'nodejs-8.6.0'}"
	env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
	sh 'echo $PATH'
	sh 'npm --version'
}

Jenkins基础:使用NPM构建前端应用2:在DSL中使用NodeJS_第1张图片

步骤3: 执行Job

执行Job,可以看到如下输出

Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/nodejs-pipeline-job
[Pipeline] {
[Pipeline] tool
[Pipeline] sh
+ echo /data/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs-8.6.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
/data/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs-8.6.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
[Pipeline] sh
+ npm --version
5.3.0
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Declarative Pipeline方式使用NodeJS插件

步骤2: 设定Pipeline脚本

接下来以Scripted Pipeline的方式来介绍以下如何使用NodeJS插件。使用如下的Jenkinsfile

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
            	nodejs('nodejs-8.6.0') {
            	    sh 'echo $PATH'
            	    sh 'npm -v'
            	    sh 'node -v'
                    sh 'npm config ls'
				}
            }
        }
    }
}

步骤3: 执行Job

执行Job,可以看到如下输出

Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/nodejs-pipeline-job
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] nodejs
[Pipeline] {
[Pipeline] sh
+ echo /data/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs-8.6.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
/data/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs-8.6.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
[Pipeline] sh
+ npm -v
5.3.0
[Pipeline] sh
+ node -v
v8.6.0
[Pipeline] sh
+ npm config ls
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/5.3.0 node/v8.6.0 linux x64"

; node bin location = /data/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs-8.6.0/bin/node
; cwd = /data/jenkins/workspace/nodejs-pipeline-job
; HOME = /root
; "npm config ls -l" to show all defaults.

[Pipeline] }
[Pipeline] // nodejs
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

参考内容

https://plugins.jenkins.io/nodejs

你可能感兴趣的:(#,Jenkins)