如何通过Jenkins执行Jupyter Notebook处理ROS数据?解决nbconvert错误
Got it, let's tackle your two questions one by one—first fixing that annoying nbconvert not found error in Jenkins, then figuring out how to get return values from your Notebook run.
Fixing the 'nbconvert: No such file or directory' Error
The root issue here is almost always environment variable mismatch between your local user shell and the Jenkins user context. Jenkins runs as a separate system user (usually jenkins on Linux/macOS) and doesn't inherit your personal PATH settings where Jupyter is installed. Here's how to fix it:
Use the absolute path to Jupyter
First, find where Jupyter is installed on your system by running this in your local terminal:which jupyterYou'll get a path like
/home/your_username/.local/bin/jupyter(Linux/macOS) orC:\Users\YourName\AppData\Local\Programs\Python\Python310\Scripts\jupyter.exe(Windows). Replace your Jenkins shell command with this absolute path:/home/your_username/.local/bin/jupyter nbconvert --to notebook --execute mynotebook.ipynbAdd Jupyter's directory to Jenkins' PATH
If you don't want to hardcode paths, configure Jenkins to include Jupyter's location in its global PATH:- Go to Jenkins → Manage Jenkins → System Configuration
- Scroll down to the Environment variables section (enable it if it's hidden)
- Add a new variable named
PATHwith the value:- Linux/macOS:
$PATH:/home/your_username/.local/bin - Windows:
%PATH%;C:\Users\YourName\AppData\Local\Programs\Python\Python310\Scripts
- Linux/macOS:
- Save and restart Jenkins if needed
Activate your virtual environment (if using one)
If you're running Jupyter from a virtual environment, make sure to activate it in the Jenkins shell script first:source /path/to/your/venv/bin/activate jupyter nbconvert --to notebook --execute mynotebook.ipynb deactivateDouble-check file permissions
Ensure the Jenkins user has read access to your rosbag file and Notebook, and write access to the directory where plots are saved. You can adjust permissions with:chmod +r mynotebook.ipynb your_rosbag.bag chmod +w ./plot_output_directory
Returning Values from a Jupyter Notebook Run via Jenkins
Absolutely, you can pass values back to Jenkins—here are the most practical methods:
Write results to a structured file
In your Notebook, export key outputs (like success status, plot paths, robot metrics) to a JSON or text file, then have Jenkins read it:import json # Example: Capture your run results run_results = { "status": "success", "plot_saved_to": "./robot_path_plot.png", "total_distance_traveled": 15.2, "rosbag_processed": True } # Write to a file with open("notebook_results.json", "w") as f: json.dump(run_results, f)In Jenkins, you can use the JSON Parser Plugin to parse this file and extract values as build variables, which you can use in subsequent pipeline steps (like sending notifications or archiving artifacts).
Use exit codes for success/failure
Jenkins automatically checks the exit code of shell commands. In your Notebook, end with an exit code to signal success or failure:try: # Your rosbag processing and plotting code here exit(0) # 0 = success except Exception as e: print(f"Error: {str(e)}") exit(1) # Non-zero = failureJenkins will mark the build as failed if it gets an exit code 1, which is great for basic pass/fail checks.
Print output to the Jenkins log
Simply useprint()in your Notebook to output key information—Jenkins will capture this in the build log. You can then use Jenkins' log parsing features to extract specific values, or write a small shell script to scrape the log for what you need.
内容的提问来源于stack exchange,提问作者HalfBloodPrince7

