Jenkins、Ansible 和 Git 的自动化部署教程

1. 环境准备

确保你的系统具有互联网访问权限,并且是基于 CentOS 7/8 或 Ubuntu 18.04/20.04 等常用发行版。你需要具有 sudo 权限的账户来完成以下步骤。


一、Git 安装与配置

1.1 安装 Git

在 CentOS 系统上:

sudo yum install git -y

在 Ubuntu 系统上:

sudo apt update sudo apt install git -y

1.2 配置 Git 用户信息

git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

验证配置:

git config --list


二、Jenkins 安装与配置

2.1 安装 Java(Jenkins 依赖)

在 CentOS 系统上:

sudo yum install java-11-openjdk-devel -y

在 Ubuntu 系统上:

sudo apt install openjdk-11-jdk -y

验证 Java 版本:

source /etc/profile

java -version

2.2 安装 Jenkins

在 CentOS 上:

sudo yum install epel-release -y sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key sudo yum install jenkins -y

在 Ubuntu 上:

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 jenkins -y

2.3 启动 Jenkins 服务

sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins

2.4 打开 Jenkins

Jenkins 默认运行在端口 8080,使用浏览器访问 http://your_server_ip:8080,第一次登录时需要输入初始管理员密码。

获取初始密码:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

2.5 安装推荐插件与创建管理员用户

根据向导完成安装推荐的插件,并创建管理员账户。


三、Ansible 安装与配置

3.1 安装 Ansible

在 CentOS 上:

sudo yum install epel-release -y sudo yum install ansible -y

在 Ubuntu 上:

sudo apt update sudo apt install software-properties-common -y sudo apt-add-repository --yes --update ppa:ansible/ansible sudo apt install ansible -y

3.2 配置 Ansible 主机文件

修改 /etc/ansible/hosts 文件,添加你需要管理的服务器:

sudo nano /etc/ansible/hosts

示例:

[web] webserver.example.com [db] dbserver.example.com

保存后可以测试连接:

ansible all -m ping


四、集成 Jenkins 与 Ansible

4.1 在 Jenkins 中安装 Ansible 插件
  1. 在 Jenkins 的仪表盘中,点击 "Manage Jenkins" -> "Manage Plugins"。
  2. 搜索 Ansible 并安装 "Ansible Plugin"。
  3. 重启 Jenkins。
4.2 在 Jenkins 中配置 Ansible
  1. 在 Jenkins 中,点击 "Manage Jenkins" -> "Global Tool Configuration"。
  2. 滚动到 "Ansible" 部分,点击 "Add Ansible"。
  3. 填写 Ansible 安装路径(例如 /usr/bin/ansible)。
4.3 创建 Jenkins Pipeline 与 Ansible 集成

创建一个 Pipeline 作业,在 Pipeline 脚本中使用 Ansible:

复制代码
pipeline {
    agent any
    stages {
        stage('Ansible Playbook') {
            steps {
                ansiblePlaybook credentialsId: 'your-credentials-id', inventory: 'inventory-file-path', playbook: 'playbook-file.yml'
            }
        }
    }
}

五、故障排除

5.1 Jenkins 启动失败
  • 确认 Java 正确安装,运行 java -version 检查版本是否为 11 以上。
  • 确认端口 8080 没有被其他服务占用。
5.2 Git 无法连接仓库
  • 检查 SSH 密钥是否正确配置。通过 ssh -T git@github.com 来测试连接。
  • 检查防火墙是否允许 Git 的连接端口。
5.3 Ansible 无法连接服务器
  • 检查目标主机是否配置了 SSH 免密登录,ansible all -m ping 命令是否正常执行。
  • 确认 /etc/ansible/hosts 中的主机名和 IP 地址是否正确。

相关推荐
不老刘8 小时前
Antigravity 的 New Worktree 模式:给 AI 代理一个安全的“平行工位”
人工智能·git·ai编程·antigravity
光谱SpectrAI9 小时前
Windows 也中招:Git Bash 和 Node 仍信任泄露的 eMudhra 证书
git
YIN_尹9 小时前
【git原理与使用】git初识与安装
大数据·git·elasticsearch
广目软件9 小时前
HA分布式架构采集节点变更操作手册20261029
运维·服务器·ansible·zabbix·prometheus
Leo_Mr14 小时前
Vue3 项目配置 Git 提交规范(企业通用方案:husky + commitlint + cz-git + lint-staged)
git
jyOverQ14 小时前
Git rebase 和 merge 有什么区别?什么时候该用哪个?
git
晗夜未央14 小时前
跨仓库 Skill 换台电脑就失效?用 Git remote 校验项目
人工智能·git·python
Lsetea14 小时前
Git访问HTTPS证书不受信任:用URL级sslCAInfo限定私有CA范围
git·https·开发工具·ssl证书·ca证书
Moshow郑锴1 天前
Ansible 批量部署 Node.js 服务:ZIP 直解与共享目录分发两种方案对比
node.js·ansible
এ慕ོ冬℘゜1 天前
Git 基础入门|和 SVN 对比 + 四大区域 + 常用命令详解
git·svn