Flutter Jenkins flutter 未发现问题

问题描述 投票:0回答:1

我目前正在尝试使用 Jenkins 构建 Flutter 应用程序。对于初始设置,我有一个主从设置,其中从机是一台将创建构建的 Mac 计算机(从机也已连接)。

下面是管道脚本:


pipeline {
    agent {
        label "macos16Node"
    }
     environment {
        flutter = " /Users/username/Desktop/flutter"
    }


    stages {
        stage ('Flutter Doctor') {
            steps {
                withEnv(['PATH+EXTRA=/opt/flutter/bin']){
                    sh '$flutter doctor -v'
                }
               
            }
           
        }
        stage('GIT PULL') {
            steps {a
                git branch: 'main', credentialsId: 'github_token', url: ‘mygitrepo’
            }
        }
        stage('TEST') {
            steps {
                
                    sh ‘$flutter test'
                
                
            }
        }
        stage('BUILD') {
            steps {
                sh '''
                  #!/bin/sh
                  flutter build apk --debug
                  '''
            }
        }
        
    }
}

每当我构建管道时,都会出现错误。

以下是控制台输出:

[Pipeline] Start of Pipeline
[Pipeline] node
Running on slaveNode in /var/root/workspace/first pipeline
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Flutter Doctor)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ /Users/user/Desktop/flutter doctor -v
/var/root/workspace/first pipeline@tmp/durable-f6dbaa4a/script.sh: 1: /Users/user/Desktop/flutter: not found

我也没有在 Jenkins 的环境变量中设置任何路径。为什么它没有检测到 Flutter,我需要为此进行额外的设置吗?

flutter dart jenkins jenkins-pipeline
1个回答
2
投票

快速解答

原因:

flutter 二进制文件不在 jenkins shell 的 PATH 中

修复:

/tmp/workspace/flutter 等任意位置下载 flutter 并覆盖环境中的 PATH。

詹金斯 2.375.2

pipeline {
    agent any
    environment {
      PATH = "$PATH:/tmp/workspace/flutter/bin"
    }    
    stages {
        stage('Setup') {
            steps {
                print "${env.PATH}"
            }    
        }
        
        stage('Build') {
            steps {
                sh "flutter doctor -v"
            }
        }        
    }
}

输出

enter image description here

详细解答

Jenkins 没有魔法,如果它在 shell 中工作,那么它也可以与 Jenkins 一起工作

步骤1:将flutter添加到shell PATH

在 jenkins 将执行构建命令的机器中,尝试命令

flutter doctor -v
。你会看到这个错误:

enter image description here

类似于詹金斯错误

enter image description here

所以,用snap安装flutter

sudo snap install flutter --classic

或使用curl下载

curl https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.7.3-stable.tar.xz -o /foo/bar/flutter.tar.xz
apt-get install xz-utils
tar xf /foo/bar/flutter.tar.xz

您应该拥有这些文件

ls -la /foo/bar/flutter

enter image description here

最后需要将 flutter 添加到 shell PATH 变量中。你可以使用java风格

export FLUTTER_HOME=/foo/bar/flutter
export PATH=$PATH:$FLUTTER_HOME/bin

enter image description here

如果你运行命令,它就会起作用

enter image description here

步骤 2:将 flutter 添加到 jenkins PATH

对于声明式管道,由于 PATH 是一个特殊环境变量,因此修改它的唯一方法是:

environment {
  PATH = "$PATH:/foo/bar/flutter/bin"
}   

类似于主机 shell 示例

PATH=$PATH:$FLUTTER_HOME/bin

之后,您可以在管道的任何位置使用 flutter

对于脚本化管道,这两个选项对我有用:

#1 修改PATH变量

node { print "${env.PATH}" def FLUTTER_HOME = "/tmp/workspace/flutter" env.PATH = env.PATH+":$FLUTTER_HOME/bin" print "${env.PATH}" sh "flutter doctor -v" }

#2 withEnv PATH+EXTRA

node { def FLUTTER_HOME = "/tmp/workspace/flutter" withEnv(["PATH+EXTRA=$FLUTTER_HOME/bin"]){ sh 'flutter doctor -v' } sh "flutter doctor -v" }
这个最新选项的唯一问题是 flutter 只能在 

withEnv{...} 范围内工作。

enter image description here

参考文献

  • https://docs.flutter.dev/get-started/install/linux
  • https://www.geeksforgeeks.org/how-to-install-flutter-on-linux/
  • https://www.cyberithub.com/how-to-install-flutter-on-ubuntu-20-04-lts-focal-fossa/
  • https://www.freecodecamp.org/news/how-to-install-and-setup-flutter-on-ubuntu/
  • 如何在 Jenkins 声明式管道中设置 PATH
  • 无法覆盖 Jenkins 中的 env PATH 变量
  • https://gist.github.com/thinhdanggroup/90528c34e4fd4c9e1c9a945f9b88d89c
  • https://dsstream.com/declarative-vs-scripted-pipeline-key-differences/
© www.soinside.com 2019 - 2024. All rights reserved.