CI/CD Integration

Integrating Prices as Code with your CI/CD pipeline allows you to automatically synchronize your pricing configuration with your payment providers whenever changes are made. This guide covers how to set up continuous integration for your pricing updates.

Benefits of CI/CD for Pricing

Implementing CI/CD for your pricing configuration provides several advantages:

General Workflow

A typical CI/CD workflow for Prices as Code might look like this:

  1. Developer updates the pricing configuration file
  2. Changes are committed to version control and a pull request is created
  3. CI runs a validation check on the configuration
  4. After approval, the changes are merged to the main branch
  5. CI/CD pipeline runs the synchronization against your staging environment
  6. After testing, the changes are promoted to production

GitHub Actions Example

Here's an example GitHub Actions workflow file that synchronizes your pricing configuration on push to the main branch:

# .github/workflows/sync-prices.yml
name: Sync Pricing Configuration

on:
  push:
    branches: [main]
    paths:
      - 'pricing.ts'
      - 'pricing.yml'
  workflow_dispatch:  # Allow manual triggering

jobs:
  sync-prices:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'npm'
          
      - name: Install dependencies
        run: npm ci
        
      - name: Validate configuration
        run: npx prices-as-code pricing.ts --dry-run
        env:
          STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}
          
      - name: Sync pricing (production)
        if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
        run: npx prices-as-code pricing.ts
        env:
          STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}

GitLab CI Example

Here's an example GitLab CI/CD configuration:

# .gitlab-ci.yml
stages:
  - validate
  - deploy-staging
  - deploy-production

validate-pricing:
  stage: validate
  image: node:18
  script:
    - npm ci
    - npx prices-as-code pricing.ts --dry-run
  only:
    changes:
      - pricing.ts
      - pricing.yml
  environment:
    name: validation
  variables:
    STRIPE_SECRET_KEY: $STRIPE_TEST_KEY

sync-pricing-staging:
  stage: deploy-staging
  image: node:18
  script:
    - npm ci
    - npx prices-as-code pricing.ts
  only:
    refs:
      - main
    changes:
      - pricing.ts
      - pricing.yml
  environment:
    name: staging
  variables:
    STRIPE_SECRET_KEY: $STRIPE_TEST_KEY

sync-pricing-production:
  stage: deploy-production
  image: node:18
  script:
    - npm ci
    - npx prices-as-code pricing.ts
  only:
    refs:
      - main
    changes:
      - pricing.ts
      - pricing.yml
  environment:
    name: production
  variables:
    STRIPE_SECRET_KEY: $STRIPE_PROD_KEY
  when: manual # Requires manual approval

Environment-Specific Configurations

For different environments (development, staging, production), you might want to use different configuration files or environment variables.

Using Environment Files

You can create environment-specific .env files:

# In CI/CD pipeline
npx prices-as-code pricing.ts --env-file .env.production

Using Different Configuration Files

Or use environment-specific configuration files:

# For staging
npx prices-as-code pricing.staging.ts

# For production
npx prices-as-code pricing.production.ts

Managing Secrets

Never commit API keys or secrets to your repository. Instead:

Setting up Secrets in GitHub Actions

  1. Go to your GitHub repository
  2. Click on "Settings"
  3. Select "Secrets and variables" then "Actions"
  4. Click "New repository secret"
  5. Add your API keys (STRIPE_SECRET_KEY, etc.)

Setting up Secrets in GitLab CI

  1. Go to your GitLab project
  2. Navigate to "Settings" > "CI/CD"
  3. Expand the "Variables" section
  4. Click "Add Variable"
  5. Add your API keys (STRIPE_SECRET_KEY, etc.) and mark them as "Protected" and "Masked"

Validation and Testing

Always implement validation steps before synchronization:

# Validate configuration without making changes
npx prices-as-code pricing.ts --dry-run

This ensures your configuration is valid before attempting to synchronize with payment providers.

Handling Failures

To make your CI/CD pipeline robust, consider:

Pull Request Validation

It's a good practice to validate pricing changes in pull requests before they're merged:

# GitHub Actions example for PR validation
name: Validate Pricing Configuration

on:
  pull_request:
    paths:
      - 'pricing.ts'
      - 'pricing.yml'

jobs:
  validate-pricing:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          
      - name: Install dependencies
        run: npm ci
        
      - name: Validate configuration
        run: npx prices-as-code pricing.ts --dry-run
        env:
          STRIPE_SECRET_KEY: ${{ secrets.STRIPE_TEST_KEY }}

Next Steps