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 地址是否正确。

相关推荐
哈__15 分钟前
飞牛OS还在手动维护?我用Ansible把重复操作写成了Playbook
开发语言·ansible·php
诚丞成21 分钟前
ELK日志分析:Elasticsearch索引、Logstash接入与Kibana可视化
elk·elasticsearch·jenkins
ControlRookie11 小时前
SyncBridge:一个 CODESYS 工程与 AI 之间工程师级的同步工具
git·ai编程·codesys·工业软件·controlrookie
元Y亨H18 小时前
宿主机 SSH 免密已通,为何 Jenkins 流水线仍报 Permission denied?
jenkins
鬼手点金19 小时前
opencode-性能优化建议
java·人工智能·git·自动化·nanogpt
网络毒刘21 小时前
开源 Aider 工作流实战:Git 提交驱动的 AI 结对,与 Cursor 何时互补
git·ai编程·cursor·aider
落魄大学生之流水线上谋生计1 天前
从 Jenkins 小白到企业级交付平台
运维·jenkins
HRTOS1 天前
HRTOS 4.0 将长期维护:为什么版本号不再频繁变化
经验分享·git·单片机·51单片机
驭渊的小故事2 天前
linux 基础命令 + git 仓库创建和配置命令
linux·git
java、iOS、Vue2 天前
Maven antrun vs Jenkins 脚本组装 deb的优缺点、适用场景
java·jenkins·maven