Jenkins 编译完后通过 SSH 上传到服务器

2024-01-09  本文已影响0人  雁过留声_泪落无痕

方式一:通过增加构建步骤

  1. 增加构建步骤
增加构建步骤.png
  1. 选择目标服务器
选择目标服务器.png
  1. 配置用户名、密码

方式二:shell 命令

使用上面的方式一,在选择目标服务器的时候,需要管理员提前配置好服务器列表,参考官网

Global Configuration

From the Jenkins home page, click "Manage Jenkins" and then click on "Configure System" and find the SSH section. It allows you to configure hosts that are later available in your builds.

很遗憾,当你不是管理员,或者不太方便联系管理员时,可以选择使用 ssh 密钥对 的方式进行处理:

  1. 在 Jenkins 上执行
ssh-keygen -t rsa -b 2048 -P "" -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
  1. 将上面得到的公钥复制到目标服务器上,如果选择用目标服务器的 root 账户,则需要复制到 /root/.ssh/authorized_keys 文件中(新起一行)

  2. 打开本地(不是 Jenkins)的 ~/.ssh/known_hosts 文件,在本地模拟执行命令:

scp test.png root@192.168.xxx.xxx:/tmp

此时会问你是否接收连接:

ED25519 key fingerprint is SHA256:AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpLHd. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])?

输入 yes 后回车,则表示允许,此时会在 known_hosts 新增目标服务器的信息:

192.168.xxx.xxx ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpLHd

把上面新增的信息复制到 Jenkins 中:

echo "192.168.xxx.xxx ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpLHd">>~/.ssh/known_hosts

此时,已经完成 Jenkins 和目标服务器的密钥对配置,后续直接使用 scp/ssh 相关命令即可

完整命令:

#rm -rf ~/.ssh/id_rsa
#rm -ef ~/.ssh/id_rsa.pub
#ssh-keygen -t rsa -b 2048 -P "" -f ~/.ssh/id_rsa
#复制到目标服务器 /root/.ssh/authorized_keys 文件中
#cat ~/.ssh/id_rsa.pub
#echo "192.168.xxx.xxx ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpLHd">>~/.ssh/known_hosts

npm run build
rm -rf xxx.zip
zip -r xxx.zip xxx
scp xxx.zip root@192.168.xxx.xxx:/data/
ssh root@192.168.xxx.xxx "cd data; rm -rf /data/html/xxx; unzip xxx.zip -d /data/html/"
上一篇 下一篇

猜你喜欢

热点阅读