DevOps & Deployment

Jenkins Setup & Deployment Guide

Self-hosted automation server – run Jenkins in Docker, create a Pipeline from a Jenkinsfile, add credentials and webhooks, and secure it with HTTPS.

Updated Official documentation ↗

1. Introduction & use cases

Jenkins is an open-source, self-hosted automation server used for CI/CD for more than a decade. Pipelines are written as code in a Jenkinsfile (Groovy-based declarative syntax) stored in your repository. Thousands of plugins connect it to Git hosts, Docker, Kubernetes, cloud providers and notification tools.

Jenkins runs on Java; the simplest way to start is the official Docker image. In production a controller schedules work and agents (other machines or containers) run the builds.

Where it is used

  • CI/CD for companies that need to self-host
  • Complex pipelines with many integrations and plugins
  • Building on special hardware or inside private networks
  • Scheduled automation jobs

2. Prerequisites

You needVersion / notes
DockerEasiest way to run Jenkins – see the Docker guide
Or JavaA Java version supported by your Jenkins LTS (Java 17 or 21) for native installs
Hardware2 GB+ RAM for the controller, more for builds
Git repositoryWith a project to build

3. Local setup

  1. 1Run Jenkins LTS in Docker

    Data is kept in the jenkins_home volume. Port 8080 is the web UI, 50000 is for agents.

    BASH
    docker run -d --name jenkins --restart unless-stopped \
      -p 8080:8080 -p 50000:50000 \
      -v jenkins_home:/var/jenkins_home \
      jenkins/jenkins:lts-jdk21
  2. 2Unlock Jenkins

    Open http://localhost:8080 and paste the initial admin password.

    BASH
    docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
  3. 3Install plugins and create the admin user

    Choose Install suggested plugins (includes Pipeline and Git), then create your admin account and confirm the Jenkins URL.

4. Create & run a project

  1. 1Add a Jenkinsfile to your repository

    Declarative pipeline with stages and post actions. (The controller needs Node.js for sh 'npm …' – or run the stages in a Docker agent.)

    Jenkinsfile
    pipeline {
      agent any
      options { timestamps() }
      environment {
        NODE_ENV = 'test'
      }
      stages {
        stage('Install') { steps { sh 'npm ci' } }
        stage('Test')    { steps { sh 'npm test' } }
        stage('Build')   { steps { sh 'npm run build' } }
      }
      post {
        success { archiveArtifacts artifacts: 'dist/**', fingerprint: true }
        failure { echo 'Build failed – check the console output' }
      }
    }
  2. 2Create a Pipeline job

    New Item → Pipeline (or Multibranch Pipeline to build every branch) → Pipeline script from SCM → Git → repository URL and credentials → Script Path Jenkinsfile → Save → Build Now.

  3. 3Add credentials

    Manage Jenkins → Credentials → System → Global → Add Credentials: e.g. SSH Username with private key for deployments, Secret text for tokens. Use them in the Jenkinsfile:

    GROOVY
    withCredentials([string(credentialsId: 'api-token', variable: 'API_TOKEN')]) {
      sh 'curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com/deploy'
    }
  4. 4Trigger builds from GitHub

    In the GitHub repository add a webhook to https://jenkins.example.com/github-webhook/ (content type JSON) and enable GitHub hook trigger for GITScm polling in the job. (Webhooks need a public URL – use polling locally.)

✅ When it works you should see output like Figure 1.

5. Development configuration

Environment variables

Use environment { } in the Jenkinsfile for normal values and the Credentials store (withCredentials, credentials()) for secrets – Jenkins masks them in console output.

Jenkinsfile (environment block)
environment {
  APP_ENV = 'staging'
  DEPLOY_HOST = '203.0.113.10'
  DB_PASSWORD = credentials('db-password')   // from the Credentials store
}

Key dependencies

Package / toolUsed for
Pipeline pluginJenkinsfile support (in suggested plugins)
Git / GitHub Branch SourceCheckout and multibranch builds
Docker PipelineRun stages inside containers (agent { docker … })
Credentials BindingwithCredentials in pipelines
SSH AgentUse SSH keys for deployments
Configuration as Code (JCasC)Configure Jenkins from YAML

Folder structure

project structure
my-app/
├── Jenkinsfile
├── src/
└── package.json

/var/jenkins_home/      # Jenkins data (volume)
├── jobs/
├── plugins/
└── secrets/

Essential commands

CommandWhat it does
docker logs -f jenkinsJenkins logs
docker exec jenkins cat …/initialAdminPasswordFirst-time password
Manage Jenkins → PluginsInstall/update plugins
http://jenkins:8080/safeRestartRestart after running builds finish
Pipeline Syntax → Snippet GeneratorGenerate Jenkinsfile steps

6. Production deployment

Server & hosting requirements

RequirementRecommendation
ServerUbuntu VPS, 2 vCPU / 4 GB RAM for the controller
AgentsSeparate machines/containers for builds (keep the controller light)
HTTPSNginx reverse proxy + Let’s Encrypt
SecurityNo anonymous access, matrix/role-based permissions, plugin updates
Backupsjenkins_home volume backed up regularly

Deployment steps

  1. 1Prepare an Ubuntu server

    Create a VPS (AWS EC2, DigitalOcean, Hostinger VPS, Linode …) with Ubuntu 24.04 LTS, at least 1 vCPU and 1–2 GB RAM. Log in with SSH, update the system and create a non-root user for the app.

    BASH
    ssh root@YOUR_SERVER_IP
    apt update && apt upgrade -y
    adduser deploy
    usermod -aG sudo deploy
    # copy your SSH key to the new user, then log in as deploy
    rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy
    exit
    ssh deploy@YOUR_SERVER_IP
  2. 2Run Jenkins in Docker on the server

    Bind the UI only to localhost; Nginx will publish it with HTTPS.

    BASH
    curl -fsSL https://get.docker.com | sudo sh
    sudo docker run -d --name jenkins --restart unless-stopped \
      -p 127.0.0.1:8080:8080 -p 50000:50000 \
      -v jenkins_home:/var/jenkins_home \
      jenkins/jenkins:lts-jdk21
  3. 3Install Nginx as a reverse proxy

    Nginx receives requests on ports 80/443 and forwards them to your app on 127.0.0.1:8080. Replace example.com with your domain. Use a subdomain such as jenkins.example.com.

    /etc/nginx/sites-available/example.com
    server {
        listen 80;
        server_name example.com www.example.com;
    
        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;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
  4. 4Enable the site and reload Nginx

    Link the config into sites-enabled, test it and reload. nginx -t must say syntax is ok before you reload.

    BASH
    sudo apt install -y nginx
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
  5. 5Open only the ports you need

    Allow SSH, HTTP and HTTPS through the firewall. Application ports (3000, 8000 …) stay closed to the internet – Nginx forwards traffic to them.

    BASH
    sudo ufw allow OpenSSH
    sudo ufw allow 'Nginx Full'   # ports 80 and 443 (after installing Nginx)
    sudo ufw enable
    sudo ufw status
  6. 6Point your domain to the server

    In your domain registrar's DNS panel add an A record for @ (and www) with the server's public IP address. DNS changes usually appear within minutes but can take up to 24–48 hours. Check with dig or an online DNS checker.

    OUTPUT
    # Type  Name  Value              TTL
    # A     @     203.0.113.10       3600
    # A     www   203.0.113.10       3600
    dig +short example.com
  7. 7Add free HTTPS with Let’s Encrypt

    Certbot gets a free SSL certificate, adds it to the Nginx config and redirects HTTP to HTTPS. Certificates renew automatically (a systemd timer runs twice a day); test renewal once with --dry-run.

    BASH
    sudo apt install -y certbot python3-certbot-nginx
    sudo certbot --nginx -d example.com -d www.example.com
    sudo certbot renew --dry-run
  8. 8Set the Jenkins URL and security

    Manage Jenkins → System → Jenkins URL = https://jenkins.example.com/. In Security, keep “Jenkins’ own user database”, disable sign-up, use matrix-based permissions, and set the built-in node’s executors to 0 once agents are connected.

  9. 9Back up jenkins_home

    Stop-free backup of the volume to an archive you copy off the server.

    BASH
    sudo docker run --rm -v jenkins_home:/data -v /backups:/backup alpine \
      tar czf /backup/jenkins-$(date +%F).tgz -C /data .

🗺 The whole path from your code to the live site is drawn in the deployment diagram.

7. Images & screenshots

Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.

terminalStarted by user adminObtained Jenkinsfile from git https://github.com/you/my-app.git[Pipeline] stage (Install)+ npm ciadded 312 packages in 11s[Pipeline] stage (Test)+ npm testTests: 24 passed, 24 total[Pipeline] stage (Build)+ npm run buildArchiving artifactsFinished: SUCCESS
Figure 1: terminal output of the sample Jenkins project (section 4).
1Jenkinsfile in Git2Webhook → Jenkins3Agent runs stages4Artifacts + deploy5HTTPS Jenkins UI
Figure 2: production deployment flow (section 6).
my-app/├── Jenkinsfile├── src/└── package.json/var/jenkins_home/ # Jenkins data (volume)├── jobs/├── plugins/└── secrets/
Figure 3: project folder structure (section 5).

8. Common errors & solutions

npm: not found in a pipeline

The agent has no Node.js. Use a Docker agent (agent { docker { image 'node:lts' } }, needs the Docker Pipeline plugin and Docker on the agent) or install the NodeJS plugin/tool.

Webhook does not trigger builds

The URL must be public and end with /github-webhook/; the job needs the GitHub hook trigger enabled; check the webhook’s delivery log in GitHub.

Reverse proxy warning / broken links

Set the correct Jenkins URL and forward X-Forwarded-Proto/Host headers in Nginx.

Plugin dependency errors after updates

Update Jenkins core to the latest LTS first, then plugins; keep a backup of jenkins_home before upgrading.

Builds slow down the UI

Builds are running on the controller. Add agents and set the built-in node’s executors to 0.

Best practices

  • Pipelines as code (Jenkinsfile) – no manual job configuration.
  • Run builds on agents, not the controller.
  • Keep Jenkins and plugins updated; remove unused plugins.
  • Store secrets only in the Credentials store.
  • Back up jenkins_home and test restoring it.