如何根据单仓库文件变更选择执行Codeship的CICD步骤?
Absolutely! You can set up your Codeship pipeline to run only the relevant CI/CD steps based on which directory (frontend/ or backend/) has changes. Here's a straightforward, actionable approach to make this work:
For Codeship Classic (Most Common Setup)
Codeship Classic lets you define pipeline logic via shell scripts, which makes conditional execution easy using git change detection.
Step 1: Add Change Detection Helpers
First, create a helper script (e.g., scripts/detect_changes.sh) with functions to check if files in your target directories were modified. These use git diff to compare the current commit against the previous one:
#!/bin/bash # Returns 1 if frontend/ has changes, 0 otherwise has_frontend_changes() { git diff --name-only HEAD~1 HEAD | grep -E "^frontend/" | head -1 | wc -l } # Returns 1 if backend/ has changes, 0 otherwise has_backend_changes() { git diff --name-only HEAD~1 HEAD | grep -E "^backend/" | head -1 | wc -l }
Make sure to mark the script as executable:
chmod +x scripts/detect_changes.sh
Step 2: Write Conditional CI/CD Logic
Create a main pipeline script (e.g., scripts/conditional_pipeline.sh) that uses the helpers to run only relevant steps:
#!/bin/bash source ./scripts/detect_changes.sh # Run frontend-specific steps if changes exist if [ $(has_frontend_changes) -eq 1 ]; then echo "🔧 Running frontend CI/CD steps..." cd frontend/ npm install npm run build npm run test # Add your frontend deployment steps here (e.g., upload to CDN, deploy to Netlify) cd .. fi # Run backend-specific steps if changes exist if [ $(has_backend_changes) -eq 1 ]; then echo "🔧 Running backend CI/CD steps..." cd backend/ pip install -r requirements.txt pytest # Add your backend deployment steps here (e.g., deploy to EC2, push Docker image) cd .. fi # Optional: Run shared steps if either directory has changes if [ $(has_frontend_changes) -eq 1 ] || [ $(has_backend_changes) -eq 1 ]; then echo "📢 Running shared post-deployment steps..." # e.g., send Slack notification, update monitoring dashboard fi
Step 3: Configure Codeship to Use the Script
Update your codeship-steps.yml to call the conditional pipeline script instead of hardcoding individual steps:
- name: Run conditional CI/CD pipeline service: app command: ./scripts/conditional_pipeline.sh
For Codeship Pro
If you're using Codeship Pro, the same git diff logic applies. You can embed the change detection directly into your codeship-services.yml commands or use a dedicated script in your service's container. The core idea remains: check for directory changes first, then execute only the relevant steps.
Key Tips
- Branch comparison: If you're merging into a branch other than
main, adjust thegit diffcommand to compare against that branch (e.g.,git diff --name-only origin/main HEAD). - Initial commit fallback: For the first commit in your repo,
HEAD~1won't exist. Add a check to handle this case:if git rev-parse --verify HEAD~1 >/dev/null 2>&1; then # Run diff logic else # Run all steps for initial commit fi - Test locally: Run the pipeline script on your machine first to verify it correctly detects changes in the right directories.
That's it! This setup ensures only frontend steps run when only frontend files change, only backend steps run when only backend files change, and both run if both directories have modifications.
内容的提问来源于stack exchange,提问作者Omprakaash

