I'm currently working on setting up automatic versioning for a Flutter package using Azure Pipeline. However, I'm facing a challenge regarding how to access the pubspec.yaml file from the azure-pipelines.yml file in order to update the version attribute.
What I aim to achieve is that when a pull request is merged with the main branch, the pipeline triggers and publishes the package to pub.dev. While I've been able to accomplish these tasks, I'm struggling with implementing automatic versioning for the package based on conventional commits and semantic versioning.
Does anyone have any ideas or suggestions on how to achieve this?
You can try like as below to write the newly generated version number into the pubspec.yaml
file of your Flutter project:
At first, ensure the pubspec.yaml
file is existing in directory of your Flutter project, so that it can be checked out together with the code files.
Use a placeholder token (such as {{package_version}}
) to set the value of 'version
' attribute in the pubspec.yaml
file. For example.
# pubspec.yaml
name: MyFlutterPackage
description: My Flutter package.
version: {{package_version}}
. . .
In the pipeline, set a pipeline variable to store/pass the newly generated version number. This variable should have the same name (package_version
) with the placeholder token in the pubspec.yaml
.
In the pipeline, before the steps to build and publish the Flutter package, add a Replace Tokens task to replace the placeholder token with the value of the pipeline variable in the pubspec.yaml
. The task is from the extension "Replace Tokens", and you need to install this extension into your Azure DevOps organization before you can use it in pipelines.
variables:
package_version: <new version number>
steps:
. . .
- task: qetza.replacetokens.replacetokens-task.replacetokens@5
displayName: 'Set version number'
inputs:
targetFiles: pubspec.yaml
tokenPattern: doublebraces
keepToken: true
actionOnNoFiles: warn
. . .
By this way, the pipeline will build and publish the Flutter package with the updated pubspec.yaml
file which has the new version number set in it.
EDIT:
If you cannot install and use the "Replace Tokens" extension in your Azure DevOps organization, you can use one of the following PowerShell commands to replace the placeholder token in the pubspec.yaml
file:
Use the PowerShell -replace
operator.
((Get-Content -Path path/to/pubspec.yaml) -replace "{{package_version}}", "$(package_version)") | Set-Content -Path path/to/pubspec.yaml
Get-Content
' cmdlet to read the content of the pubspec.yaml
file.-replace "{{package_version}}", "$(package_version)"
" to relace "{{package_version}}
" with the value of the pipeline variable (package_version
) in the content.Set-Content
' cmdlet to write the updated content into the pubspec.yaml
file to override the old content.Use the sed
command.
sed -i -e 's/\{\{package_version\}\}/$(package_version)/g' path/to/pubspec.yaml