node项目构建
1.把npm变成cnpm
2.使用cnpm进行构建打包
sh "cd /var/lib/jenkins/workspace/sdc-h5"
sh "cnpm config set strict-ssl false"
sh "cnpm install node-sass --unsafe-perm --save-dev"
sh "cnpm install"
sh "cnpm run build:sit"
ansible安装配置
1.配置免密登录 在ansible主机上使用普通用户创建公私钥即可,不一定要使用管理员用户
1.[jenkins@localhost ~]$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
2.[jenkins@localhost ~]$ ssh-copy-id -i .ssh/id_rsa.pub admin@192.168.30.98
3.ansible建立ssh免密的时候必须选用合适的用户 服务端主机上和客户端主机上必须存在同样的用户才行
ansible主机上存在jenkins普通用户 但是远程主机上没有jenkins用户,执行ansible命令的时候是以jenkins用户远程连接的 所以会出现连接异常
4.在ansible主机上从jenkins用户切换到admin用户,然后重新生成公私密钥即可
5.在执行ansible命令的时候指定远程连接用户
ansible webservers --user=admin -m ping
发布项目流程
ansible webservers --user=admin -m copy -a "src=/var/lib/jenkins/workspace/sdc-h5/dist/* dest=/home/admin/testh5/"
copy模块用于将本地或远程机器上的文件拷贝到远程主机上
拷贝前备份 backup=yes
src是目录时
源目录以/结尾,只拷贝了目录下的内容
源目录未以/结尾,直接将src目录本身拷贝到目的地
src和dest都是文件,若dest的文件的父目录不存在将报错
Pipeline依次执行指令
1.获取命令执行是否成功 excuteCode = sh(script: "<shell command>", returnStatus: true)
2.获取命令执行的输出内容 result = sh(script: "<shell command>", returnStdout: true)
stage("Deploy"){ steps { script { sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/register-center/target/register-center.jar dest=/home/admin/app/'" sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/config-center/target/config-center.jar dest=/home/admin/app/'" sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/gateway-zuul/target/gateway-zuul.jar dest=/home/admin/app/'" sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/admin/target/admin.jar dest=/home/admin/app/'" sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/bizapp/sdc-collect-config/target/sdc-collect-config-0.0.1-SNAPSHOT.jar dest=/home/admin/app/'" sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/bizapp/sdc-rule-config/target/sdc-rule-config-1.0-SNAPSHOT.jar dest=/home/admin/app/'" sh "ansible webservers --user=admin -m copy -a 'src=/var/lib/jenkins/workspace/AppServiceProject/bizapp/sdc-es-service/target/sdc-es-service.jar dest=/home/admin/app/'" } } post { success { print("Deploy success......") } } } stage("Start"){ steps { script { //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-register.sh'" res = sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-register.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("register服务启动失败,本次发布流程终止") } //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-config.sh'" res = sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-config.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("config服务启动失败,本次发布流程终止") } //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-gateway.sh'" res = sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-gateway.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("gateway服务启动失败,本次发布流程终止") } //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-admin.sh'" res = sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-admin.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("admin服务启动失败,本次发布流程终止") } //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-sdccollectconfig.sh'" res=sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-sdccollectconfig.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("sdc-collect-config服务启动失败,本次发布流程终止") } //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-sdcruleconfig.sh'" res=sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-sdcruleconfig.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("sdc-rule-config服务启动失败,本次发布流程终止") } //sh "ansible webservers --user=admin -m shell -a '/home/admin/app/start-es.sh'" res=sh(script: "ansible webservers --user=admin -m shell -a '/home/admin/app/start-es.sh'", returnStdout: true) if (res.contains("Start Fail")){ error("es服务启动失败,本次发布流程终止") } } } post { success { print("所有应用启动成功,本次流水线执行成功") } } }
如果其中一条命令执行失败,后面的指令就会中断执行.
错误排查
1. name cannot start with an underscore; name can only contain URL-friendly characters
原因分析:通过cnpm命令安装模块时,会同时生成带下划线的文件(eg: _jquery@3.4.1@jquery),不符合命名规范(根据报错提示)
1.安装模块时用npm,非cnpm
2.先通过npm init初始化目录,生成包管理文件package.json,再安装模块(npm/cnpm)
2.如何指定`package.json`的npm的路径
cnpm run build:sit -config=/var/lib/jenkins/workspace/netcehui-h52/h5-new/package.json
打包成功发布