Certified Jenkins Engineer
Automation and Security
Demo Jenkins CLI Edit a Freestyle Job
In this tutorial, you’ll learn how to fetch, modify, and update a Freestyle job (npm-version-test
) on Jenkins using the Jenkins CLI. The job prints the installed Node.js and npm 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:
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:
node -v
npm -v
2. Download the Job Configuration
Fetch the existing job’s XML to a local file.
2.1 Using HTTP
java -jar jenkins-cli.jar -s http://localhost:8080/ get-job npm-version-test > npm-job.xml
2.2 Using SSH
ssh -l siddharth -p 41397 localhost get-job npm-version-test > npm-job.xml
Note
Always back up your existing XML before editing. You can keep a copy:
cp npm-job.xml npm-job.xml.bak
3. Inspect the Current Configuration
Open npm-job.xml
and locate the <builders>
section. It should resemble:
<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:
<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="[email protected]"/>
<jenkins.plugins.nodejs.NodeJSBuildWrapper plugin="[email protected]">
<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
ssh -l siddharth -p 41397 localhost update-job npm-version-test < npm-job.xml
5.2 Via HTTP
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
In the Jenkins UI, open npm-version-test.
Check that the build step now shows:
echo "Updating job config using Jenkins CLI" node -v npm -v
Trigger a new build. The console log should display something like:
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.
Watch Video
Watch video content