Jenkins на VPS: CI/CD pipeline с нуля до production
Jenkins LTS на VPS: Docker, agents, Pipeline as Code, GitHub webhooks, деплой на staging. Полный гайд self-hosted CI/CD.
Краткий ответ: Jenkins — классический self-hosted CI/CD. На VPS 2 GB: Docker, LTS, Pipeline из Jenkinsfile, agent на том же или отдельном VPS. Webhook из GitHub/GitLab → build → test → deploy.
Если GitHub Actions и GitLab Runner не подходят (air-gapped, compliance, unlimited minutes) — Jenkins всё ещё стандарт enterprise.
Jenkins vs GitHub Actions vs GitLab CI
| Jenkins | GitHub Actions | GitLab Runner | |
|---|---|---|---|
| Self-hosted | Да | Hybrid | Да |
| Plugins | 2000+ | Marketplace | Built-in |
| Learning curve | Высокая | Низкая | Средняя |
| RAM | 2 GB+ | SaaS | 2 GB+ |
Jenkins выигрывает гибкостью pipeline и интеграциями (Slack, Jira, SonarQube).
Архитектура
Git push → Webhook → Jenkins controller
↓
Jenkins agent (Docker)
↓
build → test → docker push → deploy VPS
Controller — UI, scheduling, credentials.
Agent — выполняет job (лучше отдельный VPS для isolation).
Установка через Docker
services:
jenkins:
image: jenkins/jenkins:lts-jdk17
restart: unless-stopped
user: root
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:50000:50000"
volumes:
- jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
volumes:
jenkins_home:
docker compose up -d
docker compose exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Nginx reverse proxy + Let’s Encrypt → https://ci.example.com.
Первичная настройка
- Unlock Jenkins (initialAdminPassword)
- Install suggested plugins
- Create admin user — сразу включите 2FA (plugin)
- Configure global tools: JDK 17, Git, Docker
Не оставляйте Jenkins открытым в интернет без auth — bot’ы найдут за часы.
Jenkinsfile (Pipeline as Code)
pipeline {
agent any
stages {
stage('Checkout') {
steps { checkout scm }
}
stage('Test') {
steps {
sh 'npm ci && npm test'
}
}
stage('Build Docker') {
steps {
sh 'docker build -t myapp:${BUILD_NUMBER} .'
}
}
stage('Deploy') {
when { branch 'main' }
steps {
sh 'ssh deploy@vps "docker pull myapp:${BUILD_NUMBER} && docker compose up -d"'
}
}
}
post {
failure {
echo 'Notify Telegram/Slack'
}
}
}
Храните Jenkinsfile в репозитории — infrastructure as code mindset.
GitHub webhook
- Jenkins job → Build Triggers → GitHub hook trigger
- GitHub repo → Webhooks →
https://ci.example.com/github-webhook/ - Push → auto build
Для Gitea — аналогичный plugin.
Credentials
Jenkins → Credentials:
- SSH key для deploy на VPS
- Docker registry token
- API keys (не в Jenkinsfile plaintext!)
Лучше — Vault plugin или Jenkins credential store + rotation.
Agent на отдельном VPS
Controller на ci.example.com, agent на build.example.com:
# На agent VPS
docker run -d --name jenkins-agent \
-e JENKINS_URL=https://ci.example.com \
-e JENKINS_SECRET=... \
-e JENKINS_AGENT_NAME=build-1 \
jenkins/inbound-agent
Job не должен иметь root на production — только на build agent.
Docker-in-Docker vs socket mount
| docker.sock mount | DinD | |
|---|---|---|
| Простота | Да | Сложнее |
| Безопасность | Agent = root on host | Изолированнее |
| Production | Только trusted jobs | Предпочтительно |
Для pet-project — socket ok на dedicated build VPS. Для multi-tenant — DinD или Kaniko.
Деплой приложений
Связка с существующими гайдами:
- Node.js PM2
- Laravel
- Django
- Docker Compose pull on VPS
Pipeline stage Deploy:
rsync -az ./dist/ deploy@vps:/var/www/app/
ssh deploy@vps 'sudo systemctl restart myapp'
Мониторинг Jenkins
- Disk:
jenkins_homeрастёт (artifacts!) — cleanup policy - Prometheus plugin — build duration, queue
- Uptime Kuma — ci.example.com
- Логи:
docker compose logs jenkins
// Jenkinsfile — ограничить хранение артефактов
options {
buildDiscarder(logRotator(numToKeepStr: '20'))
}
Бэкапы
docker run --rm -v jenkins_jenkins_home:/data -v $(pwd):/backup alpine \
tar czf /backup/jenkins-$(date +%F).tar.gz /data
В volume — job configs, credentials (encrypted), plugins. Без бэкапа — потеря CI при disk failure.
Типичные ошибки
| Ошибка | Fix |
|---|---|
| OutOfMemory | JAVA_OPTS=-Xmx1024m, VPS 4 GB |
| Permission denied docker.sock | Agent user в docker group |
| Webhook 403 | CSRF / GitHub IP allowlist |
| Slow queue | Добавить agents |
| Plugin hell | Pin LTS + test upgrades on staging |
Безопасность checklist
- HTTPS only
- 2FA для admin
- Agent на отдельном VPS
- Не запускать unreviewed PR pipelines с secrets
- Fail2ban + rate limit
- Обновлять LTS ежемесячно
Итог
Jenkins на VPS — максимально гибкий CI/CD под ваш стек. Controller + agent, Jenkinsfile в git, webhooks, deploy на StormNet Cloud VPS.
Альтернативы полегче: GitHub Actions, GitLab Runner. Jenkins — когда нужны plugins и полный контроль.
Рекомендуем прочитать
Частые вопросы
Jenkins или GitHub Actions для CI/CD?
GitHub Actions проще для GitHub-проектов. Jenkins — когда нужен self-hosted, plugins и полный контроль над agents.
Можно ли Jenkins controller держать на production VPS?
Не рекомендуется. Controller + build agents лучше на отдельных VPS: builds не конкурируют с prod за RAM/CPU.






