如何基于约定式提交通过Azure Pipeline实现Flutter包自动版本发布
解决方案:Azure Pipeline配置Flutter包自动版本管理
一、访问并修改pubspec.yaml的version属性
可以通过两种可靠方式在流水线中操作pubspec.yaml的版本字段:
1. 使用sed命令(轻量,适合简单场景)
- 读取当前版本:
current_version=$(grep '^version:' pubspec.yaml | sed 's/version: //') echo "当前版本:$current_version" - 修改版本为指定值(示例为1.2.3):
sed -i 's/^version: .*/version: 1.2.3/' pubspec.yaml注意:sed的
-i参数在Ubuntu环境下可直接使用,Azure Pipeline默认Ubuntu代理无需调整。
2. 使用yq工具(更安全,适配YAML格式规范)
yq是专门处理YAML的工具,能避免sed可能出现的格式错误:
- 在Ubuntu代理中安装yq:
sudo apt update && sudo apt install -y yq - 读取当前版本:
current_version=$(yq eval '.version' pubspec.yaml) echo "当前版本:$current_version" - 修改版本:
yq eval '.version = "1.2.3"' -i pubspec.yaml
二、基于约定式提交实现语义化版本自动更新
结合Angular风格的约定式提交规范,可自动推导版本升级类型并完成版本更新,步骤如下:
1. 安装必要工具
在流水线中安装用于分析提交信息和计算版本的工具:
# 安装Node.js(Ubuntu代理默认预装,若未装则执行此命令) sudo apt update && sudo apt install -y nodejs npm # 安装约定式提交分析工具和语义化版本工具 npm install -g conventional-recommended-bump semver
2. 推导版本升级类型
根据最近的提交记录,判断升级类型(patch/minor/major):
# 基于Angular规范分析提交,返回升级类型 release_type=$(conventional-recommended-bump -p angular) echo "版本升级类型:$release_type"
3. 计算新版本号
基于当前版本和升级类型,生成语义化新版本:
current_version=$(yq eval '.version' pubspec.yaml) new_version=$(semver $current_version -i $release_type) echo "新版本号:$new_version"
4. 更新版本并同步回仓库
若需要将版本变更同步到代码仓库,添加以下步骤:
# 更新pubspec.yaml的版本号 yq eval ".version = \"$new_version\"" -i pubspec.yaml # 配置Git提交身份 git config --global user.name "Azure Pipeline" git config --global user.email "azure-pipeline@example.com" # 提交并推送版本变更 git add pubspec.yaml git commit -m "chore(release): bump version to $new_version" git push origin main
5. 发布至pub.dev
最后执行发布命令(需提前配置pub.dev的认证信息):
flutter pub publish --force
注意:需确保Azure Pipeline服务账号拥有仓库的提交权限,且pub.dev的认证密钥已通过流水线变量配置。
内容的提问来源于stack exchange,提问作者Mohamed BEN HALIMA
相关产品推荐
相关产品推荐

