Centos7.x内网环境Jenkins前端打包环境配置
参考地址:
https://www.cnblogs.com/guangdelw/p/18763336
https://2048.csdn.net/682c1be8606a8318e857d687.html
前言:环境描述和目标
最近公司新接了一个项目,要求是:需要再桌面云中进行内网开发 ,所有的开发桌面云没办法连接公网,但是内网搭建了nexus私服,java 和 npm 的应用可以通过nexus私服来下载依赖。
我们本地的文件可以复制到桌面云内部,但是桌面云中的文件无法复制出来。
目标:
- 需要将本地的代码上传到对方提供的gitlab私服
- 安装Jenkins到Centos服务器中
- 添加前后端的打包配置到Jenkins中
代码迁移的过程就不描述了,这里着重描述安装Jenkins 和 添加配置。
一、安装Jenkins
Jenkins的国内源:
以下是一些常用的国内 Jenkins 插件更新源地址:
- 清华大学:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
- 华为开源镜像站:https://mirrors.huaweicloud.com/jenkins/updates/update-center.json
- 腾讯云:https://mirrors.cloud.tencent.com/jenkins/updates/update-center.json
- 中国科学技术大学:https://mirrors.ustc.edu.cn/jenkins/updates/update-center.json
- 北京理工大学:https://mirror.bit.edu.cn/jenkins/updates/update-center.json
1.1、前置思考:
由于公司的后端代码使用的JDK版本为1.8,所以为了统一jdk版本,所以最初选定使用JDK8的最后一个版本:Jenkins-2.346.3;
但是再实际操作的时候发现:Jenkins在线安装插件的时候,并不会将当前Jenkins的版本传过去获取适合当前版本的插件,而是会获取当前最新版本,基于这种情况最终选定安装比较LTS新版本的2.479.3 (注意这里使用的是JDK17)
而安装的过程区分为两种方式:
- 再一个可联网的机器上安装插件,然后将插件和配置目录打包一起迁移到服务器中(默认的目录为:当前用户主目录下的**.jenkins**文件夹下)
- 只是将 jpi插件 copy到桌面云中,并通过web界面一个个上传上去
相比之下第一种操作方式更简单,所以最终选用第一种方式。
但是当前时间(2025-05-27)前后国内的插件源没办法使用,找不到对应的 update-center.json 文件,所以吭哧了好久最终使用魔法直接连接插件官网直接进行下载的。
1.2、安装的过程
操作步骤如下:
- 下载 jenkins.war 文件
- 通过 java -jar jenkins.war 命令进行启动
- 然后配置用户名和密码和选择插件下载即可。
- 下载完毕后,直接将jenkins配置主目录(当前用户主目录下的**.jenkins**文件夹下)和 jenkins.war 文件打包上传到桌面云服务器中
- 然后解压,将其 .jenkins 目录同样放到当前用户主目录下,然后启动即可
二、前端环境的配置
2.1、nodejs环境的安装和配置
2.1.1、前置思考:
由于我们使用服务器为:Centos7.9,而项目依赖的nodejs版本要求为:16.15.1, pnpm的安装要求为:大于18,基于安装LTS版本的要求,最终选定安装:18.20.8或者20.19.2版本。
而官网提供 18.20.8 或者 20.19.2 版本的nodejs再Centos7.9通过tar包安装配置后,再执行:node -v 的过程出现类似一下的错误:
bash
$ node -v
/lib64/libm.so.6: version `GLIBC_2.27' not found (required by ./node)
/lib64/libc.so.6: version `GLIBC_2.25' not found (required by ./node)
/lib64/libc.so.6: version `GLIBC_2.28' not found (required by ./node)
/lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ./node)
/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./node)
/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./node)
linux-vdso.so.1 => (0x00007ffca6bd4000)
libdl.so.2 => /lib64/libdl.so.2 (0x00002b574ed87000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00002b574ef8b000)
libm.so.6 => /lib64/libm.so.6 (0x00002b574f293000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002b574f595000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b574f7ab000)
libc.so.6 => /lib64/libc.so.6 (0x00002b574f9c7000)
/lib64/ld-linux-x86-64.so.2 (0x00002b574eb63000)
大概的原因是:
而CentOS 7.x等操作系统自带的glibc版本为2.17,Node.js v18.x或更高编译的环境所需glibc≥2.28,所以这里有三种方案:
- 升级本服务器的glibc,但是各个应用之间会依赖低版本的,强制升级会导致其他应用服务异常(不推荐)
- 通过Linux的scl计划,再CentOS 7.x安装各种开发工具集,重新编译(不推荐)
- 通过nodejs官网旗下的unofficial-builds子计划,来下载对方提供好的编译版。地址为:https://unofficial-builds.nodejs.org/download/release/
再unofficial-builds提供的nodejs中, 18.20.8 版本再实际的使用过程中发现还是会有报错的情况,所以最终选择了 20.19.2,下载的地址为:
2.1.2、nodejs的安装
具体的操作如下:
- 下载 node-v20.19.2-linux-x64-glibc-217.tar.xz 文件并上传到桌面云服务器中
- 通过
tar -Jxf node-v20.19.2-linux-x64-glibc-217.tar.xz
对文件进行解压缩 - 配置环境变量,将当前目录配置到 PATH 中,然后重新加载配置文件:
. /etc/profile
- npm配置使用nexus的私服,执行操作:
npm config set registry https://nexus.sunwoda.com/repository/npm-group
2.2、项目的编译
2.2.1、项目中的配置文件
json
{
// 省略部分配置
"devDependencies": {
"@ant-design/colors": "^6.0.0",
"@commitlint/cli": "^17.6.6",
"@commitlint/config-conventional": "^17.6.6",
"@iconify/json": "^2.2.87",
"@purge-icons/generated": "^0.9.0",
"@types/codemirror": "^5.60.8",
"@types/crypto-js": "^4.1.1",
"@types/intro.js": "^5.1.1",
"@types/lodash-es": "^4.17.7",
"@types/mockjs": "^1.0.7",
"@types/nprogress": "^0.2.0",
"@types/qrcode": "^1.5.1",
"@types/qs": "^6.9.7",
"@types/showdown": "^2.0.1",
"@types/sortablejs": "^1.15.1",
"@dcore/eslint-config": "workspace:*",
"@dcore/stylelint-config": "workspace:*",
"@dcore/ts-config": "workspace:*",
"@dcore/types": "workspace:*",
"@dcore/vite-config": "workspace:*",
"@vue/compiler-sfc": "^3.3.4",
"@vue/test-utils": "^2.4.0",
"cross-env": "^7.0.3",
"cz-git": "^1.6.1",
"czg": "^1.6.1",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-define-config": "^1.1.1",
"eslint-plugin-jest": "^25.2.2",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^8.6.0",
"husky": "^8.0.3",
"lint-staged": "13.2.3",
"prettier": "^2.8.8",
"prettier-plugin-packagejson": "^2.4.4",
"rimraf": "^5.0.1",
"turbo": "^1.10.7", // 这里是个关键,后面考试要考
"typescript": "^5.1.6",
"unbuild": "^1.2.1",
"vite": "^4.4.0",
"vite-plugin-mock": "^2.9.6",
"vue-tsc": "^1.8.4"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=16.15.1",
"pnpm": ">=8.1.0"
}
}
2.2.2、配置、编译和打包
bash
# npm 先配置registry私服地址,注意私服地址结尾不要添加"/"否则后期会报错
$ npm config set registry https://nexus.sunwoda.com/repository/npm-group
# 先安装 pnpm 命令
$ npm install [email protected] -g
# 安装 turbo 命令
$ npm install [email protected] -g
# pnpm 命令也配置registry私服地址
$ pnpm config set registry https://nexus.sunwoda.com/repository/npm-group
# 进入项目工程,先拉取依赖
$ pnpm install
# 打包操作
$ pnpm run build
2.2.3、操作过程中的异常情况
2.2.3.1、pnpm install
出现error (ERR_INVALID_THIS)的情况
具体的报错内容如下:
BASH
$ pnpm install
Scope: all 7 workspace projects
internal/eslint-config | WARN deprecated [email protected]
WARN deprecated [email protected]: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
WARN deprecated @iconify/[email protected]: no longer maintained, switch to modern iconify-icon web component
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/debug error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/graphemer error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/ignore error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/natural-compare-lite error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/semver error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/ts-api-utils error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/@eslint%2Feslintrc error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
WARN GET https://nexus.sunwoda.com/repository/npm-ptl-hosted/@eslint%2Fjs error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.
在 nodejs20 版本中,就会出现这个问题,为了解决这个问题,你只需要升级到 pnpm v8.3.1 或更高版本即可。
参考地址:
https://blog.csdn.net/qq_19661477/article/details/134234971
2.2.3.2、pnpm install
出现turbo run stub,run failed: error hashing package files
具体的报错内容如下:
bash
$ pnpm install
. postinstall$ turbo run stub
│ ERROR run failed: error hashing package files: git error: 'git status' in /root/abc/limsui/packages/hooks exited with code 129 stderr: error: unknown option `no-renames'
│ usage: git status [options] [--] <pathspec>...
│ -v, --verbose be verbose
│ -s, --short show status concisely
│ -b, --branch show branch information
│ --porcelain machine-readable output
│ --long show status in long format (default)
│ -z, --null terminate entries with NUL
│ -u, --untracked-files[=<mode>]
│ show untracked files, optional modes: all, normal, no. (Default: all)
│ --ignored show ignored files
│ --ignore-submodules[=<when>]
│ ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)
│ --column[=<style>] list untracked files in columns
这个是 turbo v1.10.7 版本的bug,然后升级package.json中turbo的版本到新版本即可,例如: turbo v1.10.8,
参考地址:
https://github.com/vercel/turborepo/issues/5447#issuecomment-1621908600
扩展操作
1、将依赖上传到nexus私服中
bash
# 本地npm环境添加镜像
$ npm config set registry https://registry.npmmirror.com
# 安装 pnpm
$ npm i [email protected] -g
# 安装 get-npm-tgz
$ npm i get-npm-tgz -g
# 进入到项目目录中,然后下载依赖
$ pnpm install
# 然后将 pnpm-lock.yaml 转换为:package-lock.json
$ npx pnpm-lock-to-npm-lock ./pnpm-lock.yaml
# 这个命令会把当前文件夹的 package-lock.json 的依赖tgz格式下载下来, 执行成功后所有的依赖会再当前目录的 tgz 文件夹下
$ npx get-npm-tgz
# 然后将tgz文件夹压缩为xz文件,然后上传到桌面云中
$ tar -Jcf tgz.tar.xz tgz
#======= 以下内容再桌面云中执行 =======
# 先解压缩
$ tar -Jxf tgz.tar.xz
# 先设置私服的url并进行登录
$ npm login --registry http://registry.npmjs.com
# 发布 xxxx.tgz 文件
$ npm publish xxxx.tgz
批量的发布脚本
BA
#!/bin/bash
REPOSITORY=https://nexus.sunwoda.com/repository/npm-ptl-hosted
PACKAGES_PATH=./tgz
npm login --registry=$REPOSITORY
for package in $PACKAGES_PATH/*.tgz; do
npm publish --registry=$REPOSITORY $package
done
参考地址:
https://blog.csdn.net/2401_85743969/article/details/141128702