# Jenkins Build Failed

Common Error Patterns

Jenkins build failures typically show:

bash
ERROR: Build failed with exception: java.io.IOException: remote file operation failed
bash
java.lang.OutOfMemoryError: Java heap space
bash
Agent is offline or disconnected
bash
No such property: MESSAGE for class: groovy.lang.Binding
bash
Failed to connect to repository : Problem with host key verification

Root Causes and Solutions

1. Pipeline Syntax Errors

Groovy syntax errors in Jenkinsfile.

Solution:

Use Jenkins Pipeline Syntax Generator:

  1. 1.Navigate to Jenkins > Pipeline Syntax
  2. 2.Generate correct syntax for your step
  3. 3.Validate using Replay feature

Common syntax issues:

```groovy // WRONG - Missing quotes sh 'echo ${variable}'

// CORRECT - Use double quotes for interpolation sh "echo ${variable}"

// WRONG - Incorrect block syntax stage('Build') { sh 'make' }

// CORRECT - Use steps block stage('Build') { steps { sh 'make' } } ```

2. Agent Offline or Disconnected

Build agent not available for execution.

Solution:

Check agent status:

```bash # List agents via CLI java -jar jenkins-cli.jar -s http://jenkins:8080/ get-node

# Or check in Jenkins UI # Navigate to Manage Jenkins > Manage Nodes ```

For SSH agents:

```bash # Test SSH connection ssh -v jenkins-agent@build-server

# Check agent logs tail -f /var/log/jenkins/agent.log ```

Restart agent connection:

groovy
// In Pipeline
node('build-agent') {
  // Build steps
}

3. Memory Issues

Jenkins or agent runs out of memory.

Solution:

Increase Java heap size:

```bash # Edit Jenkins service configuration # Add to /etc/default/jenkins (Linux) JENKINS_JAVA_OPTIONS="-Xmx4g -Xms512m"

# Or in systemd service # /etc/systemd/system/jenkins.service Environment="JAVA_OPTS=-Xmx4g -Xms512m"

# Restart Jenkins sudo systemctl restart jenkins ```

For agents:

bash
# Start agent with more memory
java -Xmx2g -jar agent.jar -jnlpUrl http://jenkins:8080/computer/agent/jenkins-agent.jnlp

4. SCM/Git Connection Issues

Cannot connect to source repository.

Solution:

Verify Git credentials:

```bash # Test Git connection git clone git@github.com:org/repo.git

# Check SSH key ssh -T git@github.com ```

Configure credentials in Jenkins:

  1. 1.Navigate to Credentials > System > Global credentials
  2. 2.Add SSH key or username/password
  3. 3.Use credentials ID in Jenkinsfile:
groovy
git credentialsId: 'git-ssh-key', url: 'git@github.com:org/repo.git'

For host key issues:

```bash # Add host key to known_hosts ssh-keyscan github.com >> ~/.ssh/known_hosts

# Or in Jenkinsfile withCredentials([sshUserPrivateKey(credentialsId: 'git-key', keyFileVariable: 'key')]) { sh 'ssh-keyscan github.com >> ~/.ssh/known_hosts' git url: 'git@github.com:org/repo.git' } ```

5. Plugin Failures

Required plugin missing or incompatible.

Solution:

Check plugin status:

```bash # List installed plugins java -jar jenkins-cli.jar -s http://jenkins:8080/ list-plugins

# Check plugin health # Navigate to Manage Jenkins > Manage Plugins > Updates ```

Install missing plugins:

bash
java -jar jenkins-cli.jar -s http://jenkins:8080/ install-plugin git docker-pipeline

Update plugins:

```bash # Update all plugins java -jar jenkins-cli.jar -s http://jenkins:8080/ install-plugin git docker-pipeline --no-restart

# Restart Jenkins java -jar jenkins-cli.jar -s http://jenkins:8080/ safe-restart ```

6. Docker Build Failures

Docker commands fail in pipeline.

Solution:

Ensure Docker is installed on agent:

bash
docker --version
docker info

Configure Docker access:

groovy
// Use Docker agent
pipeline {
  agent {
    docker {
      image 'maven:3-alpine'
      args '-v /var/run/docker.sock:/var/run/docker.sock'
    }
  }
  stages {
    stage('Build') {
      steps {
        sh 'docker build -t myapp .'
      }
    }
  }
}

7. Environment Variable Issues

Missing or incorrect environment variables.

Solution:

Set environment variables:

groovy
pipeline {
  agent any
  environment {
    MAVEN_HOME = '/opt/maven'
    JAVA_HOME = '/usr/lib/jvm/java-11'
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn package'
      }
    }
  }
}

Load from file:

groovy
pipeline {
  agent any
  stages {
    stage('Setup') {
      steps {
        script {
          def props = readProperties file: 'config.properties'
          env.APP_VERSION = props['version']
        }
      }
    }
  }
}

8. Permission Denied Errors

File or command permission issues.

Solution:

Check file permissions:

```bash ls -la /var/lib/jenkins/workspace/my-job/

# Fix permissions chmod 755 /var/lib/jenkins/workspace/ chown -R jenkins:jenkins /var/lib/jenkins/ ```

Run with appropriate user:

groovy
// In Pipeline
sh 'chmod +x script.sh'
sh './script.sh'

Debugging Techniques

View Build Logs

```bash # Via CLI java -jar jenkins-cli.jar -s http://jenkins:8080/ build my-job -f -v

# Or in Jenkins UI # Navigate to job > Build History > Select build > Console Output ```

Replay Pipeline

  1. 1.Navigate to completed build
  2. 2.Click "Replay"
  3. 3.Modify Groovy script
  4. 4.Run to test changes without committing

Use Try-Catch

groovy
stage('Build') {
  steps {
    script {
      try {
        sh 'make build'
      } catch (Exception e) {
        echo "Build failed: ${e.message}"
        currentBuild.result = 'FAILURE'
        // Optionally continue or fail
        error("Build failed")
      }
    }
  }
}

Enable Debug Logging

groovy
pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '10'))
    timeout(time: 1, unit: 'HOURS')
  }
  stages {
    stage('Debug') {
      steps {
        script {
          // Debug output
          echo "Environment: ${env}"
          echo "Working directory: ${pwd()}"
          sh 'ls -la'
        }
      }
    }
  }
}

Common Build Issues Quick Fix

IssueCommand/Solution
Agent offlineRestart agent service
Out of memory-Xmx4g in JAVA_OPTS
Git auth failedAdd credentials in Jenkins
Plugin missingInstall via Manage Plugins
Docker permissionAdd jenkins to docker group
Syntax errorUse Pipeline Syntax Generator

Health Check Commands

```bash # Check Jenkins status curl -s http://jenkins:8080/api/json?pretty=true

# Check agent status curl -s http://jenkins:8080/computer/api/json?pretty=true

# Check queue curl -s http://jenkins:8080/queue/api/json?pretty=true

# Check recent builds curl -s http://jenkins:8080/view/All/api/json?pretty=true&depth=2 ```

Prevention Tips

  1. 1.Use declarative pipeline syntax for validation
  2. 2.Set up health check notifications
  3. 3.Monitor disk space and memory
  4. 4.Keep plugins updated regularly
  5. 5.Use proper credentials management
  6. 6.Enable pipeline replay for debugging
  • [GitHub Actions Workflow Failed](#)
  • [Docker Build Failed in CI](#)
  • [NPM Install Failed in CI](#)