Using Proxies in CI/CD Pipelines: GitHub Actions, Docker, and Rate Limit Bypass
---
Introduction
Using proxies effectively in CI/CD pipelines is essential for managing rate limits and ensuring your tests run smoothly without interruptions. This guide will walk you through setting up proxy configurations in GitHub Actions, Docker, and using proxy rotation middleware in Playwright and pytest test suites. We'll also discuss when to use a proxy versus a mock, the cost-benefit trade-off, and how Bright Data's ephemeral proxies can be beneficial.
---
Setting Up Proxies in GitHub Actions
GitHub Actions provides a flexible way to automate workflows, including setting up environment variables for proxies. To use a proxy in your GitHub Actions workflow, you can set the HTTP_PROXY and HTTPS_PROXY environment variables using secrets to ensure they are not exposed in your repository.
1. Storing Proxy Credentials as Secrets
First, store your proxy credentials as secrets in your GitHub repository settings:
- Go to your repository on GitHub.
- Navigate to Settings > Secrets and click on New repository secret.
- Add
HTTP_PROXYandHTTPS_PROXYwith your proxy URLs.
2. Using Secrets in Workflow YAML
Here's an example of how to use these secrets in a GitHub Actions workflow:
name: CI with Proxy
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
env:
HTTP_PROXY: ${{ secrets.HTTP_PROXY }}
HTTPS_PROXY: ${{ secrets.HTTPS_PROXY }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest requests
- name: Run tests
run: |
pytest
---
Configuring Proxies in Docker Containers
Using Docker for CI/CD pipelines allows you to isolate your testing environment. You can set proxy configurations directly in the Dockerfile or using environment variables.
1. Setting Proxy in Dockerfile
You can set the proxy settings in the Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set environment variables for proxies
ENV HTTP_PROXY=http://your-proxy-url:port \
HTTPS_PROXY=http://your-proxy-url:port
# Install any needed packages specified in requirements.txt
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy the current directory contents into the container at /app
WORKDIR /app
COPY . .
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
2. Using ENV in Docker Compose
Alternatively, you can set the proxy configurations using a .env file or directly in your docker-compose.yml:
version: '3'
services:
web:
build: .
environment:
- HTTP_PROXY=http://your-proxy-url:port
- HTTPS_PROXY=http://your-proxy-url:port
---
Proxy Rotation Middleware in Playwright and pytest
Proxy rotation is crucial for bypassing rate limits, especially when running tests at scale. Here’s how you can implement proxy rotation in both Playwright and pytest.
1. Proxy Rotation with Playwright
Playwright supports setting proxies through browser launch options. Here's a basic example:
from playwright.sync_api import sync_playwright
def rotate_proxies(proxies):
for proxy in proxies:
with sync_playwright() as p:
browser = p.chromium.launch(proxy={
'server': f'http://{proxy}'
})
page = browser.new_page()
# Your test logic here
page.goto('http://example.com')
# Close the browser after each proxy use
browser.close()
if __name__ == '__main__':
proxies = ['proxy1.example.com:8080', 'proxy2.example.com:8080']
rotate_proxies(proxies)
2. Proxy Rotation with pytest
For pytest, you can create a fixture to manage proxy rotation:
import pytest
from playwright.sync_api import sync_playwright
@pytest.fixture(scope='session')
def proxies():
return ['proxy1.example.com:8080', 'proxy2.example.com:8080']
@pytest.fixture(autouse=True, scope='function')
def browser_with_proxy(proxies, request):
proxy = next(request.config.option.proxies)
with sync_playwright() as p:
browser = p.chromium.launch(proxy={
'server': f'http://{proxy}'
})
yield browser
browser.close()
def test_example(browser_with_proxy):
page = browser_with_proxy.new_page()
page.goto('http://example.com')
# Assert statements here
---
When to Use a Proxy vs. Mock
Choosing between using a proxy and a mock depends on your specific needs:
- Proxy: Useful for testing real-world scenarios, including handling different network conditions, IP restrictions, and rate limits. However, proxies can be more expensive and may introduce latency.
- Mock: Ideal for simulating responses without actually hitting external services. This is faster and cheaper but lacks the realism of actual network interactions.
Cost-Benefit Trade-off: While mocks are free and fast, they do not replicate real-world behavior. Proxies, on the other hand, provide a more realistic testing environment but come with costs associated with maintaining and scaling proxy infrastructure.
---
Bright Data API for Ephemeral Proxies
Bright Data offers ephemeral proxies that can be dynamically allocated and deallocated as needed. This is particularly useful for CI/CD pipelines where you need temporary access to bypass rate limits without the overhead of managing a permanent proxy pool. To use Bright Data, you typically need an account with their service.
---
Conclusion
Setting up proxies in your CI/CD pipeline can significantly improve the reliability and realism of your tests. By using GitHub Actions secrets, Docker environment variables, and middleware for Playwright and pytest, you can effectively manage proxy configurations and rotate proxies to avoid rate limits.
If you're looking for a solution that automates proxy testing and rotation locally on your machine, ProxyForge is worth considering. It offers a one-off purchase model without any subscription fees:
---
Note: Always ensure that the proxies you use comply with the terms of service of the websites you are interacting with. Using proxies improperly can lead to your IP being banned or other legal consequences.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →