Skip to main content
advanced

CI/CD for Beam Pipelines

7 min read

1. Introduction

CI/CD for Apache Beam (Continuous Integration and Continuous Deployment) is the practice of automating the testing, container building, and deployment of data processing pipelines whenever code changes are pushed to code repositories like GitHub or GitLab.

2. Why This Concept Exists

Building and deploying Apache Beam jobs manually from a developer's laptop creates major operational risks:

  • Dependency Drifts: Code might run on your laptop but crash on cluster workers due to missing libraries.
  • Production Outages: Un-tested pipeline changes can break downstream databases or drop live streaming data.
  • Manual Overhead: Rebuilding Docker container images and updating pipeline specifications manually is slow and error-prone.

Automating CI/CD ensures that code changes undergo automated unit tests, integration tests, and container packaging before deployment to production.

3. Key Terminology

  • Continuous Integration (CI): Automatically testing and linting code every time a developer creates a pull request.
  • Continuous Deployment (CD): Automatically building Docker images or Flex Templates and deploying them to staging or production runners.
  • Flex Template: A containerized specification format in Google Cloud Dataflow that packages pipeline code and runtime dependencies into a reusable container.

4. How It Works

  1. Code Push: A developer pushes code to GitHub.
  2. CI Pipeline Firing: GitHub Actions runs pytest or unittest scripts to verify transforms.
  3. Docker Build: If tests pass, CI builds a Docker container image holding the pipeline code and dependencies.
  4. Registry Push: The container image is pushed to a secure cloud registry (like GCP Artifact Registry).
  5. Template Registration: A Dataflow Flex Template spec file is created and uploaded to Google Cloud Storage.
  6. CD Deployment: The CD agent updates existing streaming jobs (via job replacement) or schedules new batch executions.

5. Visual Diagram

Automated Beam CI/CD Pipeline Workflow

1. Git Commit & PR

Developer submits code changes to GitHub.

GitHub Repository
2. CI Test Runner

Runs unit & integration tests in isolated container runner.

pytest / DirectRunner
3. Container Registry

Builds Docker image & registers Flex Template spec.

Artifact Registry

6. Code Example

Below is an example GitHub Actions workflow file (.github/workflows/deploy-beam.yml) that automates unit testing and deploys a Beam Flex Template:
yaml
name: CI/CD Pipeline for Beam

on:
  push:
    branches: [ main ]

jobs:
  test-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install apache-beam pytest

      - name: Run Unit Tests
        run: |
          pytest tests/

      - name: Authenticate to Google Cloud
        uses: google-github-actions/auth@v1
        with:
          credentials_json: ${{ secrets.GCP_SA_KEY }}

      - name: Build and Push Flex Template Container
        run: |
          gcloud dataflow flex-template build gs://my-bucket/templates/etl_job.json \
            --image-gcr-path="us-central1-docker.pkg.dev/my-project/beam/etl:latest" \
            --sdk-language="PYTHON" \
            --flex-template-base-image="PYTHON3" \
            --metadata-file="metadata.json" \
            --py-path="." \
            --env="FLEX_TEMPLATE_PYTHON_PY_FILE=main.py"

7. Code Explanation

  • pytest tests/ runs all test suites locally on the GitHub Action runner using DirectRunner.
  • gcloud dataflow flex-template build packages the pipeline python dependencies into a Docker image, pushes it to Google Artifact Registry, and creates a template launch spec on GCS (gs://my-bucket/templates/etl_job.json).

8. Real Production Example

At a telemetry company, when a streaming pipeline needs an update:

  1. CI automatically triggers a test pipeline using DirectRunner.
  2. Once validated, CD runs gcloud dataflow flex-template run with --update flag.
  3. Dataflow performs a zero-downtime drain and replacement of the live streaming cluster using the new container spec.

9. Common Mistakes

  • Testing against live Cloud Services during CI: CI tests should mock external sources or use DirectRunner with test datasets to prevent slow, costly test runs.
  • Hardcoding secret keys: Never store service account keys directly in source code or template files; use secrets managers or IAM workload identity federation.

10. Best Practices

  • Enforce pull request checks requiring test suites to pass before merging code to main.
  • Maintain separate GCS template locations for staging and production environments.

11. Summary

  • Automates pipeline verification and deployment.
  • Uses DirectRunner for fast, lightweight local testing in CI runners.
  • Packages pipelines as containerized Flex Templates for reliable production deployment.

12. Interactive Challenges

Challenge 1: CI Unit Test Trigger (Beginner)

Which runner should be specified in pipeline unit tests executed within a CI workflow?

Related Apache Beam Topics & Lessons

Advertisement
AdSense Slot #000001Leaderboard Banner (728x90)
Support