一、构建你的第一个自动部署流程
因为我们的代码是存放到github上的,大致的流程就是,首先我们需要将代码从github上面拉下来,然后使用pnpm打包工具,将前端项目打包成静态文件,然后通过SSH将生成的静态文件传送到指定的服务器上,使用nginx来代理。
1.1、创建item
Jenkins拉取git代码以来jenkins中git的插件,打包遍历依赖Nodejs插件,传输文件依赖SSH插件
1.2、源文件管理

如果使用的是公开的仓库,就不用选择下面的credentials,如果是私有的仓库的话,就需要添加credential,(添加Credentials界面如下)
如果不知道如何生成github令牌,可以参考:github访问令牌token的创建方法
1.3、配置运行环境

配置NodeJS
1.4、配置执行shell


shell
echo $WORKSPACE
node -v
npm -v
npm install -g pnpm
pnpm -v
pnpm config set registry https://registry.npmmirror.com
pnpm i
pnpm build
二、遇到的问题
2.1 配置Github的链接,代码拉不下来
这一种拉不下来是因为网络的问题,在大陆访问github,使用过的人都知道。
这里我在Jenkins服务器上面生成公钥和私钥,把公钥配置到github上面解决的。
2.2、拉取私有的Github仓库代码
当时一直没有配置对,就是没有整明白credentials这个,使用Username Password要使用github生成的令牌,因为密码github在很久之前就放弃使用了。
2.3、容器中jenkins用户没有创建目录的权限问题
当时以为拉不下来是因为jenkins没有创建目录的权限,但不是因为这个。
2.4、Github公开仓库都拉取不到
shell
fatal: unable to access 'https://github.com/SmaxxxengC/bxxng.git/': Failed to connect to github.com port 443 after 130257 ms: Couldn't connect to server
我是用gitee的就能拉取到,还是因为在国内拉github代码的网络问题。
2.5、都配置好了,但是还是会有报错
shell
error: RPC failed; curl 92 HTTP/2 stream 11 was not closed cleanly: CANCEL (err 8)
error: 30402 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2852)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2188)
at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:638)
at PluginClassLoader for git//hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:997)
... 12 more
ERROR: Error fetching remote repo 'origin'
Finished: FAILURE
今天早上重新拉取,不知道为什么就可以拉了,应该是当时网络的问题。
2.6、将生成的文件推到指定服务器
实现:将构建后的文件推送到指定服务器上,需要哪些配置
参考:Jenkins拉取代码构建完成后部署到其他服务器(详解)
如果出现传输0个文件的问题,参考:解决SSH: Transferred 0 file(s)