下面给你一套从零开始的实操流程,讲清楚 Jenkins 怎么连接 Ubuntu 并部署项目。适用于大多数场景(Java / Node / Python 项目都类似)。
Jenkins(控制端)
↓ SSH
Ubuntu 服务器(目标机)
↓ 执行脚本
拉代码 → 构建 → 部署 → 启动服务
两种方式常见:
下面以 SSH 方式 为主。
sudo apt update
sudo apt install -y openjdk-11-jdk
sudo apt install -y git
sudo adduser deploy
sudo usermod -aG sudo deploy
sudo apt install -y openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh
查看 IP:
ip a
sudo apt update
sudo apt install -y openjdk-11-jdk
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install -y jenkins
访问:
http://<jenkins-ip>:8080
Manage Jenkins → Plugins
搜索安装:
✅ Publish Over SSH
Manage Jenkins
→ System
→ Publish over SSH
填写:
测试连接 ✅
New Item → Freestyle project
Git
Repository URL: git@github.com:xxx/xxx.git
Credentials: 配置 Git 账号
Send files or execute commands over SSH
示例(部署 Spring Boot):
cd /home/deploy/app
git pull
mvn clean package
ps -ef | grep app.jar | grep -v grep | awk '{print $2}' | xargs kill
nohup java -jar target/app.jar > app.log 2>&1 &
cd /home/deploy/web
git pull
npm install
npm run build
pm2 restart app || pm2 start npm --name app -- run start
sudo ufw allow 22chmod 600 ~/.ssh/id_rsasudo visudo
deploy ALL=(ALL) NOPASSWD:ALL
lsof -i:8080
如果你愿意,可以告诉我:
我可以直接给你 一份可复制的 Jenkinsfile 或部署脚本。