> ## Documentation Index
> Fetch the complete documentation index at: https://docs.usebruno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Jenkins CI Integration

export const BrunoButton = ({collectionUrl, width = 160, height = 40, className = '', style = {}}) => {
  const encodedUrl = encodeURIComponent(collectionUrl);
  const buttonUrl = `https://fetch.usebruno.com?url=${encodedUrl}`;
  return <div style={{
    display: 'flex',
    justifyContent: 'center',
    width: '100%',
    margin: '2rem 0',
    ...style
  }} className={className}>
      <a href={buttonUrl} target="_blank" rel="noopener noreferrer" style={{
    textDecoration: 'none',
    display: 'inline-block'
  }}>
        <img src="https://fetch.usebruno.com/button.svg" alt="Fetch in Bruno" width={width} height={height} noZoom style={{
    width: `${width}px`,
    height: `${height}px`,
    display: 'block',
    cursor: 'pointer'
  }} />
      </a>
    </div>;
};

[Jenkins](https://www.jenkins.io/) is a powerful open-source automation server that enables continuous integration and continuous delivery (CI/CD) for software development workflows. It provides a robust platform for automating build, test, and deployment processes across multiple environments and platforms.

Explore our Jenkins setup collection to see practical examples and test Jenkins CI/CD integration:

<BrunoButton collectionUrl="https://github.com/bruno-collections/jenkins-setup" width={160} height={40} />

## Prerequisites

* A running Jenkins instance (local or remote).
* The [NodeJS Plugin](https://plugins.jenkins.io/nodejs/) installed in your Jenkins environment.
* A Git repository containing your Bruno collections and a `Jenkinsfile`.

## Automate API Testing with Jenkins

### Step 1: Organize Your Bruno Collections

Ensure your Bruno collections and environment files are properly organized within your Git repository. A typical structure looks like this:

```
your-api-project/
├── Jenkinsfile
├── collections/
│   ├── authentication/
│   │   ├── login.bru
│   │   └── logout.bru
├── environments/
│   ├── development.bru
│   ├── ci.bru
│   └── production.bru
└── bruno.json
```

### Step 2: Configure Node.js in Jenkins

Before running Bruno CLI, you need to ensure Jenkins can access a Node.js runtime environment.

1. **Install the NodeJS Plugin:**
   * Navigate to `Manage Jenkins` > `Plugins`.
   * Select the `Available plugins` tab, search for "NodeJS", and install it.

2. **Configure a Node.js Installation:**
   * Go to `Manage Jenkins` > `Tools` (under `System Configuration`).
   * Scroll down to the `NodeJS installations` section.
   * Click `Add NodeJS`.
   * Provide a descriptive **Name** (e.g., `Node.js 18`). This exact name will be used in your Jenkinsfile.
   * Check `Install automatically` and select your desired `Node.js version` (e.g., `NodeJS 18.20.8`).
   * **Crucially, ensure the checkbox "Provide Node & npm folder to PATH" is checked.** This ensures `node` and `npm` commands are available to your pipeline.
   * Click `Save`.

### Step 3: Create Your Jenkinsfile

Create a file named `Jenkinsfile` in the root of your Git repository. This file defines the steps of your CI pipeline.

```groovy theme={null}
// Sample Jenkinsfile
pipeline {
    agent any
    environment {
        // Capture the path to Node.js 18. This variable will be available globally.
        // This requires the Node.js tool to be configured in Jenkins.
        NODE_HOME = tool 'Node.js 18'
    }
    stages {
        stage('Checkout Code') {
            steps {
                checkout scm
            }
        }
        stage('Setup Node.js & Install Bruno CLI') {
            steps {
                // Ensure the bin directory is added to PATH for all subsequent sh commands
                withEnv(["PATH+NODE=${NODE_HOME}/bin"]) {
                    echo 'Verifying Node.js and npm versions...'
                    sh 'node -v'
                    sh 'npm -v'
                    echo 'Installing Bruno CLI globally...'
                    sh 'npm install -g @usebruno/cli'
                    sh 'bru --version'
                }
            }
        }
        stage('Run API Tests') {
            steps {
                withEnv(["PATH+NODE=${NODE_HOME}/bin"]) { // Still need this or declare it inside script block
                    echo 'Executing Bruno API tests...'
                    sh 'bru run --env ci --reporter-html results.html'
                }
            }
        }
        stage('Archive Test Results') {
            steps {
                echo 'Archiving test report...'
                archiveArtifacts artifacts: 'results.html', fingerprint: true
            }
        }
    }
    post {
        always {
            echo 'Pipeline finished.'
        }
        success {
            echo 'API Tests Passed Successfully!'
        }
        failure {
            echo 'API Tests Failed! Check console output for details.'
        }
    }
}
            
```

### Step 4: Configure Jenkins Pipeline Job

1. Create a New Jenkins Job:

   * From the Jenkins Dashboard, click New Item.
   * Enter an Item name (e.g., bruno-api-tests-pipeline).
   * Select Pipeline as the item type and click OK.

2. Configure Job Details:

   * In the job configuration page, navigate to the Pipeline section.
   * Set Definition to Pipeline script from SCM and Choose your SCM (e.g., Git).
   * Enter your Repository URL (e.g., `https://github.com/your-org/your-repo.git`).
   * Specify any Credentials if your repository is private.
   * Set Branches to build to \***/main** (or the branch containing your Jenkinsfile).
   * Ensure Script Path is Jenkinsfile (this is the default).

3. Click Save to finalize your job configuration.

### Step 5: Run and Monitor Your Pipeline

1. Trigger a Build:

   * From your Jenkins pipeline job page, click Build Now (in the left-hand menu) to manually start the pipeline.

2. Monitor Progress:

   * Observe the Build History section to see the status of your builds (green checkmark for success, red cross mark for failure).
   * Click on a specific build number, then select Console Output to view real-time logs and detailed execution steps.

3. View Test Results:

   * Once a build that generated results.html completes, navigate to that specific build's page.
   * Look for the **Artifacts** section (usually on the left sidebar or at the top of the build summary).
   * Click on `results.html` to download the report, then open it in your web browser for detailed test outcomes.

## Learn More

For more advanced Jenkins features and configurations, visit the [Jenkins documentation](https://www.jenkins.io/doc/).
