最近有需求需要Jenkins实现发布NPM新包,这里记录下配置方法。

  • 执行release命令,生成新版本号及changelog记录
  • 提交修改后的package.json,changelog到GitLab仓库
  • 打包新版本到私服Nexus

为实现该功能,需要确保安装以下3个插件

node() {

nodejs(nodeJSInstallationName: "nodejs_v10.16.0", configId: "96de5baa-02b1-4cb9-9f65-e8f96452b59c") {

stage("checkout") {
checkout([$class: 'GitSCM', branches: [
[name: 'master']
], userRemoteConfigs: [
[credentialsId: 'gitlab', url: 'git@gitlab.1991421.cn:personal/hello-web.git']
]])
}


stage("release lib") {
sshagent( ['gitlab']) {
sh '''
git config --global user.email "alan@1991421.cn"
git config --global user.name "Alan's 2st Bot"
npm install -g yarn
yarn install
npm run release
git push origin HEAD:refs/heads/master --tags
'''
}
}

}

}

文件下载

  • nodeJSInstallationName为Global Tool Configuration-NodeJS中指定版本name
  • nodejs configId为Config File Management中npmrc文件ID
  • userRemoteConfigs credentialsId为Credentials中Git仓库私有令牌ID
  • push到上游时增加--tags有两个原因
  • release命令的背后是用standard-version进行版本管理,生成ChangeLog时需要基于Tag,否则出现ChangeLog中重复显示commit问题
  • Tag充当版本快照,有益于版本管理,和问题修复
  • 这里未考虑npm包缓存化,建议有时间了,优化该点,否则每次重新安装第三方稳定版本包,浪费资源
  • 一直觉得,Jenkins文档,插件都是参差不齐,但同时因为Jenkins确实设计的合理巧妙,依然也算是CI/CD的主流工具,真是哭笑不得。实际实现上述需求时,还是踩了些坑,因此这里Mark下,留作备忘
  • 如上配置后,对于包的版本管理相当于交付给了Jenkins来自动化,对于开发来说只需要按照规范进行提交feat,fix等即可

--

--

--

Develop with pleasure!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alan He

Alan He

Develop with pleasure!

More from Medium

Rooting Gryphon Routers via Shared VPN

In ABAP Make an API HTTP Request. / ABAP HTTP Request

How To Install Go Language in Kali Linux for Bug Bounty Hunting

Introduction To Docker — Part 1