如何在调用yarn version时动态选择Git标签前缀?
yarn version Great question! I totally get wanting a faster way to toggle tag prefixes for your commits with yarn version—especially when you’re using those prefixes to trigger different Bitbucket Pipelines scripts (like production vs. staging workflows). Here are a few straightforward solutions that skip the hassle of repeatedly running yarn config set:
1. Use the --tag-prefix Flag (One-Time Quick Fix)
The simplest way is to pass the prefix directly in your yarn version command using the built-in --tag-prefix option. This overrides your config temporarily for that single run, so you don’t have to change any persistent settings.
For example:
# Create a tag with "prod-" prefix (e.g., prod-1.2.3) yarn version --tag-prefix "prod-" # Or use a beta prefix (e.g., beta-1.2.3-beta.1) yarn version --tag-prefix "beta-"
This is perfect for one-off cases where you need to switch prefixes on the fly.
2. Add Script Shortcuts to package.json
If you regularly use a set of prefixes (like prod- and beta-), wrap the commands in npm/yarn scripts to save typing. Just add these to your package.json:
{ "scripts": { "version:prod": "yarn version --tag-prefix \"prod-\"", "version:beta": "yarn version --tag-prefix \"beta-\"" } }
Now you can trigger the versioning with your desired prefix in one short command:
yarn version:prod # or yarn version:beta
This keeps your workflow consistent and avoids typos in long commands.
3. Use Environment Variables (Great for CI/CD)
If you’re running versioning from a script or CI pipeline, you can set the prefix via an environment variable instead of modifying config. Yarn respects the YARN_VERSION_TAG_PREFIX env var:
# Run versioning with a prod prefix YARN_VERSION_TAG_PREFIX=prod- yarn version # Or in a Bitbucket Pipelines step, you could set this dynamically
This is super useful if you want to automate prefix selection based on pipeline triggers (e.g., tagging a release branch uses prod-, while a feature branch uses beta-).
How This Works with Bitbucket Pipelines
Once you’re tagging with different prefixes, you can add conditional logic to your bitbucket-pipelines.yml to run different scripts based on the tag name. For example:
pipelines: tags: 'prod-*': - step: name: Deploy to Production script: - ./deploy-production.sh 'beta-*': - step: name: Deploy to Staging script: - ./deploy-staging.sh
This way, pushing a prod- tag triggers your production workflow, while a beta- tag kicks off staging builds—exactly what you’re aiming for!
内容的提问来源于stack exchange,提问作者dnmh

