jenkins编译部署nodejs项目
jenkins的搭建和编译在之前的文章中,大家可以看看
https://blog.csdn.net/losersnake/article/details/129661379
1. jenkins本机安装nodejs
下载版本包https://nodejs.org/zh-cn/download/
下载二进制包,安装简便
官网界面的版本是稳定版的18.16.0,我的系统是centos7,直接把二进制包解压之后无法使用,会报错glib版本没有找到
下面是18版本的nodejs的报错
node: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.25' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by node)
node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by node)
所以换成了16版本的
下面开始安装
su - jenkins
#######上传
cp /nas/deploy/node-v16.20.0-linux-x64.tar.xz .
tar -xvf node-v16.20.0-linux-x64.tar.xz
rm -f node-v16.20.0-linux-x64.tar.xz
mv node-v16.20.0-linux-x64/ node-v16.20.0
cd node-v16.20.0/bin/
./node -v
v16.20.0
exit
ln -s /home/jenkins/node-v16.20.0/bin/node /usr/local/bin
ln -s /home/jenkins/node-v16.20.0/bin/npm /uscr/local/bin
su - jenkins
配置淘宝镜像
切换新的镜像源
npm config set registry https://registry.npmmirror.com
然后再执行以下操作查看是否成功
npm config list
如果没安装过淘宝镜像源的,则直接安装
npm install -g cnpm --registry=https://registry.npmmirror.com
exit
ln -s /home/jenkins/node-v16.20.0/bin/cnpm /usr/local/bin
node -v
v16.20.0
npm -v
8.19.4
安装vite
npm install -g vite
2.配置全局工具配置
先安装插件
然后配置全局工具
3.配置任务
新建任务
选择自由风格
General
选择丢弃旧的构建
策略
Log Rotation
点击高级
发布包保留天数
7
发布包最大保留#个构建
7
源码管理
选择git,填写地址和用户信息
指定代码分支
构建环境
选择Provide Node & npm bin/ folder to PATH
NodeJS Installation 选择之前配置的node-v16.20.0 其余选项默认
Build Steps
先添加执行shell
命令
node -v
npm -v
cnpm i
cnpm run build:test
rm -f dist.zip
zip -q -r dist.zip dist
Build Steps中再添加Send files or execute commands over SSH
SSH Publishers
SSH Server
Name
localhost
Transfers
Transfer Set
Source files
dist.zip
Remote directory
/nas/yggq/web/testpackage
Build Steps中再添加Invoke Ansible Playbook
Ansible installation
ansible2.9.27
Playbook path
/home/jenkins/playbook/dist.yml
Inventory
Inline content
Content
[host]
192.168.79.79
Credentials
填写密码
点击高级
Number of parallel processes
1
勾选Disable the host SSH key check
勾选Unbuffered stdout
Extra Variables
Key
nasdir
Value
/nas/%%%%%%%/testpackage
保存
4.编写ansible
su - jenkins
vi /home/jenkins/playbook/dist.yml
---
- name: "本机备份dist包"
hosts: localhost
tasks:
- name: "获取本机当前时间"
debug:
msg: "{{ hostvars['localhost']['ansible_date_time']['iso8601_basic_short'] }}"
register: localdate
- name: "查看版本包状态"
stat:
path: "{{ nasdir }}/dist.zip"
register: diststat
- debug:
var: diststat.stat.exists
- name: "创建备份目录"
file:
path: "{{ nasdir }}/{{ localdate.msg }}"
state: directory
mode: 0777
- name: "转移dist包到备份文件夹"
copy:
src: "{{ nasdir }}/dist.zip"
dest: "{{ nasdir }}/{{ localdate.msg }}"
- file:
path: "{{ nasdir }}/dist.zip"
state: absent
- name: "更新版本信息"
lineinfile:
dest: "{{ nasdir }}/info.txt"
regexp: "^previous version:"
state: absent
create: yes
- replace:
path: "{{ nasdir }}/info.txt"
regexp: "lastest version"
replace: "previous version"
- lineinfile:
path: "{{ nasdir }}/info.txt"
regexp: 'add a line!'
line: "lastest version: {{ localdate.msg }}"
backrefs: no
- name: "dist自动上版"
hosts: host
gather_facts: false
serial: 1
tasks:
- name: "获取本机当前时间"
debug:
msg: "{{ hostvars['localhost']['ansible_date_time']['iso8601_basic_short'] }}"
register: localdate
- name: "查看版本包状态"
stat:
path: "{{ nasdir }}/{{ localdate.msg }}/dist.zip"
register: diststat
- debug:
var: diststat.stat.exists
- name: "删除旧版本包"
file:
path: "/home/nginx/dist"
state: absent
- name: "拷贝并解压版本包"
unarchive:
src: "{{ nasdir }}/{{ localdate.msg }}/dist.zip"
dest: "/home/nginx"
copy: no
- name: "重启nginx"
service:
name: nginx
state: restarted
5.执行任务
查看控制台输出
Started by user admin
Running as SYSTEM
Building in workspace %%%%%%%
The recommended git tool is: NONE
using credential f06c2e32-%%%%%%%
> /home/jenkins/git-2.40.0/bin/git rev-parse --resolve-git-dir /home/jenkins/jenkins_data/workspace/%%%%%%%/.git # timeout=10
Fetching changes from the remote Git repository
%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
> /home/jenkins/git-2.40.0/bin/git rev-list --no-walk e5c32b1bab254c8fece4dba743e52f68956dcb92 # timeout=10
[%%%%%%%] $ /bin/sh -xe /tmp/jenkins9411525704591339550.sh
+ node -v
v16.20.0
+ npm -v
8.19.4
+ cnpm i
reusing global emitter
✔ Installed 32 packages on /home/jenkins/jenkins_data/workspace/%%%%%%%
✔ All packages installed (used 39ms(network 37ms), speed 0B/s, json 0(0B), tarball 0B, manifests cache hit 0, etag hit 0 / miss 0)
+ cnpm run build:test
> %%%%%%%@0.0.0 build:test
> vite build --mode development
vite v4.3.5 building for development...
transforming...
2202 modules transformed.
rendering chunks...
%%%%%%%%%%%%%%
computing gzip size...
Some chunks are larger than 500 kBs after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
✓ built in 23.52s[39m
+ rm -f dist.zip
+ zip -q -r dist.zip dist
SSH: Connecting from host [%%%%%%%]
SSH: Connecting with configuration [localhost] ...
SSH: Disconnecting configuration [localhost] ...
SSH: Transferred 1 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
[%%%%%%%] $ sshpass ******** /bin/ansible-playbook /home/jenkins/playbook/dist.yml -i /tmp/inventory14329446576719795213.ini -f 1 -u root -k -e nasdir=%%%%%%%
PLAY [本机备份dist包] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [获取本机当前时间] ****************************************************************
ok: [localhost] => {
"msg": "20230509T105223"
}
TASK [查看版本包状态] *****************************************************************
ok: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"diststat.stat.exists": true
}
TASK [创建备份目录] ******************************************************************
changed: [localhost]
TASK [转移dist包到备份文件夹] ***********************************************************
changed: [localhost]
TASK [file] ********************************************************************
changed: [localhost]
TASK [更新版本信息] ******************************************************************
changed: [localhost]
TASK [replace] *****************************************************************
changed: [localhost]
TASK [lineinfile] **************************************************************
changed: [localhost]
PLAY [dist自动上版] ****************************************************************
TASK [获取本机当前时间] ****************************************************************
ok: [192.168.79.79] => {
"msg": "20230509T105223"
}
TASK [查看版本包状态] *****************************************************************
ok: [192.168.79.79]
TASK [debug] *******************************************************************
ok: [192.168.79.79] => {
"diststat.stat.exists": true
}
TASK [删除旧版本包] ******************************************************************
changed: [192.168.79.79]
TASK [拷贝并解压版本包] ****************************************************************
changed: [192.168.79.79]
TASK [重启nginx] *****************************************************************
changed: [192.168.79.79]
PLAY RECAP *********************************************************************
192.168.79.79 : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=10 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finished: SUCCESS