Web Scraping in GitHub Actions: Proxies, Secrets, and Scheduling
Web Scraping in GitHub Actions: Proxies, Secrets, and Scheduling
If you're a developer looking to automate web scraping tasks using GitHub Actions, this guide is for you. We'll cover best practices for setting up proxies, managing secrets securely, scheduling your workflows, and handling scraped data efficiently.
Introduction
GitHub Actions provides a robust CI/CD platform that's perfect for automating web scraping tasks. However, dealing with proxies, secrets, and scheduling can be tricky. In this guide, we'll walk through each of these aspects step-by-step, focusing on practical solutions rather than marketing hype.
Secrets Management: Never Hardcode Proxy Credentials
Why: Hardcoding your proxy credentials directly into your workflow files is a bad security practice. Instead, use GitHub's built-in secret management feature to keep sensitive information secure.
Steps:
- Store Proxy URL as a Repository Secret:
- Go to your repository settings.
- Navigate to "Secrets" and then "Actions."
- Click on "New repository secret."
- Name it PROXY_URL and paste your proxy credentials (e.g., http://user:[email protected]:port).
- Reference Secrets in Your Workflow File:
Use the ${{ secrets.PROXY_URL }} syntax to reference the secret.
Setting Up Playwright for Web Scraping
Playwright is a powerful tool for browser automation and web scraping. You can use it either via GitHub Actions or by installing it directly in your workflow.
Option 1: Using the Microsoft Playwright GitHub Action
uses: microsoft/playwright-github-action@v1
This action sets up Playwright with Chromium, Firefox, and WebKit pre-installed. It's a convenient way to get started without manually installing dependencies.
Option 2: Installing Playwright Manually
If you prefer more control over the installation process, use the following steps:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install Playwright
run: |
pip install playwright
playwright install --with-deps chromium
This approach is useful if you need to install additional browsers or dependencies.
Using Proxies in Your Workflow
Setting Environment Variables at Job Level
You can set the proxy environment variables at either the job level or the step level. This ensures that all steps within a job use the same proxy settings.
jobs:
scrape:
runs-on: ubuntu-latest
env:
HTTP_PROXY: ${{ secrets.PROXY_URL }}
Passing Proxies to Python Script via `os.environ`
If you need more granular control, pass the proxy directly to your Python script using environment variables:
import os
proxy_url = os.getenv('HTTP_PROXY')
# Use the proxy URL in your scraping logic
Scheduling Your Workflow
You can schedule your GitHub Actions workflows using cron syntax. For example, to run a workflow every 6 hours:
on:
schedule:
- cron: '0 */6 * * *'
This setup is ideal for tasks that need to be executed periodically without manual intervention.
Handling Scraped Data with Artifacts
After scraping data, you can either save it as an artifact or push it directly to a branch in your repository.
Uploading Artifacts
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: scraped-data
path: ./data/
This approach is useful for keeping your repository clean and only storing relevant data.
Pushing Data to a Branch
If you prefer, you can push the scraped data directly to a branch:
- name: Commit and Push Changes
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email '[email protected]'
git add ./data/
git commit -m "Scraped data update"
git push origin HEAD:refs/heads/data-branch
Parallel Scraping with a Matrix Strategy
To scrape multiple URLs simultaneously, use the matrix strategy in GitHub Actions.
jobs:
scrape-matrix:
runs-on: ubuntu-latest
strategy:
matrix:
url: [https://example1.com, https://example2.com]
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install requests
- name: Scrape URL
env:
HTTP_PROXY: ${{ secrets.PROXY_URL }}
run: |
echo "Scraping ${matrix.url}"
# Add your scraping logic here
Conclusion
Using GitHub Actions for web scraping can be a powerful way to automate repetitive tasks. By following the best practices outlined in this guide, you can ensure that your workflows are secure, efficient, and scalable.
Caveats:
- GitHub Runner IPs: Be aware that GitHub Actions runners operate from known cloud IP ranges. For websites protected by Cloudflare or other bot detection systems, consider using residential proxies.
- Proxy Reliability: Managed proxy services like Bright Data and SmartProxy offer reliable rotating proxies suitable for production use.
If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription:
https://proxyforge.dev/#download
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 →Need guaranteed uptime? Try a premium provider