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'
}

在这里插入图片描述

步骤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

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页