基于Gitea与Jenkins构建轻量级CI/CD流水线实战

📅 2026/6/29 16:41:55
基于Gitea与Jenkins构建轻量级CI/CD流水线实战
1. 为什么选择GiteaJenkins组合最近两年帮不少创业团队搭建过CI/CD环境发现很多小团队在技术选型时容易陷入两难既想要GitLab那样的完整功能又担心服务器资源吃不消。直到遇到Gitea这个轻量级方案配合Jenkins的灵活性终于找到了资源消耗与功能完备性的平衡点。先说Gitea这个用Go语言开发的开源项目内存占用只有GitLab的1/10。我实测过在1核2G的云服务器上同时运行Gitea和Jenkins完全无压力。更难得的是它保留了代码审核、Issue跟踪、Webhook等核心功能特别适合5人以下的微型团队。Jenkins的优势在于插件生态目前有超过1800个插件可供选择。最近帮一个做跨境电商的客户配置自动化部署时就用到了NodeJS插件、SSH插件和Slack通知插件从代码提交到测试环境部署再到结果通知全程无需人工干预。2. 环境准备与基础安装2.1 服务器基础配置建议选择Ubuntu 22.04或CentOS 7系统这里以CentOS 7为例。首先处理几个常见坑点防火墙配置容易遗漏# 永久开放常用端口 firewall-cmd --permanent --add-port3000/tcp # Gitea firewall-cmd --permanent --add-port8080/tcp # Jenkins firewall-cmd --permanent --add-port3306/tcp # MySQL firewall-cmd --reload创建专用用户更安全useradd -m ci-user passwd ci-user usermod -aG wheel ci-user时区设置影响日志时间timedatectl set-timezone Asia/Shanghai2.2 数据库选型与优化虽然Gitea支持SQLite但团队项目建议用MySQL。最近一个项目就遇到过SQLite锁表导致推送失败的情况。安装MySQL时注意# 安装社区版 wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm yum localinstall mysql80-community-release-el7-7.noarch.rpm yum install mysql-community-server关键配置项/etc/my.cnf[mysqld] character-set-serverutf8mb4 collation-serverutf8mb4_unicode_ci innodb_buffer_pool_size256M # 小内存机器特别重要3. Gitea的深度配置技巧3.1 高性能部署方案直接运行二进制虽然简单但生产环境建议用systemd托管# /etc/systemd/system/gitea.service [Unit] DescriptionGitea Aftersyslog.target network.target [Service] Usergit Groupgit WorkingDirectory/var/lib/gitea ExecStart/usr/local/bin/gitea web --config /etc/gitea/app.ini Restartalways EnvironmentUSERgit HOME/home/git几个优化参数app.ini[server] PROTOCOL http DOMAIN yourdomain.com HTTP_PORT 3000 ROOT_URL http://yourdomain.com:3000 DISABLE_SSH false SSH_PORT 22 START_SSH_SERVER false OFFLINE_MODE false3.2 安全加固措施定期备份策略# 数据库备份 mysqldump -u root -p gitea gitea_$(date %Y%m%d).sql # 仓库备份 tar czf gitea_data_$(date %Y%m%d).tar.gz /var/lib/gitea开启HTTPSCertbot示例sudo certbot certonly --standalone -d yourdomain.com4. Jenkins的进阶玩法4.1 插件管理艺术必装插件清单PipelineBlue Ocean新版UIGit Parameter分支选择SSH PipelineGeneric Webhook Trigger插件安装有个坑国内网络可能超时。可以先用清华镜像sed -i s/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g /var/lib/jenkins/hudson.model.UpdateCenter.xml systemctl restart jenkins4.2 Pipeline即代码实践在项目根目录放Jenkinsfile实现配置即代码pipeline { agent any stages { stage(Checkout) { steps { git branch: main, url: http://your-gitea:3000/username/repo.git, credentialsId: gitea-access } } stage(Build) { steps { sh npm install sh npm run build } } stage(Deploy) { steps { sshPublisher( publishers: [ sshPublisherDesc( configName: production-server, transfers: [ sshTransfer( sourceFiles: dist/**, removePrefix: dist, remoteDirectory: /var/www/html ) ] ) ] ) } } } }5. 自动化流水线实战5.1 Webhook配置细节Gitea侧配置时容易出错的点内容类型要选application/json密钥验证建议开启触发事件根据需求选择推荐push事件Jenkins的Generic Webhook Trigger配置示例token: YOUR_SECRET_TOKEN variables: [ [ key: ref, value: $.ref ] ]5.2 多环境部署策略在Jenkinsfile中通过参数控制部署环境parameters { choice( name: DEPLOY_ENV, choices: [dev, staging, production], description: 选择部署环境 ) } stage(Deploy) { steps { script { if(params.DEPLOY_ENV production) { sh rsync -avz dist/ prod-server:/var/www } else { sh rsync -avz dist/ ${params.DEPLOY_ENV}-server:/var/www } } } }6. 踩坑记录与性能调优去年给一个客户部署时遇到Jenkins内存泄漏后来发现是旧的构建记录没清理。现在都会配置自动清理options { buildDiscarder( logRotator( daysToKeepStr: 7, numToKeepStr: 10 ) ) }Gitea的git操作偶尔超时调整这些参数后稳定很多[git.timeout] DEFAULT 3600 MIGRATE 600 MIRROR 300 CLONE 300 PULL 300 GC 600对于资源特别紧张的环境可以限制并发构建# Jenkins配置 numExecutors2这套方案经过十几个项目的验证最长的已经稳定运行2年多。关键是要做好监控我习惯用PrometheusGrafana监控构建耗时和成功率当发现异常时能及时介入。