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

相关推荐
吃饭最爱1 小时前
Elasticsearch的用法
运维·jenkins
初学者_xuan2 小时前
零基础新手小白快速了解掌握服务集群与自动化运维(十八)Ansible自动化模块--安装与入门
运维·自动化·ansible
C.咖.3 小时前
Linux环境下——Git 与 GitHub
linux·git·github·远程仓库
Empty_7774 小时前
Ansible进行Nginx编译安装的详细步骤
linux·nginx·ansible
杂鱼豆腐人5 小时前
pnpm环境下防止误使用npm的方法
前端·git·npm·node.js·git bash
摇滚侠6 小时前
GIT版本管理工具轻松入门 | TortoiseGit,Git 介绍软件安装配置,笔记01
笔记·git
山有木兮木有枝_8 小时前
“误操作导致 feat 功能未生效,尽管 merge 已完成”
git
百锦再8 小时前
第14章 智能指针
android·java·开发语言·git·rust·go·错误
摇滚侠8 小时前
GIT版本管理工具轻松入门 | TortoiseGit,解决冲突,笔记07
笔记·git
Elastic 中国社区官方博客9 小时前
Elasticsearch 的结构化文档配置 - 递归分块实践
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·jenkins