Jenkins服务器搭建及各种基础插件安装在此先跳过了。本次用的Jenkins 2.387.2,JDK11,node用的是Jenkins中Global Tool Configuration配的。
会遇到的问题,调试中印象较深的记录下来了:
1.插件build user vars plugin,Pipeline NPM Integration Plugin其中:
Full name
:全名BUILD_USER_FIRST_NAME
:名字BUILD_USER_LAST_NAME
:姓BUILD_USER_ID
:Jenkins用户IDBUILD_USER_EMAIL
:用户邮箱
2.npm config 解决npm未授权问题
npm config list --l 或 npm config ls -l #查看全部配置项
编辑usrconfig 那一行带.npmrc 文件,清空内容或者手动配置进去
3.有的会遇到Jenkins与git公钥私钥都配置好了,但是代码push不上去,在日志中看下第一行的Jenkins用户,点击下拉选配下credentials,访问git的
直接上pipeline代码,没有优化,能用。
workspace='/root/.jenkins/workspace/vvp'//jenkins中创建流水项的工作目录
pipeline {
agent any
tools{
nodejs 'Node14'//如果是用Jenkins服务器上的自己安装的node,就不用这个tools;如果是在Jenkins工具环境变量中配置的多个node,需要在此指定
}
stages {
stage("clean workspace"){
steps{
script{
/* clean up our workspace */
dir("${workspace}") {
deleteDir()}
/* clean up tmp directory */
dir("${workspace}@tmp") {
deleteDir()}
/* clean up script directory */
dir("${workspace}@script") {
deleteDir()}
}
}
}
stage("git branch ") {
steps {
script {
git branch:'dev', credentialsId:'Jenkins中配置的账号密码ID', url: 'http://gitlab.****/*****/PROJECT.git';
}
}
}
stage("Update Version&Git Push") {
steps {
script {
wrap([$class: 'BuildUser']) {
withCredentials([usernamePassword(credentialsId:"jenkins中配置访问git的账号密码ID",usernameVariable: "GIT_USERNAME", passwordVariable: "GIT_PASSWORD")]) {
sh 'git config --local credential.helper "!p() { echo username=\\$GIT_USERNAME; echo password=\\$GIT_PASSWORD; }; p"'
sh 'git config --global user.name "${BUILD_USER_ID}"'
sh 'git config --global user.email "${BUILD_USER_EMAIL}"'
sh 'git clone http://gitlab.****/*****/PROJECT.git'
sh 'git config --global credential.helper store'
// 将文件推送到 git 远程仓库
//sh 'npm install'
sh 'git add .'
//sh 'git commit -m "`date +%Y%m%d%H%M%S`更新版本版本啦"'
sh 'echo===='
// sh 'git status -s'
// sh 'npm version prerelease --preid beta' //更新测试版带beta的tag,1.0.0->1.0.0-beta.0
sh'npm version patch' //更新最终版1.0.0->1.0.1
sh 'echo===='
//sh 'npm publish'
sh 'git push -u origin dev'
} }
}
}
}
stage("NEW Git clone ") {
steps {
script {
git branch:'dev', credentialsId:'jenkins中配置访问git的账号密码ID', url: 'http://gitlab.****/*****/PROJECT.git';
}
}
}
stage("Build & Publish Nexus") {
steps {
script {
sh """
node -v
#cat $PATH
npm config set registry http://nexus.*****.cn/repository/npm-group/
#查看全局安装包
#npm list -g --depth=0
#npm cache clean --force
#npm install
npm i
npm install npm-login-cmd
export NPM_USER=nexus账号
export NPM_PASS=nexus密码
export NPM_EMAIL=nexus账号邮箱
npm config set registry http://nexus.*****.cn/repository/npm-hosted/
npx npm-login-cmd
npm config set cache-min 9999999
npm publish --registry=http://nexus.*****.cn/repository/npm-hosted/
"""
}
}
}
}
}