|
| 1 | +# 🔄 Service Sync Workflows |
| 2 | + |
| 3 | +> Automate syncing service directories to deployment repositories |
| 4 | +
|
| 5 | +## 📋 Overview |
| 6 | + |
| 7 | +This repository contains a reusable GitHub Actions workflow for syncing service directories to deployment repositories. The workflow is designed to be flexible and can be used with any service directory and target repository. |
| 8 | + |
| 9 | +## 🚀 Getting Started |
| 10 | + |
| 11 | +### Reusable Workflow Template |
| 12 | + |
| 13 | +The `sync-service-template.yml` workflow handles: |
| 14 | + |
| 15 | +- ✅ Validating source directory exists |
| 16 | +- 🔄 Cloning target repository |
| 17 | +- 🌱 Creating target branch if needed |
| 18 | +- 📂 Syncing directory contents |
| 19 | +- 🚢 Committing and pushing changes |
| 20 | + |
| 21 | +### Usage |
| 22 | + |
| 23 | +Create a new workflow file for your service (e.g. `sync-your-service.yml`): |
| 24 | + |
| 25 | +```yaml |
| 26 | +name: Sync Your Service |
| 27 | +on: |
| 28 | + push: |
| 29 | + paths: |
| 30 | + - 'path/to/your/service/**' |
| 31 | + branches: |
| 32 | + - main |
| 33 | + |
| 34 | +jobs: |
| 35 | + sync-service: |
| 36 | + uses: ./.github/workflows/sync-service-template.yml |
| 37 | + with: |
| 38 | + source_path: path/to/your/service |
| 39 | + target_repo: org/deployment-repo-name |
| 40 | + target_branch: main # optional, defaults to main |
| 41 | + secrets: |
| 42 | + deploy_token: ${{ secrets.YOUR_DEPLOY_TOKEN }} |
| 43 | +``` |
| 44 | +
|
| 45 | +## 🔑 Setting Up Deploy Keys |
| 46 | +
|
| 47 | +For each target repository, set up a deploy key for secure repository access. Deploy keys are SSH keys that grant access to a single repository. See the [GitHub documentation on deploy keys](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/managing-deploy-keys#deploy-keys) for more information. |
| 48 | +
|
| 49 | +### 1. Generate an SSH Key Pair |
| 50 | +
|
| 51 | +Generate a new SSH key pair specifically for this deployment: |
| 52 | +
|
| 53 | +```bash |
| 54 | +ssh-keygen -t ed25519 -C "your_email@example.com" -f deploy_key_filename |
| 55 | +``` |
| 56 | + |
| 57 | +This creates: |
| 58 | + |
| 59 | +- `deploy_key_filename` (private key) |
| 60 | +- `deploy_key_filename.pub` (public key) |
| 61 | + |
| 62 | +When prompted for a passphrase, press Enter to create a key without a passphrase (since it will be used in automated workflows). |
| 63 | + |
| 64 | +To display the public key so you can copy it: |
| 65 | + |
| 66 | +```bash |
| 67 | +# On macOS/Linux |
| 68 | +cat deploy_key_filename.pub |
| 69 | + |
| 70 | +# On Windows (PowerShell) |
| 71 | +Get-Content deploy_key_filename.pub |
| 72 | +``` |
| 73 | + |
| 74 | +For more detailed instructions on generating SSH keys, see [GitHub's documentation on generating a new SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key). |
| 75 | + |
| 76 | +> **Security Note**: Always create a unique key for each repository. Never reuse SSH keys across different repositories or purposes. |
| 77 | +
|
| 78 | +### 2. Add the Public Key to the Target Repository |
| 79 | + |
| 80 | +- Go to the target repository on GitHub |
| 81 | +- Navigate to Settings > Deploy keys |
| 82 | +- Click "Add deploy key" |
| 83 | +- Title: Give it a descriptive name (e.g. "Service Sync Deploy Key") |
| 84 | +- Key: Paste the contents of `deploy_key_filename.pub` |
| 85 | +- ✅ Check "Allow write access" if the workflow needs to push changes |
| 86 | +- Click "Add key" |
| 87 | + |
| 88 | +### 3. Add the Private Key as a Repository Secret |
| 89 | + |
| 90 | +- Go to your source repository's Settings > Secrets and variables > Actions |
| 91 | +- Click "New repository secret" |
| 92 | +- Name: Use a descriptive name (e.g. `RSS_DEPLOY_TOKEN`) |
| 93 | +- Value: Paste the contents of the `deploy_key_filename` file |
| 94 | +- Click "Add secret" |
| 95 | + |
| 96 | +### 4. Use the Deploy Key in Your Workflow |
| 97 | + |
| 98 | +The workflow template in this repository is already configured to use SSH deploy keys. The `sync-service-template.yml` workflow: |
| 99 | + |
| 100 | +1. Uses the [webfactory/ssh-agent](https://github.com/webfactory/ssh-agent) action to securely install the SSH key |
| 101 | +2. Automatically handles SSH authentication for git operations |
| 102 | +3. Uses SSH for git clone and push operations |
| 103 | + |
| 104 | +No additional modifications are needed to use deploy keys with this workflow. |
| 105 | + |
| 106 | +## 📊 Example: RSS Service |
| 107 | + |
| 108 | +The RSS service workflow (`sync-rss-service.yml`) demonstrates how to use the template: |
| 109 | + |
| 110 | +```yaml |
| 111 | +name: Sync RSS Service |
| 112 | +on: |
| 113 | + push: |
| 114 | + paths: |
| 115 | + - 'packages/rss/service/**' |
| 116 | + branches: |
| 117 | + - main |
| 118 | + |
| 119 | +jobs: |
| 120 | + sync-rss: |
| 121 | + uses: ./.github/workflows/sync-service-template.yml |
| 122 | + with: |
| 123 | + source_path: packages/rss/service |
| 124 | + target_repo: curatedotfun/rss-service-template |
| 125 | + target_branch: main |
| 126 | + secrets: |
| 127 | + deploy_token: ${{ secrets.RSS_DEPLOY_TOKEN }} |
| 128 | +``` |
| 129 | +
|
| 130 | +## 🔒 Security Best Practices |
| 131 | +
|
| 132 | +1. **Repository-Specific Keys**: Use a unique deploy key for each target repository. |
| 133 | +
|
| 134 | +2. **Minimal Permissions**: Configure deploy keys with read-only access when possible. |
| 135 | +
|
| 136 | +3. **Key Rotation**: Regularly rotate your deploy keys for better security. |
| 137 | +
|
| 138 | +4. **Branch Protection**: Enable branch protection rules on target repositories to ensure changes follow your workflow. |
| 139 | +
|
| 140 | +## ❓ Troubleshooting |
| 141 | +
|
| 142 | +If the workflow fails, check: |
| 143 | +
|
| 144 | +1. **Source Path**: Ensure the source directory exists and contains the files you want to sync. |
| 145 | +
|
| 146 | +2. **Deploy Key**: Verify the key has the correct permissions and is properly configured. |
| 147 | +
|
| 148 | +3. **Target Repository**: Confirm the repository exists and is accessible with the provided key. |
| 149 | +
|
| 150 | +4. **SSH Configuration**: Check if the SSH configuration in the workflow is correct. |
| 151 | +
|
| 152 | +5. **Branch Access**: Verify there are no branch protection rules preventing pushes. |
0 commit comments