目前在开发的项目中,需要部署vue前端代码。但是给了它一台无法连接外网的服务器。综合考虑后,采用的解决方案是从可以连接外网的服务器上下载代码并打包,然后将打包后的dist文件夹传输到无法连接外网的服务器上。其中有几个难点以前没有做过,特记录如下。一、难点(一)如何实现两台服务器之间免密码ssh传输?(2)如何实现git的免密码ssh方式下载代码?(3)如何编写shell脚本?2.两台服务器之间免密ssh传输cd~/.sshssh-keygen-trsa//生成密钥和公钥//生成具体名称//ssh-keygen-trsa-fssh-testssh-copy-idroot@10.*.*,*//将公钥(名为id\_rsa.pub的文件)附加到身份验证文件(名为authorized\_keys的文件)//连接特定名称//ssh-copy-id-i~/.ssh/ssh-test.pubroot@172.*.*.*sshroot@10.*.*.*//测试是否连接成功//连接特定名称ssh-i~/.ssh/ssh-testroot@10.*.*.*3,gitlab的免密码ssh更新代码cd~/.sshssh-keygen-q-fwater-client-dev-P''//重新生成一堆key和公钥,公钥分配给gitlab用户的相应设置。//将公钥分配给SSH密钥。然后点击“Addkey”生成printcd代码目录eval"$(ssh-agent-s)"ssh-add~/.ssh/water-client-dev//加载key,就可以clone代码了~~4.WriteScript#!/bin/bash##desc:前端构建脚本,从已有nodejs环境的服务器构建,上传到前端服务器。因为前端服务器无法从依赖库中下载需要的依赖,所以需要通过构建机中转。#preDefine:#1.privatekeyputin~/.ssh/water-client-devforpassWordfreeWordremotelogin#2.mkdirlocalandremotebuildpath#脚本位置:与前端项目同级根目录eval"$(ssh-agent-s)">>/dev/nulssh-add~/.ssh/water-client-dev2>>/dev/nulorigin_dictionary='/DATA/water/client/ui'build_file_path="dist"generated_tar_file_name="build.tar"target_host="10.*.*.*"target_dictionary="/DATA/water/client/"target_host_user='root'branch_name=$2#进入build目录,也就是所在目录package.json位于#cdtipupdate_src(){echo"1,startupdate>>>>>>"cd$origin_dictionarygitpull#如果设置了branch参数,检查是否需要checkoutif[[-n"$分店名称”]];然后gitbranch|grep"\*$branch_name"如果[$?-ne0];然后回显“结帐新分支”;gitcheckout$branch_name;gitpull;fifi#gitcheckout。#gitpullssh-agent-k>>/dev/nulecho">>>>>>>更新完成"}build_prj(){echo"2、开始构建>>>>>>>>"echo"checkpackage.json是否更新"modified=$(find$origin_dictionary/package.json-mmin-1)if[-n"$modified"]thenecho"modified,runnpminstall"npminstall--unsafe-permif[$?-ne0]然后退出1;fielseecho"notmodified"finpmrunbuildif[$?-ne0]thenexit1;fiecho">>>>>>>buildcomplete"}update_srcbuild_prjecho"3、starttar>>>>>>>>"#removeoldandtarnewif[-e$generated_tar_file_name]thenecho"fileexist"echo"startremovefile"rm-f$generated_tar_file_nameelseecho"filenotexist"fiecho"creatingtarfile"tar-cf$generated_tar_file_name$build_file_pathecho"创建成功"echo">>>>>>>tarcomplete"echo"4、开始上传到远程服务器>>>>>>>>"#scp-i$生成的_tar_文件名root@$target_host:$target_dictionaryscp$generated_tar_file_nameroot@$target_host:$target_dictionaryecho">>>>>>>uploadcomplete"echo"5、在远程服务器上开始untar>>>>>>>>"#removeoldanduntarnewsshroot@$target_host"cd$target_dictionary;rm-rf$build_file_path;tar-xf$generated_tar_file_name;exit;"echo">>>>>>>updatecomplete"5.Jenkins自动执行脚本配置jenkins设置为,git代码已经提交,会自动执行这个脚本配置jenkins:将红框中的链接复制到gitlab中的webhook页面,gitlab调用jenkins的url触发构建。在jenkins构建页面输入调用脚本的命令后,可以直接更新代码,jenkins会自动调用脚本完成代码的更新、打包和传输~~
