Environment Variables ​
TestBeats supports environment variables for dynamic configuration values. This allows you to keep sensitive information like API keys and webhook URLs out of your configuration files.
Using Environment Variables ​
To use environment variables in your configuration file, wrap the variable name inside {}
:
{
"api_key": "{TEST_BEATS_API_KEY}",
"targets": [
{
"name": "slack",
"inputs": {
"url": "{SLACK_WEBHOOK_URL}"
}
},
{
"name": "teams",
"inputs": {
"url": "{TEAMS_WEBHOOK_URL}"
}
}
]
}
.env File Support ​
TestBeats automatically reads environment variables from a .env
file located in the same directory where TestBeats is executed.
Example .env file: ​
# TestBeats Portal Configuration
TEST_BEATS_API_KEY=api-keyabcdef
TEST_BEATS_PROJECT=my-project
TEST_BEATS_RUN=nightly-tests
# Notification Webhooks
SLACK_WEBHOOK_URL=<slack-webhook-url>
TEAMS_WEBHOOK_URL=https://outlook.office.com/webhook/...
# CI/CD Overrides
TEST_BEATS_CI_NAME=GitHub Actions
TEST_BEATS_CI_REPOSITORY_NAME=my-org/my-repo
.env File Location
The .env
file must be located in the same directory where TestBeats is executed. TestBeats will not search parent directories for .env
files.
Pre-defined Environment Variables ​
TestBeats Portal Variables ​
These variables configure TestBeats Portal integration:
Variable | Description | Example |
---|---|---|
TEST_BEATS_API_KEY | TestBeats Portal API key | api-keyabcdef |
TEST_BEATS_PROJECT | Project name for portal organization | my-project |
TEST_BEATS_RUN | Custom run name for this execution | nightly-tests |
TEST_BEATS_DELAY | Delay between fetching analysis from portal (ms) | 3000 |
CI/CD Information Variables ​
These variables override CI/CD environment detection:
Variable | Description | Example |
---|---|---|
TEST_BEATS_CI_NAME | Name of the CI provider | GitHub Actions |
TEST_BEATS_CI_GIT | Name of the Git provider | GitHub |
TEST_BEATS_CI_REPOSITORY_URL | Repository URL | https://github.com/owner/repo |
TEST_BEATS_CI_REPOSITORY_NAME | Repository name | owner/repo |
TEST_BEATS_CI_REPOSITORY_REF | Branch name or pull request | main or refs/pull/123/merge |
TEST_BEATS_CI_REPOSITORY_COMMIT_SHA | Git commit SHA | abc123def456... |
TEST_BEATS_CI_BUILD_URL | Build URL | https://github.com/owner/repo/actions/runs/123 |
TEST_BEATS_CI_BUILD_NUMBER | Build number | 123 |
TEST_BEATS_CI_BUILD_NAME | Build name | CI Build |
TEST_BEATS_CI_BUILD_REASON | Build reason | push or pull_request |
TEST_BEATS_CI_USER | User who triggered the build | john.doe |
Common Usage Patterns ​
1. Separate Environments ​
Use different environment variables for different environments:
# Development
TEST_BEATS_PROJECT=my-project-dev
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/.../dev-channel
# Production
TEST_BEATS_PROJECT=my-project-prod
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/.../prod-channel
2. CI/CD Secrets ​
Store sensitive values in your CI/CD platform's secrets management:
GitHub Actions:
env:
TEST_BEATS_API_KEY: ${{ secrets.TEST_BEATS_API_KEY }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
GitLab CI:
variables:
TEST_BEATS_API_KEY: $TEST_BEATS_API_KEY
SLACK_WEBHOOK_URL: $SLACK_WEBHOOK_URL
Jenkins:
environment {
TEST_BEATS_API_KEY = credentials('testbeats-api-key')
SLACK_WEBHOOK_URL = credentials('slack-webhook-url')
}
3. Local Development ​
Create a .env
file for local development:
# .env (local development)
TEST_BEATS_API_KEY=tb_dev_key_123
TEST_BEATS_PROJECT=my-project-local
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/.../dev-channel
# Override CI detection for local testing
TEST_BEATS_CI_NAME=Local Development
TEST_BEATS_CI_USER=developer
Environment Variable Priority ​
TestBeats resolves environment variables in the following order (highest to lowest priority):
- System environment variables - Set via
export
or CI/CD .env
file - Local.env
file in execution directory- Default values - Built-in defaults where applicable
Validation and Debugging ​
Check Environment Variables ​
# List all TestBeats-related environment variables
env | grep TEST_BEATS
# Check specific variables
echo "API Key: $TEST_BEATS_API_KEY"
echo "Project: $TEST_BEATS_PROJECT"
echo "Slack URL: $SLACK_WEBHOOK_URL"
Test Variable Substitution ​
# Test if variables are being resolved
testbeats publish --verbose \
--config config.json \
--junit "results/*.xml"
Common Issues ​
Variable not found:
Error: Environment variable SLACK_WEBHOOK_URL not found
Solution: Ensure the variable is set and exported:
bashexport SLACK_WEBHOOK_URL="https://hooks.slack.com/..."
Incorrect syntax:
json// ❌ Wrong "url": "$SLACK_WEBHOOK_URL" "url": "${SLACK_WEBHOOK_URL}" // ✅ Correct "url": "{SLACK_WEBHOOK_URL}"
.env
file not found:Warning: .env file not found
Solution: Ensure
.env
file is in the same directory where TestBeats runs.
Security Best Practices ​
1. Never Commit Secrets ​
# Add to .gitignore
echo ".env" >> .gitignore
echo "*.env" >> .gitignore
2. Use CI/CD Secrets ​
- GitHub Actions: Repository/Organization secrets
- GitLab CI: CI/CD variables (mark as masked)
- Jenkins: Credentials plugin
- Azure DevOps: Variable groups (mark as secret)
3. Rotate Secrets Regularly ​
- Update API keys periodically
- Regenerate webhook URLs
- Update environment variables/secrets in CI/CD
4. Validate in CI/CD ​
# GitHub Actions example
- name: Validate environment
run: |
if [ -z "$TEST_BEATS_API_KEY" ]; then
echo "ERROR: TEST_BEATS_API_KEY not set"
exit 1
fi
if [ -z "$SLACK_WEBHOOK_URL" ]; then
echo "ERROR: SLACK_WEBHOOK_URL not set"
exit 1
fi
Examples ​
Complete Configuration with Environment Variables ​
{
"api_key": "{TEST_BEATS_API_KEY}",
"project": "{TEST_BEATS_PROJECT}",
"run": "{TEST_BEATS_RUN}",
"targets": [
{
"name": "slack",
"condition": "fail",
"inputs": {
"url": "{SLACK_WEBHOOK_URL}",
"channel": "{SLACK_CHANNEL}"
}
},
{
"name": "teams",
"condition": "pass-fail",
"inputs": {
"url": "{TEAMS_WEBHOOK_URL}"
}
}
],
"results": [
{
"type": "junit",
"files": ["{JUNIT_RESULTS_PATH}"]
}
],
"extensions": [
{
"name": "ci-info"
}
]
}
Corresponding .env file ​
# Portal Configuration
TEST_BEATS_API_KEY=api-keyabcdef
TEST_BEATS_PROJECT=my-awesome-project
TEST_BEATS_RUN=automated-tests
# Notification Configuration
SLACK_WEBHOOK_URL=<slack-webhook-url>
SLACK_CHANNEL=#test-alerts
TEAMS_WEBHOOK_URL=https://outlook.office.com/webhook/...
# Test Results Configuration
JUNIT_RESULTS_PATH=test-results/*.xml
# CI/CD Override (optional)
TEST_BEATS_CI_NAME=Custom CI
TEST_BEATS_CI_REPOSITORY_NAME=my-org/my-project
See Also ​
- Configuration Reference - Complete configuration guide