変数とシークレット
概要
Bitbucket Pipelines enables configuration of default and custom variables usable in builds and scripts as environment variables.
Referencing Variables
Variables are accessed as environment variables in the build container:
Linux/macOS:
$AWS_SECRETWindows runners:
$env:AWS_SECRETDefault Variables
Pipelines provides built-in variables available for all builds:
変数 | 目的 |
|---|---|
| Set to |
| Unique, incrementing build identifier |
| Repository clone directory path |
| Commit hash triggering the build |
| ワークスペース名 |
| Workspace UUID |
| URL-friendly repository name |
| Repository UUID |
| Full repository name (workspace/repo-slug) |
| True if the repository is private, otherwise false |
| Source branch (branches only) |
| Tag name (tags only) |
| For use with Mercurial projects |
| グループ内の現在のステップのゼロベース インデックス。例: 0、1、2、... 並行ステップでのみ使用できます。 |
| グループ内のステップの合計数。例: 5。 並行ステップでのみ使用できます。 |
| Pull request ID (PR builds only) |
| Target branch for PR |
| Current commit hash of the destination branch |
| Ordered list of IDs of the pull requests currently in the queue |
| URL for the origin, for example: http://bitbucket.org/<workspace>/<repo> |
| SSH origin, for example: git@bitbucket.org:/<workspace>/<repo>.git |
| The exit code of a step, can be used in after-script sections. Values can be 0 (success) or 1 (failed) |
| Unique identifier for the step |
| Person who started the build (by completing a push, merge, etc.), and for scheduled builds, the UUID of the pipelines user. |
| Unique identifier of the upstream pipeline that triggered the pipeline |
| Unique identifier of the pipeline run that triggered the pipeline |
| Unique identifier of the step that triggered the pipeline |
| Selector type of the pipeline that triggered the pipeline |
| Selector pattern of the pipeline that triggered the pipeline |
| Status of the pipeline that triggered the pipeline |
| Unique identifier of the deployment that triggered the pipeline |
| Status of the deployment that triggered the pipeline |
| Name of the environment belonging to the deployment that triggered the pipeline |
| Type of the package, for example CONTAINER or MAVEN |
| Name of the package, for example ubuntu or com.atlassian:my-library |
| Name of the artifact, for example for Docker containers this is the ID of the manifest, such as manifest:sha256:1646cefe9f37d36912d5a179eabef4a91ace6489377b252fe7dd3eeb062eabab, or for Maven packages it is the version, such as 1.0.0. |
| Branch that the pull request containing the generated code fix for the flaky test should target. Typically your default branch where the fix needs to land. |
| Pre-generated branch name the agent must use to push fixes. This is to be used as the source branch when raising the pull request to ensure linking between the flaky test and the generated pull request. |
| Fully qualified domain name of the flaky test. Use this to locate the exact test file and method in your codebase that the agent should fix. |
| Unique identifier of the flaky test case that triggered this pipeline. Use this to look up test history, execution details, or call the API. |
| ID Token generated by the Bitbucket OIDC provider that identifies the step. This token can be used to access resource servers, such as AWS and GCP without using credentials. Learn more |
| Unique identifier for the pipeline |
| デプロイ環境名 |
| Unique identifier of the environment to access environments via the REST API |
| The project key belonging to the current pipeline |
| Unique identifier of the project belonging to the current pipeline |
| The location of the Bitbucket Pipelines private SSH key. This key can be used with BuildKit to access external resources using SSH. この変数は、Bitbucket Cloud と Linux Docker Pipelines ランナー上で実行されているパイプラインでのみ利用できます。 |
| The current step's run number. It helps identify the number of times a particular step has been executed, beginning at 1 for the first execution. |
| The 'username' to authenticate as Bitbucket Pipelines against Bitbucket Packages. This variable is to be used with BITBUCKET_PACKAGES_TOKEN. |
| The 'token' to authenticate as Bitbucket Pipelines against Bitbucket Packages. This variable is to be used with BITBUCKET_PACKAGES_USERNAME. |
| Host of the step's Docker daemon |
|
|
Using Default Variables
pipelines:
default:
- step:
script:
- echo "Building commit $BITBUCKET_COMMIT"
- echo "Build number $BITBUCKET_BUILD_NUMBER"
- echo "Branch $BITBUCKET_BRANCH"User-Defined Variables
Variables can be configured at multiple levels with override hierarchy:
Pipeline > Deployment > Repository > Workspace
Naming Requirements
Alphanumeric characters and underscores only
Case-sensitive
Cannot start with digits
120,000 character value limit
Avoid using PATH variable—it breaks pipeline commands.
Workspace Variables
Accessible across all repository builds. Requires workspace administrator access.
Navigation: Workspace Settings → Pipelines → Workspace variables
Use case: Shared credentials, API keys used across multiple repositories
Repository Variables
Available to users with write access. Requires repository admin to manage.
Location: Repository Settings → Pipelines → Repository variables
Use case: Repository-specific configuration, API endpoints
Deployment Variables
Environment-specific variables override repository and workspace settings.
Location: Repository Settings → Pipelines → Deployments
Use case: Different credentials per environment (staging vs production)
pipelines:
branches:
main:
- step:
name: Deploy to Production
deployment: production
script:
- echo "API URL: $API_URL" # Uses production deployment variableSecured Variables
Sensitive values can be encrypted—marked with a padlock icon. These provide:
Hidden values in build logs
Automatic masking of matching values
URL-encoded variant detection
Secured variables cannot be edited, only replaced or deleted.
Creating Secured Variables
Navigate to Repository Settings → Pipelines → Repository variables
Click "Add variable"
Enter name and value
Check "Secured" checkbox
Click "Add"
使用例
pipelines:
default:
- step:
script:
- echo "Deploying with key $API_KEY" # Will show as $API_KEY in logs
- curl -H "Authorization: Bearer $API_KEY" https://api.example.comShared Pipeline Variables
Export variables between steps using $BITBUCKET_PIPELINES_VARIABLES_PATH:
pipelines:
default:
- step:
name: Generate version
script:
- export VERSION=$(date +%Y%m%d)
- echo "VERSION=$VERSION" >> $BITBUCKET_PIPELINES_VARIABLES_PATH
output-variables:
- VERSION
- step:
name: Use version
script:
- echo "Deploying version $VERSION"Constraints:
50 variable limit per pipeline
100KB total size limit
Only available to subsequent steps
YAML Templating
Non-secured variables can be injected into YAML using template syntax:
image: ${{IMAGE_NAME}}
definitions:
caches:
custom-cache: ${{CACHE_PATH}}
pipelines:
default:
- step:
name: Build for ${{ENVIRONMENT}}
script:
- npm run buildSupported variables:
Workspace and repository variables
Specific defaults:
BITBUCKET_WORKSPACE,BITBUCKET_REPO_SLUG,BITBUCKET_BRANCH,BITBUCKET_COMMIT,BITBUCKET_PR_ID
Limitations: Secured variables and runtime-defined custom variables are not supported.
Variable Examples
API Keys and Credentials
pipelines:
default:
- step:
script:
- npm install
- npm run deploy -- --key=$DEPLOY_KEYDynamic Configuration
pipelines:
branches:
staging:
- step:
deployment: staging
script:
- echo "Deploying to $BITBUCKET_DEPLOYMENT_ENVIRONMENT"
- ./deploy.sh $API_URL $DB_HOST
main:
- step:
deployment: production
script:
- echo "Deploying to $BITBUCKET_DEPLOYMENT_ENVIRONMENT"
- ./deploy.sh $API_URL $DB_HOST # Different values for productionビルド情報
pipelines:
default:
- step:
script:
- echo "Build Info" > build-info.txt
- echo "Commit: $BITBUCKET_COMMIT" >> build-info.txt
- echo "Branch: $BITBUCKET_BRANCH" >> build-info.txt
- echo "Build: $BITBUCKET_BUILD_NUMBER" >> build-info.txtThird-Party Secret Providers
Advanced organizations can integrate secret management tools for dynamic secret retrieval:
HashiCorp Vault
AWS Secrets Manager
Azure Key Vault
Google Secret Manager
These integrations work with self-hosted or cloud runners to fetch secrets at runtime without storing them in Bitbucket.
ベスト プラクティス
Use secured variables for sensitive data - Always mark passwords, API keys, and tokens as secured
Scope appropriately - Use workspace variables for shared resources, deployment variables for environment-specific values
Don't commit secrets - Never put credentials in your
bitbucket-pipelines.ymlfileRotate regularly - Update credentials periodically, especially after team member departures
Use descriptive names - Name variables clearly (e.g.,
AWS_PROD_ACCESS_KEYvsKEY1)Document variables - Keep a list of required variables in your README
次のステップ
Learn about Docker Images to use private registries
Configure Deployments with environment-specific variables
Set up SSH Keys for secure authentication
この内容はお役に立ちましたか?