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:
- Ensures pricing changes are applied consistently
- Provides an audit trail of pricing changes through your version control system
- Enables review processes (e.g., pull requests) for pricing changes
- Prevents manual errors in pricing configurations
- Ensures staging environments have the correct pricing before production changes
General Workflow
A typical CI/CD workflow for Prices as Code might look like this:
- Developer updates the pricing configuration file
- Changes are committed to version control and a pull request is created
- CI runs a validation check on the configuration
- After approval, the changes are merged to the main branch
- CI/CD pipeline runs the synchronization against your staging environment
- 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:
- Use your CI/CD platform's secrets management
- Set environment variables in your CI/CD pipeline
- Use secret management services like AWS Secrets Manager, HashiCorp Vault, etc.
Setting up Secrets in GitHub Actions
- Go to your GitHub repository
- Click on "Settings"
- Select "Secrets and variables" then "Actions"
- Click "New repository secret"
- Add your API keys (STRIPE_SECRET_KEY, etc.)
Setting up Secrets in GitLab CI
- Go to your GitLab project
- Navigate to "Settings" > "CI/CD"
- Expand the "Variables" section
- Click "Add Variable"
- 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:
- Adding retry logic for temporary provider API failures
- Implementing notifications for failed synchronizations
- Setting up monitoring for pricing changes
- Adding rollback capabilities for pricing changes
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
- Learn about Custom Pricing Logic for complex scenarios
- Explore Custom Providers to extend the library
- Check out Working with Metadata for enhanced flexibility