1、安装 NodeJS 插件

“系统管理”-->“管理插件”-->“可选插件”

搜索 nodejs插件,安装

2、Linux系统安装NodeJS

下载nodejs的LTS版本

# wget https://nodejs.org/dist/v12.19.0/node-v12.19.0-linux-x64.tar.gz

解压

# tar xvzf node-v11.14.0-linux-x64.tar.gz -C

修改环境变量

# vim /etc/profile

添加以下内容

#nodejs
export PATH=/usr/local/node/node-v12.19.0-linux-x64/bin:$PATH

 刷新环境变量

# source /etc/profile

安装yarn

npm  install -g yarn

yarn -v

安装cnpm与配置淘宝镜像

# npm install -g cnpm --registry=https://registry.npm.taobao.org

或直接修改 npm 下载包服务器地址为淘宝镜像

# npm config set registry https://registry.npm.taobao.org
[root@VM-0-9-centos ~]# npm config list
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.8 node/v12.19.0 linux x64"

; node bin location = /usr/local/node/node-v12.19.0/bin/node
; cwd = /root
; HOME = /root
; "npm config ls -l" to show all defaults.

[root@VM-0-9-centos ~]# npm config set registry https://registry.npm.taobao.org
[root@VM-0-9-centos ~]# npm config list
; cli configs
metrics-registry = "https://registry.npm.taobao.org/"
scope = ""
user-agent = "npm/6.14.8 node/v12.19.0 linux x64"

; userconfig /root/.npmrc
registry = "https://registry.npm.taobao.org/"

; node bin location = /usr/local/node/node-v12.19.0/bin/node
; cwd = /root
; HOME = /root
; "npm config ls -l" to show all defaults.

npm和 cnpm区别:
npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装、卸载、管理依赖等) npm安装插件过程:从http://registry.npmjs.org下载对应的插件包(该网站服务器位于国外,所以经常下载缓慢或出现异常)。
cnpm是国内的淘宝团队分享的镜像,同步频率目前为 10分钟 一次以保证尽量与官方服务同步。
cnpm跟npm用法完全一致,只是在执行命令时将npm改为cnpm。

3、Jenkins 上NodeJS配置

"系统管理"--"全局工具设置"---" NodeJS 安装"

4、在Jenkins里添加npm配置文件,将npm源设为淘宝的

"系统管理" -> "Managed files" -> "Add a new Config" -> 选择 "Npm config file" -> 提交

在内容里面更改 registry 内容,设为 registry = https://registry.npm.taobao.org

5、如果没有安装git,需要安装git

# yum install git

6、参数化构建过程

配置分支选择

使用Extended Choice Parameter 的Single Select

7、如果将编译打包好的文件发送部署到远程服务器,配置远程的服务器

Jenkins安装 ssh发送插件 - Publish Over SSH

该插件通过ssh发布,用来将打包好的项目用ssh连接的方式发送到部署的服务器,并且执行其他的命令。

“系统管理”-->"系统配置"-->最后面找到"Publish over SSH"--> 新增 SSH-Server

8、pipeline 内容

nodejs(nodeJSInstallationName: '<Name>', configId: '<ID>') {
    sh 'npm config ls'
}
pipeline {
    stages {
        stage('Git pull') {
            steps {
                // 下载代码
                git credentialsId: '1639462c-7254-497a-b352-0fba485a0fcb', branch: "${params.BRANCH}", url: 'http://git.klvchen.com/klvchen/klvchen_vue.git'
            }
        }
        
        stage('Build') {
            steps {
                // configId 即为之前配置的npm配置文件
                nodejs(nodeJSInstallationName: 'nodejs12', configId: 'npm-config') {
                    // npm 编译安装
                    sh 'npm install && npm run build'
                }
            }
        }
    }       
}

如果不使用 npm的配置文件,也可以这样写

stage('Build') {
    nodejs("nodejs12"){// 括号里面的名字在jenkins->系统管理->全局工具配置中设定的NodeJS的别名
        sh 'npm install && npm run build'
    }
}

发送到服务器

stage('deploy') {
    sshPublisher(publishers: [sshPublisherDesc(configName: "${server_name}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/usr/local/nginx/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}

转:Jenkins自动化部署nodejs项目(前端项目)_越努力,越幸运!的技术博客_51CTO博客

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐