在 CentOS 上让 Jenkins 支持多平台的核心思路是:保持 Jenkins Master 稳定运行在 CentOS,通过 Agent 节点扩展到 Windows、macOS、Linux 等平台,使用 标签与 Pipeline 将任务调度到对应平台,必要时结合 Docker 提供一致的构建环境,并用 SSH 插件或 远程脚本完成跨机部署。
架构与节点规划
- 组件分工:
- Master:运行在 CentOS,负责调度与插件管理,不建议在 Master 上直接执行构建。
- Agent:在 Windows、macOS、Linux 上运行,承载实际构建与测试。
- 节点标签:为每个平台设置统一标签(如 linux、windows、macos),便于 Pipeline 选择。
- 典型场景:
- 在 Windows 构建 .NET/Visual Studio/MSBuild 项目。
- 在 Linux 构建 Java/Golang/Node.js/Python 项目。
- 在 macOS 构建 iOS/macOS 应用(需 Apple 硬件与证书)。
- 扩展方式:同一代码库可在不同标签上并行构建与发布,缩短交付周期。
在 CentOS 部署 Master
- 安装 Java 11(推荐 LTS 版本):
- sudo yum install -y java-11-openjdk-devel
- 添加 Jenkins 官方仓库并安装:
- 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 -y jenkins
- 启动与开机自启:
- sudo systemctl start jenkins
- sudo systemctl enable jenkins
- 防火墙放行 8080 端口(默认 Web 端口):
- sudo firewall-cmd --permanent --add-port=8080/tcp
- sudo firewall-cmd --reload
- 访问 http://<CentOS_IP>:8080 完成初始化与插件安装。
添加多平台 Agent 节点
- 在 Jenkins 管理界面进入 Manage Nodes and Clouds → New Node,创建 Permanent Agent,填写:
- Name:如 win-build-01、mac-build-01、linux-build-01。
- Labels:设置 windows / macos / linux,便于后续按标签调度。
- Remote root directory:如 /var/jenkins(Linux/macOS)或 C:\Jenkins(Windows)。
- Executors:根据 CPU/内存设置并发数。
- 启动方式建议:
- Linux/macOS:在目标机器下载 agent.jar,以 java -jar agent.jar 方式连接 Master(推荐在 systemd 或 launchd 中托管为服务)。
- Windows:使用 Java Web Start 或 Launch agent via Java Web Start 方式启动,并在系统服务中托管。
- 安全与连通性:
- 在 全局安全配置中启用 JNLP 代理端口(可固定或随机),并放行相应端口。
- 确保 Master 与 Agent 之间 网络可达、时间同步、必要的 SSH 密钥已分发。
Pipeline 与部署实践
- 按标签选择平台的 Pipeline 示例:
pipeline {
agent none
parameters {
choice(name: ‘PLATFORM’, choices: [‘linux’, ‘windows’, ‘macos’], description: ‘选择构建平台’)
}
stages {
stage(‘Build’) {
agent { label params.PLATFORM }
steps {
script {
if (params.PLATFORM == ‘windows’) {
bat ‘’’
mkdir build
cd build
cmake …
msbuild MyApp.sln /t:Rebuild /p:Configuration=Release
‘’’
} else {
sh ‘’’
mkdir -p build
cd build
cmake …
make
‘’’
}
}
}
}
stage(‘Archive’) {
steps {
archiveArtifacts artifacts: ‘build/**/*’, onlyIfSuccessful: true
}
}
}
}
- 远程部署与容器化:
- 使用 Publish Over SSH 插件将产物发布到 Linux/Windows 目标机并执行部署脚本。
- 在 Pipeline 中使用 Docker 镜像提供一致的跨平台构建环境(如多阶段构建、工具链镜像)。