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

# Demo Jenkins CLI Edit a Freestyle Job

> This tutorial explains how to edit a Freestyle job in Jenkins using the Jenkins CLI.

In this tutorial, you’ll learn how to fetch, modify, and update a Freestyle job (`npm-version-test`) on Jenkins using the [Jenkins CLI](https://www.jenkins.io/doc/book/managing/cli/). The job prints the installed [Node.js](https://nodejs.org/) and [npm](https://www.npmjs.com/) versions. We’ll download its XML configuration, adjust the build steps, and push the changes back.

## Prerequisites

* Jenkins server accessible at `http://localhost:8080/`
* `jenkins-cli.jar` downloaded locally
* CLI access via SSH; for example:
  ```bash theme={null}
  ssh -l <user> -p <port> localhost
  ```

| Authentication Method | Command Example                                            |
| --------------------- | ---------------------------------------------------------- |
| HTTP                  | `java -jar jenkins-cli.jar -s http://localhost:8080/ help` |
| SSH                   | `ssh -l siddharth -p 41397 localhost help`                 |

## 1. Check Installed Node.js & npm

First, verify your local Node.js and npm versions:

```bash theme={null}
node -v
npm -v
```

## 2. Download the Job Configuration

Fetch the existing job’s XML to a local file.

### 2.1 Using HTTP

```bash theme={null}
java -jar jenkins-cli.jar -s http://localhost:8080/ get-job npm-version-test > npm-job.xml
```

### 2.2 Using SSH

```bash theme={null}
ssh -l siddharth -p 41397 localhost get-job npm-version-test > npm-job.xml
```

<Callout icon="lightbulb" color="#1CB2FE">
  Always back up your existing XML before editing. You can keep a copy:

  ```bash theme={null}
  cp npm-job.xml npm-job.xml.bak
  ```
</Callout>

## 3. Inspect the Current Configuration

Open `npm-job.xml` and locate the `<builders>` section. It should resemble:

```xml theme={null}
<project>
  ...
  <builders>
    <hudson.tasks.Shell>
      <command>node -v</command>
      <configuredLocalRules/>
    </hudson.tasks.Shell>
  </builders>
  ...
</project>
```

## 4. Update the Build Steps

Modify `npm-job.xml` so the shell step echoes a message and invokes both Node.js and npm versions. Replace the `<builders>` section with:

```xml theme={null}
<project>
  ...
  <builders>
    <hudson.tasks.Shell>
      <command>
        echo "Updating job config using Jenkins CLI"
        node -v
        npm -v
      </command>
      <configuredLocalRules/>
    </hudson.tasks.Shell>
  </builders>
  ...
  <buildWrappers>
    <jenkins.plugins.timestamper.TimestamperBuildWrapper plugin="timestamper@1.27"/>
    <jenkins.plugins.nodejs.NodeJSBuildWrapper plugin="nodejs@1.6.2">
      <nodeJSInstallationName>nodejs-22-6-0</nodeJSInstallationName>
      <cacheLocationStrategy class="jenkins.plugins.nodejs.cache.DefaultCacheLocationLocator"/>
    </jenkins.plugins.nodejs.NodeJSBuildWrapper>
  </buildWrappers>
</project>
```

Save your edits to `npm-job.xml`.

## 5. Push the Updated Configuration

Send the modified XML back to Jenkins:

### 5.1 Via SSH

```bash theme={null}
ssh -l siddharth -p 41397 localhost update-job npm-version-test < npm-job.xml
```

### 5.2 Via HTTP

```bash theme={null}
java -jar jenkins-cli.jar -s http://localhost:8080/ update-job npm-version-test < npm-job.xml
```

A successful update returns no output.

## 6. Verify Your Changes

1. In the Jenkins UI, open **npm-version-test**.
2. Check that the build step now shows:
   ```bash theme={null}
   echo "Updating job config using Jenkins CLI"
   node -v
   npm -v
   ```
3. Trigger a new build. The console log should display something like:

   ```text theme={null}
   Started by user siddharth
   Building on the built-in node in workspace /var/lib/jenkins/workspace/npm-version-test
   + echo Updating job config using Jenkins CLI
   Updating job config using Jenkins CLI
   + node -v
   v22.6.0
   + npm -v
   10.8.2
   Finished: SUCCESS
   ```

That’s it! You’ve successfully edited a Freestyle job’s configuration and updated it in Jenkins using the CLI.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-jenkins-engineer/module/4d6d1f39-307c-4fdb-8d2b-834c1650e792/lesson/0574086d-2d1d-427d-a91c-d1cb8780c9d6" />
</CardGroup>
