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

1. 环境准备

确保你的系统具有互联网访问权限,并且是基于 CentOS 7/8Ubuntu 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 "[email protected]"

验证配置:

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 [email protected] 来测试连接。
  • 检查防火墙是否允许 Git 的连接端口。
5.3 Ansible 无法连接服务器
  • 检查目标主机是否配置了 SSH 免密登录,ansible all -m ping 命令是否正常执行。
  • 确认 /etc/ansible/hosts 中的主机名和 IP 地址是否正确。

相关推荐
忍者扔飞镖2 分钟前
git
git
去伪存真1 小时前
不用动脑,手把手跟着我做,就能掌握Gitlab+Jenkins提交代码自动构部署
前端·jenkins
李菠菜1 小时前
解决Windows系统下Git克隆时报错“unable to checkout working tree”的方法详解
windows·git
island13142 小时前
【git#4】分支管理 -- 知识补充
大数据·git·elasticsearch
船长@Quant3 小时前
协作开发攻略:Git全面使用指南 — 引言
git·版本控制·源代码管理·协作开发
极小狐4 小时前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab
极小狐4 小时前
极狐GitLab 如何 cherry-pick 变更?
人工智能·git·机器学习·gitlab
前端太佬7 小时前
从拧螺丝到造火箭:Git高阶玩家生存报告
前端·git·github
前端太佬7 小时前
从青铜到塑料:Git逃难指南(附救命指令大全)
前端·git·github
Athel7 小时前
git 建立本地仓库并且推送到github上
git