When Jenkins clones a Git repository, it creates a workspace directory structure that exactly mirrors your repository. The key insight is that Jenkins doesn't place files in the root workspace directory unless they exist in the root of your repository.
workspace/
├── omniture-video/
│ └── test.py
└── roottest.py
The error message you're seeing occurs because Jenkins can't locate the script in the expected path. Several factors could cause this:
- Incorrect relative path specification
- Repository subdirectory structure not accounted for
- Workspace permissions issues
- Git checkout not completing successfully
Here are three reliable approaches to execute your scripts:
Method 1: Absolute Path Reference
python "${WORKSPACE}/roottest.py"
Method 2: Change Directory First
cd "${WORKSPACE}"
python roottest.py
Method 3: Using Jenkins Environment Variables
python "%WORKSPACE%\\roottest.py" # Windows syntax
For more flexibility, consider adding build parameters:
properties([
parameters([
string(name: 'SCRIPT_PATH', defaultValue: 'roottest.py', description: 'Path to script relative to workspace')
])
])
pipeline {
agent any
stages {
stage('Run Script') {
steps {
bat "python \"${env.WORKSPACE}\\${params.SCRIPT_PATH}\""
}
}
}
}
Add these diagnostic commands to your build step:
# Windows
dir /s /b "%WORKSPACE%"
# Linux/macOS
ls -lR "${WORKSPACE}"
If your script requires virtual environments:
pipeline {
agent any
stages {
stage('Setup') {
steps {
bat '''
python -m venv "%WORKSPACE%\\venv"
call "%WORKSPACE%\\venv\\Scripts\\activate.bat"
pip install -r requirements.txt
'''
}
}
stage('Run') {
steps {
bat 'python roottest.py'
}
}
}
}
- Always use absolute paths in your build steps
- Add repository content verification steps
- Implement proper error handling in your scripts
- Consider using the Jenkinsfile pipeline approach
- Set up proper cleanup steps after execution
When setting up Jenkins to run scripts from a Git repository, many developers encounter path-related issues. The core misunderstanding stems from how Jenkins handles workspace directories during Git checkouts.
Jenkins creates a dedicated workspace directory for each job, typically following this pattern:
Jenkins_installation_path/jobs/Job_Name/workspace/
When you configure Git SCM in your job, Jenkins clones the repository into this workspace directory. However, there are several common pitfalls:
- The script path might be relative to a subdirectory in the repository
- Windows path handling might differ from Unix systems
- Python might not be in the system PATH
Here's how to properly execute a Python script from a Git repository in Jenkins:
// Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git url: 'https://github.com/mcholl/SARS-Import.git'
}
}
stage('Execute Script') {
steps {
bat 'python roottest.py' // For Windows
// sh 'python roottest.py' // For Linux
}
}
}
}
If the script is in a subdirectory, you need to reference it properly:
// Windows Batch Command in Jenkins
cd workspace
python omniture-video\\test.py
To verify the repository structure, add these commands before execution:
// Windows
dir /s
// Linux
ls -lR
- Always check the workspace structure first
- Use absolute paths when possible
- Consider using Jenkins Pipeline for better control
- Verify Python installation and PATH settings
- Check file permissions on Windows systems
For more flexibility, you can parameterize the script path:
// Jenkinsfile with parameters
properties([
parameters([
string(name: 'SCRIPT_PATH', defaultValue: 'roottest.py', description: 'Path to script')
])
])
pipeline {
agent any
stages {
stage('Execute') {
steps {
bat "python ${params.SCRIPT_PATH}"
}
}
}
}