Skip to content

Jenkins Setup Guide for Node.js with GitHub Webhook and systemd


1. Docker Compose for Jenkins

yaml
services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    restart: always
    ports:
      - "8082:8080"
      - "50000:50000"
    volumes:
      - ./data:/var/jenkins_home
      - ./plugins.txt:/usr/share/jenkins/ref/plugins.txt:ro
      - /<home>/.ssh:/var/jenkins_home/.ssh:ro
    environment:
      - JAVA_OPTS=-Djenkins.install.runSetupWizard=false

Commands:

bash
cd /opt/jenkins
sudo docker compose up -d

2. Setup systemd for Jenkins

Create /etc/systemd/system/jenkins.service:

ini
[Unit]
Description=Jenkins Service (Docker Compose)
Requires=docker.service
After=docker.service network.target

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/jenkins
ExecStart=/usr/bin/docker compose -f docker-compose.yaml up -d
ExecStop=/usr/bin/docker compose -f docker-compose.yaml down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

Commands:

bash
sudo systemctl daemon-reload
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

3. Access Jenkins

  1. Open: http://<your-server-ip>:8080
  2. Get initial admin password:
bash
sudo docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
  1. Install suggested plugins.

4. Install Required Plugins

Manage Jenkins → Manage Plugins:

  • GitHub Plugin
  • GitHub Branch Source
  • Pipeline
  • NodeJS Plugin
  • Git Plugin
  • Credentials Binding Plugin

5. Configure Global Tools

Manage Jenkins → Global Tool Configuration:

  1. NodeJS

    • Add NodeJS installation (e.g., NodeJS 20)
    • Check "Install automatically"
  2. Git

    • Ensure the Git path is correct.

6. Install Node.js inside Jenkins container (Optional)

bash
sudo docker exec -it jenkins bash
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
node -v
npm -v
exit

7. Create SSH Credentials for GitHub

bash
sudo docker exec -it jenkins bash
ssh-keygen -t ed25519 -C "jenkins@yourdomain.com"
cat ~/.ssh/id_ed25519.pub
  • Copy the public key to GitHub repository Deploy keys → Add deploy key
  • In Jenkins: Manage Jenkins → Credentials → Add SSH Key

8. Create a Pipeline Job

  1. New Item → Pipeline → OK

  2. Pipeline Definition: Pipeline script from SCM

    • SCM: Git
    • Repository URL: git@github.com:<user>/<repo>.git
    • Credentials: Your SSH key
    • Branch: main

Jenkinsfile example:

groovy
pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'git@github.com:wesdevteam/vite-app-sample.git'
            }
        }

        stage('Install') {
            steps {
                sh 'npm install'
            }
        }

        stage('Test') {
            steps {
                sh 'npm test'
            }
        }

        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }
    }
}

9. Configure GitHub Webhook

  1. In GitHub: Settings → Webhooks → Add webhook

    • Payload URL: http://<jenkins-domain>/github-webhook/
    • Content type: application/json
    • Events: Just the push event
  2. In Jenkins job: Build Triggers → GitHub hook trigger for GITScm polling


10. Optional: Docker Agent for Node.js

groovy
pipeline {
    agent {
        docker { image 'node:20-alpine' }
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
    }
}

Requires Docker Pipeline Plugin.


11. Troubleshooting Tips

  • npm: not found → Install NodeJS via plugin or use Docker agent.
  • SSH issues → Ensure Jenkins has private key and GitHub knows public key.
  • Webhook not triggering → Check Jenkins logs and GitHub webhook delivery status.

✅ With this setup, your Node.js project will automatically build and test on every push, with Jenkins managed by systemd and Node.js available inside the container.