Jenkins CI/CD na VPS — instalacja i konfiguracja pipeline
Opublikowano: 10 kwietnia 2026 · Kategoria: VPS / DevOps
Jenkins to najdłużej działające narzędzie CI/CD w ekosystemie open source — dojrzały, doskonale udokumentowany, z tysiącami pluginów. Self-hosted Jenkins na VPS daje pełną kontrolę: brak limitów czasowych, własne środowiska build, integracja z dowolnymi systemami wewnętrznymi. Ten artykuł pokazuje instalację Jenkins na Ubuntu, konfigurację declarative pipeline (Jenkinsfile), integrację z GitHubem przez webhook, użycie Docker agentów i automatyczny deployment na serwer produkcyjny przez SSH.
Instalacja Jenkins przez apt (Ubuntu 22.04+)
# Wymagane: Java 17 lub 21 sudo apt update && sudo apt install -y openjdk-21-jdk # Dodaj klucz i repozytorium Jenkins sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \ https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian-stable binary/" \ | sudo tee /etc/apt/sources.list.d/jenkins.list sudo apt update && sudo apt install -y jenkins # Uruchom i wlacz przy starcie sudo systemctl enable --now jenkins sudo systemctl status jenkins # Sprawdz haslo inicjalne sudo cat /var/lib/jenkins/secrets/initialAdminPassword # Jenkins domyslnie dziala na porcie 8080 # http://TWOJ_IP:8080
Nginx reverse proxy z SSL dla Jenkins
server {
listen 443 ssl http2;
server_name jenkins.example.com;
ssl_certificate /etc/letsencrypt/live/jenkins.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jenkins.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Wazne dla webhookow i SSE (Blue Ocean)
proxy_read_timeout 90s;
proxy_buffering off;
}
}
server {
listen 80;
server_name jenkins.example.com;
return 301 https://$host$request_uri;
} Jenkinsfile — Declarative Pipeline
Dodaj plik Jenkinsfile do korzenia repozytorium. Declarative Pipeline ma stałą strukturę
i jest walidowany przez Jenkins — błędy składni są wyłapywane przed uruchomieniem.
pipeline {
// Uruchom w kontenerze Docker (izolacja srodowiska)
agent {
docker {
image 'node:20-alpine'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
// Zmienne srodowiskowe (sekrety z Jenkins Credentials)
environment {
DEPLOY_HOST = 'prod.example.com'
DEPLOY_USER = 'deploy'
SSH_KEY = credentials('deploy-ssh-key')
NODE_ENV = 'production'
}
options {
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20'))
skipDefaultCheckout(false)
}
stages {
stage('Install') {
steps {
sh 'npm ci --prefer-offline'
}
}
stage('Lint') {
steps {
sh 'npm run lint'
}
}
stage('Test') {
steps {
sh 'npm test -- --ci --coverage'
}
post {
always {
junit 'coverage/junit.xml'
publishHTML([
allowMissing: false,
reportDir: 'coverage',
reportFiles: 'index.html',
reportName: 'Coverage Report'
])
}
}
}
stage('Build') {
steps {
sh 'npm run build'
}
}
stage('Deploy') {
// Tylko z brancha main
when { branch 'main' }
steps {
sh '''
rsync -avz --delete dist/ \
-e "ssh -i ${SSH_KEY} -o StrictHostKeyChecking=no" \
${DEPLOY_USER}@${DEPLOY_HOST}:/var/www/app/
ssh -i ${SSH_KEY} ${DEPLOY_USER}@${DEPLOY_HOST} \
"cd /var/www/app && pm2 restart app"
'''
}
}
}
post {
success {
echo 'Build i deploy zakonczone sukcesem!'
}
failure {
mail to: '[email protected]',
subject: "Jenkins FAIL: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
body: "Sprawdz logi: ${env.BUILD_URL}"
}
always {
cleanWs()
}
}
} Integracja z GitHub — webhook
Webhook pozwala GitHubowi automatycznie uruchamiać buildy przy każdym push lub pull request. Wymagane: Jenkins dostępny publicznie przez HTTPS.
- Zainstaluj plugin GitHub Integration w Jenkins (Manage Jenkins → Plugins → Available).
-
Utwórz Pipeline job w Jenkins (New Item → Pipeline). W Pipeline definition
wybierz
Pipeline script from SCM, SCM: Git, wpisz URL repo i branch. - W konfiguracji joba zaznacz GitHub hook trigger for GITScm polling.
-
W GitHub: repo → Settings → Webhooks → Add webhook. Payload URL:
https://jenkins.example.com/github-webhook/. Content type:application/json. Events: Just the push event.
# Przetestuj webhook # GitHub: Webhooks → Recent Deliveries → zobaczysz status 200 OK # Alternatywa: polling SCM co 5 minut (nie wymaga publicznego HTTPS) # W konfiguracji joba: Build Triggers → Poll SCM # Schedule: H/5 * * * * (H = hash-based jitter, unika thundering herd)
Credentials i sekrety w Jenkins
# Dodaj SSH key do Jenkins Credentials:
# Manage Jenkins → Credentials → System → Global credentials → Add Credentials
# Kind: SSH Username with private key
# ID: deploy-ssh-key (to ID uzyte w Jenkinsfile: credentials('deploy-ssh-key'))
# Username: deploy
# Private Key: wklej tresc klucza prywatnego
# Dodaj API key lub token:
# Kind: Secret text
# ID: docker-hub-token
# Uzycie w Jenkinsfile (binding do zmiennej):
# environment {
# DOCKER_TOKEN = credentials('docker-hub-token')
# }
# Jenkins nie wyswietla wartosc sekretow w logach (maskuje ***) Blue Ocean — nowoczesny UI dla pipeline
Blue Ocean to plugin Jenkins z nowoczesnym interfejsem wizualizującym pipeline jako diagramy przepływu. Szczególnie przydatny przy złożonych pipeline z równoległymi stage.
- Instalacja: Manage Jenkins → Plugins → Available → Blue Ocean (All)
-
Dostęp:
https://jenkins.example.com/blue - Pipeline Editor — graficzne tworzenie Jenkinsfile bez ręcznego pisania Groovy
- Wizualizacja równoległych stage (parallel steps) — widać które kroki toczą się jednocześnie
- Logi per step — zamiast jednego długiego loga, każdy krok ma własny widok
Jenkins vs alternatywy — porównanie
| Narzędzie | Model | Koszt | Najlepszy dla |
|---|---|---|---|
| Jenkins | Self-hosted, open source | Koszt VPS (~20-40 PLN/msc) | Duże projekty, pełna kontrola, wewnętrzne systemy |
| GitHub Actions | Managed, cloud | Darmowy limit, potem $0.008/min | Projekty na GitHub, szybki start, małe/średnie CI |
| GitLab CI | Managed lub self-hosted | Free tier, SaaS od $29/msc | GitLab repos, DevSecOps, wbudowany registry |
| Drone CI | Self-hosted, Docker-native | Koszt VPS | Lekki Jenkins, YAML config, kontenerowy workflow |
| Woodpecker CI | Self-hosted, open source fork | Koszt VPS | Fork Drone, aktywna społeczność, Gitea/Forgejo |