|
| 1 | +# ClickHouse Cloud IP Whitelist Action |
| 2 | + |
| 3 | +A GitHub Action that automatically whitelists your GitHub Actions runner IP address in ClickHouse Cloud, with automatic cleanup after your job completes. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🔐 **Automatic IP Whitelisting**: Adds the current runner's IP to your ClickHouse Cloud service allowlist |
| 8 | +- 🧹 **Automatic Cleanup**: Removes the IP from the allowlist when the job completes (even if it fails) |
| 9 | +- 🚀 **Zero Configuration**: Just provide your ClickHouse credentials |
| 10 | +- ✅ **Secure**: Uses ClickHouse Cloud API with proper authentication |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Prerequisites |
| 15 | + |
| 16 | +You'll need the following from ClickHouse Cloud: |
| 17 | +- Organization ID |
| 18 | +- Service ID |
| 19 | +- API Key ID |
| 20 | +- API Key Secret |
| 21 | + |
| 22 | +### Basic Example |
| 23 | + |
| 24 | +```yaml |
| 25 | +name: ClickHouse Migration |
| 26 | + |
| 27 | +on: [push] |
| 28 | + |
| 29 | +jobs: |
| 30 | + migrate: |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + - name: Checkout |
| 34 | + uses: actions/checkout@v4 |
| 35 | + |
| 36 | + - name: Whitelist Runner IP |
| 37 | + uses: your-username/clickhouse-cloud-whitelist-ip@v1 |
| 38 | + with: |
| 39 | + clickhouse-org-id: ${{ secrets.CLICKHOUSE_ORG_ID }} |
| 40 | + clickhouse-service-id: ${{ secrets.CLICKHOUSE_SERVICE_ID }} |
| 41 | + clickhouse-api-key-id: ${{ secrets.CLICKHOUSE_API_KEY_ID }} |
| 42 | + clickhouse-api-key-secret: ${{ secrets.CLICKHOUSE_API_KEY_SECRET }} |
| 43 | + |
| 44 | + - name: Run Database Operations |
| 45 | + run: | |
| 46 | + # Your ClickHouse operations here |
| 47 | + # The runner IP is now whitelisted! |
| 48 | + |
| 49 | + # IP is automatically removed from allowlist after job completes |
| 50 | +``` |
| 51 | + |
| 52 | +### Using the Output |
| 53 | + |
| 54 | +The action outputs the whitelisted IP address: |
| 55 | + |
| 56 | +```yaml |
| 57 | +- name: Whitelist Runner IP |
| 58 | + id: whitelist |
| 59 | + uses: your-username/clickhouse-cloud-whitelist-ip@v1 |
| 60 | + with: |
| 61 | + clickhouse-org-id: ${{ secrets.CLICKHOUSE_ORG_ID }} |
| 62 | + clickhouse-service-id: ${{ secrets.CLICKHOUSE_SERVICE_ID }} |
| 63 | + clickhouse-api-key-id: ${{ secrets.CLICKHOUSE_API_KEY_ID }} |
| 64 | + clickhouse-api-key-secret: ${{ secrets.CLICKHOUSE_API_KEY_SECRET }} |
| 65 | + |
| 66 | +- name: Show IP |
| 67 | + run: echo "Runner IP is ${{ steps.whitelist.outputs.runner-ip }}" |
| 68 | +``` |
| 69 | +
|
| 70 | +## Inputs |
| 71 | +
|
| 72 | +| Input | Description | Required | |
| 73 | +|-------|-------------|----------| |
| 74 | +| `clickhouse-org-id` | ClickHouse Cloud Organization ID | Yes | |
| 75 | +| `clickhouse-service-id` | ClickHouse Cloud Service ID | Yes | |
| 76 | +| `clickhouse-api-key-id` | ClickHouse Cloud API Key ID | Yes | |
| 77 | +| `clickhouse-api-key-secret` | ClickHouse Cloud API Key Secret | Yes | |
| 78 | + |
| 79 | +## Outputs |
| 80 | + |
| 81 | +| Output | Description | |
| 82 | +|--------|-------------| |
| 83 | +| `runner-ip` | The IP address of the GitHub Actions runner that was whitelisted | |
| 84 | + |
| 85 | +## How It Works |
| 86 | + |
| 87 | +1. **Main Step**: |
| 88 | + - Fetches the current runner's public IP address using ipify.org |
| 89 | + - Adds the IP (as a /32 CIDR) to your ClickHouse Cloud service's IP allowlist |
| 90 | + - Saves the IP and credentials to action state |
| 91 | + - Outputs the IP address |
| 92 | + |
| 93 | +2. **Cleanup Step** (runs automatically at job end): |
| 94 | + - Retrieves the IP from action state |
| 95 | + - Removes the IP from the allowlist |
| 96 | + - Runs even if the job fails (using `post-if: always()`) |
| 97 | + |
| 98 | +## Security Best Practices |
| 99 | + |
| 100 | +1. **Store credentials as GitHub Secrets**: Never hardcode your ClickHouse credentials in workflows |
| 101 | +2. **Use environment-specific secrets**: Create separate API keys for different environments |
| 102 | +3. **Limit API key permissions**: Use the minimum required permissions for your API keys |
| 103 | +4. **Monitor access logs**: Regularly check your ClickHouse Cloud access logs |
| 104 | + |
| 105 | +## Troubleshooting |
| 106 | + |
| 107 | +### IP not whitelisted |
| 108 | + |
| 109 | +If you see connection errors: |
| 110 | +1. Check that all four credentials are correct |
| 111 | +2. Verify your service ID is correct |
| 112 | +3. Ensure your API key has permissions to modify IP allowlists |
| 113 | + |
| 114 | +### Cleanup not running |
| 115 | + |
| 116 | +The cleanup step runs automatically using GitHub Actions' `post` lifecycle. It will: |
| 117 | +- Run even if previous steps fail |
| 118 | +- Only log warnings if cleanup fails (won't fail the job) |
| 119 | + |
| 120 | +## Development |
| 121 | + |
| 122 | +### Building |
| 123 | + |
| 124 | +```bash |
| 125 | +# Install dependencies |
| 126 | +npm install |
| 127 | +
|
| 128 | +# Build TypeScript |
| 129 | +npm run build |
| 130 | +
|
| 131 | +# Bundle for distribution |
| 132 | +npm run bundle |
| 133 | +
|
| 134 | +# Run all checks (format, lint, test, bundle) |
| 135 | +npm run all |
| 136 | +``` |
| 137 | + |
| 138 | +### Testing Locally |
| 139 | + |
| 140 | +You can test the action locally using the `@github/local-action` utility: |
| 141 | + |
| 142 | +```bash |
| 143 | +# Create a .env file with your credentials (see .env.example) |
| 144 | +# Then run: |
| 145 | +npx @github/local-action . src/main.ts .env |
| 146 | +``` |
| 147 | + |
| 148 | +### Project Structure |
| 149 | + |
| 150 | +``` |
| 151 | +. |
| 152 | +├── src/ |
| 153 | +│ ├── main.ts # Main action (adds IP to allowlist) |
| 154 | +│ └── cleanup.ts # Cleanup action (removes IP from allowlist) |
| 155 | +├── dist/ # Compiled JavaScript (committed to repo) |
| 156 | +├── action.yml # Action metadata |
| 157 | +├── package.json # Dependencies and scripts |
| 158 | +└── tsconfig.json # TypeScript configuration |
| 159 | +``` |
| 160 | +
|
| 161 | +## Contributing |
| 162 | +
|
| 163 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 164 | +
|
| 165 | +## License |
| 166 | +
|
| 167 | +MIT |
| 168 | +
|
| 169 | +## Acknowledgments |
| 170 | +
|
| 171 | +- Built with [actions/toolkit](https://github.com/actions/toolkit) |
| 172 | +- IP detection via [ipify.org](https://www.ipify.org/) |
| 173 | +- Template from [actions/typescript-action](https://github.com/actions/typescript-action) |
| 174 | +
|
0 commit comments