使用 azure devops 构建管道

flutter build pipeline with azure devops

我正在尝试使用 azure devops 为我的 flutter 应用创建构建管道。

我使用 Aloïs Deniel 的 flutter 天蓝色扩展。 flutter install 任务顺利通过。 Flutter 构建挂起并在无限循环中抛出以下错误:

 stderr: Cloning into bare repository '/Users/runner/hostedtoolcache/Flutter/1.26.0-1.0.pre-dev/macos/flutter/.pub-cache/git/cache/app_alh_packages-384b2d81da8d887d80ab6f47deedece96035bf0c'...
    fatal: could not read Username for 'https://jointhedartsidewehavewidgets.visualstudio.com': terminal prompts disabled
    exit code: 128
    
    pub get failed (server unavailable) -- attempting retry 1 in 1 second...

我的 azure pipeline.yaml 文件很简单:

variables:
  projectDirectory: 'cleanedBloc'

trigger:
- main

pool:
  vmImage: 'macos-latest'

steps:
- task: FlutterInstall@0
  inputs:
    channel: 'dev'
    version: 'latest'
- task: FlutterBuild@0
  inputs:
    target: 'ios'
    projectDirectory: $(projectDirectory)

我很高兴得到帮助。提前致谢。

看起来 flutter 构建任务正在尝试从 https://jointhedartsidewehavewidgets.visualstudio.com 下载 azure git 依赖项。由于云构建代理没有此 git 存储库的凭据。它会抛出以上错误。

您可以查看以下解决方法来解决此问题。

1,在 git url 中添加 git 存储库凭据,这在您的 pubspec.yaml 文件中定义。见下文:

name: FlutterProject
environment:
  sdk: ">=2.0.0 <3.0.0"

dependencies:            
  flutter:            
    sdk: flutter            
  cupertino_icons: 0.1.2      
  Yourpackage:
     git: 
       url: https://user_name:password@jointhedartsidewehavewidgets.visualstudio.com/yourProject/_git/yourRepo
       ref: master      

或者您可以使用 Personal access tokenCode read scope 作为凭据。

Yourpackage:
         git: 
           url: https://{Personal Access Token}@jointhedartsidewehavewidgets.visualstudio.com/yourProject/_git/yourRepo
           ref: master  

如果您不想在 pubspec.yaml 文件中公开您的个人访问令牌。您可以创建一个管道秘密变量来保存 PAT。并添加替换令牌任务以将 PAT 添加到 pubspec.yaml 文件。

参见下面的示例:如下更改您的 pubspec.yaml:

 Yourpackage:
             git: 
               url: https://#{token}#@jointhedartsidewehavewidgets.visualstudio.com/yourProject/_git/yourRepo
               ref: master

在您的管道中定义 secret variable

添加 replace token task 以用 PAT 替换 #{token}#

- task: qetza.replacetokens.replacetokens-task.replacetokens@3
  displayName: 'Replace tokens in pubspec.yaml'
  inputs:
    targetFiles: pubspec.yaml   

- task: FlutterInstall@0