Bitbucket Pipelines
Levo's security/contract tests can be embedded in quality gates via Bitbucket Pipelines.
This integration uses a PowerShell script (levo-cli-runner.ps1) that handles Python environment setup, Levo CLI installation, and test execution on Windows self-hosted runners.
Table of Contents
- Prerequisites
- Setup
- Repository Variables
- PowerShell Script Overview
- Pipeline Configuration
- Command Reference
- Pipeline Examples
- Troubleshooting
- Best Practices
Prerequisites
Before setting up Levo in your Bitbucket Pipelines, ensure you have:
- A runnable Levo Test Plan - Create a test plan in the Levo console. Refer to Test Plans for more information
- Windows Self-Hosted Runner - Bitbucket Pipelines must be configured to use a Windows self-hosted runner
- Python 3.12+ - The PowerShell script will detect and use Python 3.12 or higher if available
- Levo CLI Authorization Key - Refer to Generating CLI Authorization Keys for instructions
- Organization ID - Refer to Accessing Organization IDs for instructions
- Google Cloud Access Token - Required for accessing Levo CLI from Google Artifact Registry (GAR)
Setup
Step 1: Download the PowerShell Script
The levo-cli-runner.ps1 script is available on GitHub.
You can either:
- Download it manually and place it on your Windows self-hosted runner (e.g.,
C:\Scripts\levo-cli-runner.ps1) - Download it automatically in your pipeline using the raw GitHub URL (recommended)
The recommended approach is to download it in your pipeline to ensure you always use the latest version.
Step 2: Configure Repository Variables
Navigate to your Bitbucket repository:
- Go to Repository settings > Repository variables
- Add the required variables (see Repository Variables section below)
- Mark sensitive variables (API keys, tokens) as Secured
Step 3: Create Pipeline Configuration
Create or update your
Repository Variables
Configure these variables in Repository settings > Repository variables.
Required Variables
| Variable | Description | Secured |
|---|---|---|
LEVOAI_AUTH_KEY | Levo Auth key for authentication. Refer to Generating CLI Authorization Keys | Yes |
LEVOAI_ORG_ID | Levo organization ID. Refer to Accessing Organization IDs | No |
PYPI_USERNAME | Set to _json_key_base64 for Google Artifact Registry | No |
PYPI_PASSWORD | Google Cloud access token for accessing GAR. Please contact support@levo.ai | Yes |
Optional Variables
| Variable | Description | Default |
|---|---|---|
LEVOAI_CLI_VERSION | Specific CLI version to install (e.g., 1.2.3). If not set, latest version is installed | latest |
LEVOAI_BASE_URL | Custom Levo API URL. Default: https://api.levo.ai. | https://api.levo.ai |
Configuration via Parameters or Environment Variables
The script accepts configuration either as command-line parameters or environment variables. Parameters take precedence over environment variables.
| Parameter | Environment Variable | Description | Default |
|---|---|---|---|
-AppName | APP_NAME | Application name for test runs. If not set, auto-detected from repository | Auto-detected |
-Environment | ENVIRONMENT | Target environment (e.g., staging, production, development) | staging |
-TestMethods | TEST_METHODS | HTTP methods to test (comma-separated: GET,POST,PUT,DELETE). Mutually exclusive with -ExcludeMethods | GET,POST |
-ExcludeMethods | EXCLUDE_METHODS | HTTP methods to exclude from testing (comma-separated: GET,POST,PUT,DELETE). Mutually exclusive with -TestMethods | None |
-EndpointPattern | ENDPOINT_PATTERN | Regex pattern to match endpoints to be tested (e.g., ^/api/v1/.*) | None (all endpoints) |
-ExcludeEndpointPattern | EXCLUDE_ENDPOINT_PATTERN | Regex pattern to exclude endpoints from testing (e.g., ^/health) | None |
-TestUsers | TEST_USERS | Comma-separated list of test user names to use for testing. Users must be configured in the app's IAM configuration | Auto-detected from app config |
-TargetUrl | TARGET_URL | Target URL for the test run. If not provided, will attempt to use target URL from traces | Auto-detected from app config |
-Categories | CATEGORIES | Comma-separated list of test categories to run (e.g., CORS, Broken User Authentication, Fuzzing, SQL Injection). See the Levo console for the complete list. | All available categories |
-FailScope | FAIL_SCOPE | When to fail the build: new (new vulnerabilities), any (any vulnerabilities), none (never fail) | new |
-FailSeverity | FAIL_SEVERITY | Minimum severity to fail: critical, high, medium, low, none | high |
-FailThreshold | FAIL_THRESHOLD | Optional: fail if vulnerability count exceeds this threshold (integer) | None (no threshold) |
Note: You can pass these as parameters directly to the script (e.g., levo-cli-runner.ps1 test -AppName myapp -Environment production) or set them as environment variables in your pipeline. -TestMethods and -ExcludeMethods are mutually exclusive—use only one.
Pipeline Configuration
Basic Pipeline Structure
Configure your
test command. Include artifacts for levo-test-output.log and pip-install.log for debugging.
Complete Pipeline Example
For a complete reference implementation, see the example
- Pull request validation pipelines
- Branch-based testing (main/master)
- Custom pipelines for audits and environment-specific testing
- Proper caching and artifact configuration
Using the Script
Download the levo-cli-runner.ps1 script from GitHub in your pipeline using PowerShell. This ensures you always use the latest version:
script:
- powershell -ExecutionPolicy Bypass -Command "Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/levoai/downloads/main/bitbucket/levo-cli-runner.ps1' -OutFile 'levo-cli-runner.ps1'"
- powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test
Command Reference
The levo-cli-runner.ps1 PowerShell script supports the following commands:
install
Installs Levo CLI into the virtual environment. Run with powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' install.
Use Cases:
- Initial setup
- Debugging installation issues
- Updating to a specific version (set
LEVOAI_CLI_VERSION)
test
Runs security tests. This is the primary command for CI/CD pipelines. Execute with powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test.
Usage:
Basic Examples:
# Using environment variables (default)
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test
# Using parameters
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -AppName myapp -Environment production -FailSeverity critical
# Mix of environment variables and parameters
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -Environment production -TestMethods "GET,POST,PUT"
Endpoint Filtering:
# Filter endpoints by pattern
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -EndpointPattern "^/api/v1/.*"
Test Configuration:
# Specify test users
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -TestUsers "admin,user1"
# Specify target URL
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -TargetUrl "https://api.example.com"
# Run specific test categories
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -Categories "CORS,Broken User Authentication"
Failure Configuration:
# Set failure threshold
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -FailThreshold 10
Complex Example:
# Complex example with multiple options
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -AppName myapp -Environment staging -TestMethods "GET,POST" -EndpointPattern "^/api/.*" -Categories "CORS,Rate Limit" -FailScope new -FailSeverity high -FailThreshold 5
Behavior:
- Auto-installs Levo CLI if not present
- Runs tests based on parameters or environment variables (parameters take precedence)
- Fails the build if vulnerabilities are found (based on
FAIL_SCOPE,FAIL_SEVERITY, andFAIL_THRESHOLD) - If
TARGET_URLis not provided, attempts to use target URL from app configuration or traces - If
TEST_USERSis not provided, uses the default user from app's IAM configuration - If
CATEGORIESis not provided, runs all available test categories
audit
Runs comprehensive security audit without failing the build. Execute with powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' audit.
Behavior:
- Runs all HTTP methods (GET, POST, PUT, DELETE, PATCH)
- Sets
FAIL_SCOPE=noneandFAIL_SEVERITY=none - Always returns exit code 0 (never fails build)
- Useful for comprehensive security assessments
version
Displays the installed Levo CLI version. Run with powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' version.
help
Displays help information and usage examples. Run with powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' help.
Pipeline Examples
Example 1: Pull Request Validation
Run security tests on pull requests with strict criteria (only fail on new critical vulnerabilities). Configure the pipeline to set FAIL_SCOPE=new and FAIL_SEVERITY=critical for pull request triggers. Include all required environment variables in the script execution.
See the example
Example 2: Branch-Based Testing
Run different test configurations for different branches. For the main branch, configure staging tests with ENVIRONMENT=staging, FAIL_SCOPE=new, and FAIL_SEVERITY=high. Add a separate production step with manual trigger, using ENVIRONMENT=production, FAIL_SCOPE=any, and FAIL_SEVERITY=high.
See the example
Example 3: Custom Pipeline - Security Audit
Run a comprehensive audit that never fails the build. Use the audit command which automatically sets FAIL_SCOPE=none and FAIL_SEVERITY=none, ensuring the build always succeeds while providing comprehensive security assessment results.
See the example
full-security-audit custom pipeline.
Example 4: Custom Pipeline - Install Only
Useful for debugging installation issues. Create a custom pipeline that runs the install command followed by version to verify the installation without running tests.
See the example
install-levo-cli custom pipeline.
Example 5: Environment-Specific Testing
Test against different environments with a custom pipeline variable. Define a TARGET_ENV variable with allowed values (staging, production, development) and use it to set the ENVIRONMENT variable dynamically in your pipeline.
See the example
test-environment custom pipeline.
Example 6: Complete Pipeline with Multiple Stages
Create a complete pipeline with build, test, and deploy stages. Add a security testing step between build and deployment that runs the Levo security tests, ensuring vulnerabilities are caught before deployment.
For a complete reference implementation with all examples, see the example
Example 7: Advanced Filtering and Configuration
Use advanced options to fine-tune your test runs:
Endpoint Filtering:
# Test only API v1 endpoints
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -EndpointPattern "^/api/v1/.*"
# Exclude health check and metrics endpoints
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -ExcludeEndpointPattern "^/health|^/metrics|^/status"
HTTP Method Filtering:
# Test only GET and POST methods
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -TestMethods "GET,POST"
# Exclude DELETE and PATCH methods
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -ExcludeMethods "DELETE,PATCH"
Test Categories:
# Run specific categories (e.g., CORS and Broken User Authentication)
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -Categories "CORS,Broken User Authentication"
# Run multiple categories
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -Categories "CORS,Fuzzing,Rate Limit,SQL Injection"
Test Users:
# Use specific test users (must be configured in app's IAM config)
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -TestUsers "admin,user1"
Target URL Override:
# Override target URL from app configuration
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -TargetUrl "https://staging-api.example.com"
Failure Threshold:
# Fail if more than 10 vulnerabilities are found
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -FailThreshold 10
Combined Example:
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test `
-AppName myapp `
-Environment staging `
-TestMethods "GET,POST" `
-EndpointPattern "^/api/v1/.*" `
-ExcludeEndpointPattern "^/health" `
-Categories "CORS,Broken User Authentication,Rate Limit" `
-TestUsers "admin" `
-TargetUrl "https://staging-api.example.com" `
-FailScope new `
-FailSeverity high `
-FailThreshold 5
Troubleshooting
Python Not Found
Error: Python 3.12 or higher is required but not found
Solutions:
- Install Python 3.12+ from python.org
- Ensure Python is in your PATH
- Use the Python launcher:
py -3.12should work - Verify installation:
python --versionorpy --version
Virtual Environment Creation Fails
Error: Failed to create virtual environment
Solutions:
- Check Python installation:
python -m venv --help - Ensure sufficient disk space
- Check write permissions in the repository directory
- Try deleting
.levo-venvand recreating
Levo CLI Installation Fails
Error: pip install failed
Solutions:
- Check
pip-install.logfor detailed error messages - Verify
PYPI_USERNAMEis set to_json_key_base64 - Verify
PYPI_PASSWORDcontains a valid Google Cloud access token - Please contact support@levo.ai for assistance with access tokens
- Check network connectivity to
us-python.pkg.dev
Authentication Errors
Error: LEVOAI_AUTH_KEY is required but not set or LEVOAI_ORG_ID is required but not set
Solutions:
- Verify repository variables are set in Bitbucket with the correct names:
LEVOAI_AUTH_KEYandLEVOAI_ORG_ID - Ensure variables are not marked as "Secured" if they shouldn't be (except for sensitive values)
- Check variable names match exactly (case-sensitive)
- Verify variables are available in the pipeline context
Test Execution Fails
Error: Test runs but exits with non-zero code
Solutions:
- Check
levo-test-output.logfor detailed error messages - Verify your Test Plan LRN is correct
- Ensure the target API is accessible from the runner
- Check
FAIL_SCOPEandFAIL_SEVERITYsettings - Review test results in the Levo console
C Extension Compilation Issues
Error: Warnings about C extension compilation
Solutions:
- The script automatically reinstalls packages with C extensions
- Ensure Visual C++ Build Tools are installed (for Windows)
- Check
pip-install.logfor specific compilation errors - The script should handle most cases automatically
Script Not Found
Error: The system cannot find the file specified
Solutions:
- Verify the script path in your Loading...file
- Ensure the download command uses the correct URL for
levo-cli-runner.ps1 - If using a local script, use absolute paths:
C:\Scripts\levo-cli-runner.ps1 - Or use forward slashes:
C:/Scripts/levo-cli-runner.ps1 - Ensure the script exists on the runner
- Check file permissions
Execution Policy Errors
Error: Execution of scripts is disabled on this system
Solutions:
- The pipeline uses
-ExecutionPolicy Bypassflag - If issues persist, check PowerShell execution policy on the runner
- Run:
Get-ExecutionPolicyto check current policy - Contact your system administrator if needed
Best Practices
1. Use Caching
Always enable caching for virtual environments and pip. Configure levo-venv: .levo-venv and pip: ~/.cache/pip in your pipeline definitions. This significantly speeds up pipeline execution.
2. Store Artifacts
Save test output logs for debugging. Include levo-test-output.log and pip-install.log in your artifacts section to review test results and troubleshoot installation issues.
3. Environment-Specific Configuration
Use different FAIL_SCOPE and FAIL_SEVERITY values for different environments. You can set these as environment variables or pass them as parameters:
- Pull Requests:
FAIL_SCOPE=new,FAIL_SEVERITY=critical(strict) - Staging:
FAIL_SCOPE=new,FAIL_SEVERITY=high(moderate) - Production:
FAIL_SCOPE=any,FAIL_SEVERITY=high(strict)
Example using parameters:
powershell -ExecutionPolicy Bypass -File 'levo-cli-runner.ps1' test -FailScope new -FailSeverity critical
4. Secure Sensitive Variables
Always mark sensitive variables as "Secured" in Bitbucket:
LEVOAI_AUTH_KEYPYPI_PASSWORD- Any tokens or secrets
5. Use Custom Pipelines for Audits
For comprehensive security audits that shouldn't fail builds, use the audit command in a custom pipeline. This allows you to run comprehensive security assessments without blocking your deployment process.
6. Pin CLI Version (Optional)
For production pipelines, consider pinning the CLI version by setting the LEVOAI_CLI_VERSION repository variable to a specific version (e.g., 1.2.3). This ensures consistent behavior across pipeline runs.
7. Monitor Test Results
- Review
levo-test-output.logartifacts after each run - Check the Levo console for detailed test results
- Set up notifications for failed tests
8. Separate Install and Test Steps
For debugging, you can separate installation and testing into different pipeline steps. This allows you to verify the installation independently before running tests.
9. Use Deployment Environments
Leverage Bitbucket's deployment environments for better organization. Use deployment: staging or deployment: production to categorize your pipeline steps and track deployments.
10. Handle Script Updates
The levo-cli-runner.ps1 script is hosted on GitHub. By downloading it in your pipeline, you automatically get the latest version. If you need to pin to a specific version, you can reference a specific commit or tag in the raw GitHub URL.
Additional Resources
- Example Loading...Configuration - Complete reference implementation
levo-cli-runner.ps1Script - PowerShell runner script source- Levo CLI Command Reference
- Test Plans Documentation
- Generating CLI Authorization Keys
- Accessing Organization IDs
- Bitbucket Pipelines Documentation
Need Help?
For further assistance, please reach out to support@levo.ai.