A Python script that automatically downloads C# .NET projects from GitHub and restarts the server whenever changes are detected in the repository.
- ✅ GitHub Integration: Downloads repositories using GitHub API with personal access tokens
- ✅ Change Detection: Monitors repository changes using file hashing
- ✅ Auto Restart: Automatically restarts .NET server when changes are detected
- ✅ No Git Required: Works without Git CLI or system Git configuration
- ✅ Comprehensive Logging: Detailed logging to both file and console
- ✅ Error Handling: Robust error handling and recovery
- ✅ Configurable: Easy configuration via JSON file
- Python 3.7+ installed on your system
- .NET SDK installed and accessible via
dotnetcommand - GitHub Personal Access Token with appropriate permissions
- Clone or download this repository
- Install Python dependencies:
pip install -r requirements.txt
- Go to GitHub Developer Settings → Tokens
- Click "Generate new token (classic)"
- Select the following scopes:
repo(for private repositories) orpublic_repo(for public repositories)
- Copy the generated token
Run the script with the --create-config flag to generate a sample configuration:
python dotnet_auto_deploy.py --create-configThis will create a config.json file. Edit it with your specific details:
{
"github_user": "your-username",
"repo_name": "your-repo-name",
"branch": "main",
"token": "ghp_your_github_token_here",
"extract_path": "./repo",
"dotnet_project_path": "src/YourProject",
"check_interval": 60
}| Field | Description | Required | Default |
|---|---|---|---|
github_user |
Your GitHub username | ✅ | - |
repo_name |
Repository name | ✅ | - |
branch |
Branch to monitor | ❌ | main |
token |
GitHub personal access token | ✅ | - |
extract_path |
Local path to extract repository | ✅ | - |
dotnet_project_path |
Path to .NET project within repo | ✅ | - |
check_interval |
Check interval in seconds | ❌ | 60 |
python dotnet_auto_deploy.pypython dotnet_auto_deploy.py --config my-config.jsonpython dotnet_auto_deploy.py --create-config- Repository Download: The script downloads the repository as a ZIP file using GitHub's API
- Change Detection: Generates SHA256 hashes of the repository contents to detect changes
- Auto Restart: When changes are detected, the script:
- Stops the existing .NET process (if running)
- Builds the project using
dotnet build - Starts the application using
dotnet run
- Monitoring: Continuously monitors the repository at the specified interval
-
Initial Setup:
# Create sample config python dotnet_auto_deploy.py --create-config # Edit config.json with your details # Run the script python dotnet_auto_deploy.py
-
Development Workflow:
- Make changes to your .NET project
- Push changes to GitHub
- The script automatically detects changes and restarts the server
The script creates detailed logs in:
- Console: Real-time output
- File:
dotnet_auto_deploy.log(in the same directory)
Log levels include:
INFO: General information and status updatesWARNING: Non-critical issuesERROR: Critical errors that may affect operation
-
"Download failed" error:
- Check your GitHub token has the correct permissions
- Verify the repository name and username are correct
- Ensure the repository exists and is accessible
-
".NET project path does not exist" error:
- Verify the
dotnet_project_pathin your config points to the correct directory - Check that the path is relative to the extracted repository root
- Verify the
-
"Build failed" error:
- Ensure .NET SDK is installed and accessible
- Check the project has all required dependencies
- Review the build output in the logs
-
Permission errors:
- Ensure the script has write permissions to the extract path
- Check that .NET can access the project directory
To enable debug logging, modify the logging level in the script:
logging.basicConfig(
level=logging.DEBUG, # Change from INFO to DEBUG
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('dotnet_auto_deploy.log'),
logging.StreamHandler(sys.stdout)
]
)- Token Security: Keep your GitHub token secure and don't commit it to version control
- File Permissions: Ensure the script runs with appropriate permissions
- Network Security: The script downloads from GitHub over HTTPS
Feel free to submit issues, feature requests, or pull requests to improve this script.
This project is open source and available under the MIT License.