diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..c3cb7bd --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,198 @@ +# Copilot Instructions for git-incron-deploy + +## Project Overview +**git-incron-deploy** is a lightweight auto-deployment system that uses `incron` (file system event monitoring) to automatically pull and deploy code from remote git repositories when triggered. It's designed for keeping development and production systems synchronized with git commits without manual intervention. + +## Architecture & Data Flow + +### Core Mechanism +1. **Trigger Event**: External webhook (PHP/shell script) creates `git.flag` file in monitored directory +2. **incron Detection**: `incron` watches directory for `IN_CREATE` events (new file creation) +3. **Script Execution**: `git.sh` automatically runs on flag detection +4. **Git Sync**: Script performs `git fetch --all` and `git reset --hard origin/` to sync code +5. **Permissions**: Resets ownership to `www-data:www-data` for web server access +6. **Cleanup**: Removes `git.flag` to prevent re-triggering + +### Configuration Files +- **incron.d/www-deploy**: Incron watch rule defining the directory, trigger event, script path, and branch +- **bitbucket-pipelines.yml**: CI/CD integration for automated testing (example shows deployment via calling git.sh) +- **example-trigger/**: Sample webhook implementations (PHP and shell) that create the flag file + +## Key Developer Patterns + +### Flag-Based Triggering +The system relies on file creation as a signal. Cannot monitor non-existent files directly, so `git.flag` must be created by external triggers. This is intentional - incron cannot watch for file creation of files that don't exist yet. + +### Script Parameters +`git.sh` accepts three required parameters: +```bash +./git.sh +# Example: ./git.sh /var/www www-deploy master +# $1: deployment folder path +# $2: expected filename trigger (must be "git.flag") +# $3: branch to deploy from remote +``` + +### Branch Specification +Different branches can be deployed to different directories. Common pattern: +- `/var/www` → production branch (often `master`) +- `/var/www-dev` → development branch (often `dev` or `testing`) +- Each monitored directory gets its own incron rule with branch specified + +### Permission Handling +After git operations, script forces `www-data:www-data` ownership recursively. This is required because git operations may pull files owned by different users. + +## Integration Points + +### External Triggers +Two example implementations provided: +1. **PHP**: `touch("git.flag")` - Lightweight, suitable for webhook endpoints +2. **Shell**: `touch git.flag` - Useful for cron jobs or manual scripts + +These are typically called from: +- GitLab/Bitbucket webhooks (configured to POST to your endpoint) +- External monitoring systems +- Manual deployment scripts + +### Slack Integration +Script calls `slackecho` function (user-implemented) for deployment notifications. The function is referenced but not defined in repo - implementer must provide their own notification wrapper. + +**Implementation Pattern**: Define `slackecho` as a bash function in your deployment environment or as a sourced script: +```bash +# Example function to add to your shell environment +slackecho() { + local message="$1" + curl -X POST -H 'Content-type: application/json' \ + --data "{\"text\":\"$message\"}" \ + https://hooks.slack.com/services/YOUR/WEBHOOK/URL +} +export -f slackecho +``` + +Or wrap deployment calls to capture output: +```bash +slackecho "Deploying to $1" +# ... deployment commands ... +slackecho "Completed" +``` + +The function receives human-readable messages identifying the deployment target and completion status. This allows operators to monitor deployments in real-time across multiple environments. + +## Deployment Workflow + +### Setup Checklist +1. Clone git repo with pre-authentication configured (SSH key or git credentials) +2. Make `git.sh` executable: `chmod +x git.sh` +3. Install incron: `apt-get install incron` (Linux/Debian systems) +4. Copy incron rule to `/etc/incron.d/www-deploy`, adjust paths and branch +5. Set up external trigger (webhook or cron) to create `git.flag` + +### Manual Trigger (Debugging) +```bash +# To manually trigger deployment without webhook +touch git.flag +# incron will detect this and run git.sh +``` + +### Testing via Bitbucket Pipelines +The pipeline runs: `chmod +x` followed by calling `git.sh` with test parameters. This validates the script works in CI environment. + +### Incron Rule Configuration Syntax +Incron rules are stored in `/etc/incron.d/` with the format: +``` +