方式一:通過(guò)增加構(gòu)建步驟
- 增加構(gòu)建步驟
- 選擇目標(biāo)服務(wù)器
- 配置用戶(hù)名、密碼
方式二:shell 命令
使用上面的方式一,在選擇目標(biāo)服務(wù)器的時(shí)候,需要管理員提前配置好服務(wù)器列表丽猬,參考官網(wǎng)
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.
很遺憾,當(dāng)你不是管理員熏瞄,或者不太方便聯(lián)系管理員時(shí)脚祟,可以選擇使用 ssh 密鑰對(duì)
的方式進(jìn)行處理:
- 在 Jenkins 上執(zhí)行
ssh-keygen -t rsa -b 2048 -P "" -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
將上面得到的公鑰復(fù)制到目標(biāo)服務(wù)器上,如果選擇用目標(biāo)服務(wù)器的
root
賬戶(hù)强饮,則需要復(fù)制到/root/.ssh/authorized_keys
文件中(新起一行)打開(kāi)本地(不是 Jenkins)的
~/.ssh/known_hosts
文件由桌,在本地模擬執(zhí)行命令:
scp test.png root@192.168.xxx.xxx:/tmp
此時(shí)會(huì)問(wèn)你是否接收連接:
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
后回車(chē),則表示允許邮丰,此時(shí)會(huì)在 known_hosts 新增目標(biāo)服務(wù)器的信息:
192.168.xxx.xxx ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpLHd
把上面新增的信息復(fù)制到 Jenkins 中:
echo "192.168.xxx.xxx ssh-ed25519 AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpLHd">>~/.ssh/known_hosts
此時(shí)行您,已經(jīng)完成 Jenkins 和目標(biāo)服務(wù)器的密鑰對(duì)配置,后續(xù)直接使用 scp/ssh 相關(guān)命令即可
完整命令:
#rm -rf ~/.ssh/id_rsa
#rm -ef ~/.ssh/id_rsa.pub
#ssh-keygen -t rsa -b 2048 -P "" -f ~/.ssh/id_rsa
#復(fù)制到目標(biāo)服務(wù)器 /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/"