我尝试在 Windows Build Server(Windows Server 2022 Datacenter Azure Edition)上使用 Arduino CLI 来编译一些 Arduino 代码。我的 Arduino 代码托管在 Github 中,并且配置了一个 YAML 文件/Github Action,以便每当我合并到目标分支时都会在此 Windows 服务器上启动构建。
每当我在 Github Actions 中执行涉及 Arduino CLI 的步骤时,我都会看到:
Unable to get Documents Folder: The system cannot find the file specified.
此屏幕截图显示了我安装 ESP32 Core 库的步骤,但涉及
arduino-cli
的所有其他步骤都会产生相同的消息。
我知道 arduino-cli 正在尝试查找我的文档文件夹。当我手动连接到构建服务器并以本地管理员身份执行 arduino-cli 命令时,我确实没有看到此消息。显然,我的 Github Action 正在以
nt authority/system
身份运行,因此 arduino-cli 在以此用户身份运行时无法找到 Documents 文件夹是有道理的。我的问题是:有没有办法手动为 Arduino-CLI 指定“Documents”文件夹?如果没有,有没有办法以不同的用户身份执行?
我正在运行 64 位 Arduino CLI,版本 1.0.1。
我找到了上述两个问题的一些答案。
首先,关于是否可以指定一个文件夹作为“文档”,事实证明Arduino CLI是开源的。具体看这个代码块,似乎Arduino CLI使用
win32.GetDocumentsFolder()
来获取文件夹。我注意到“另一个文件”似乎使用了环境变量,但我无法让它工作。除此之外,我不确定“文档”问题是否是使用系统用户的唯一副作用。因此,我决定以不同的用户身份执行脚本。
要以其他用户身份执行,我使用 New-PSSession
和
Invoke-Command
在 YAML 文件中运行 Arduino CLI 脚本。例如: - name: Run Arduino CLI Installation scripts
shell: powershell
run: |
$password = "${{ secrets.PASSWORD }}" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList "MyDomain\MyUser",$password
$MySession = New-PSSession -Credential $cred
Invoke-Command -Session $MySession -ScriptBlock {
whoami
arduino-cli core update-index --config-file arduino-cli.yaml
arduino-cli lib install [email protected]
arduino-cli core install esp32:[email protected]
cd C:\actions-runner\_work\MyDir
arduino-cli compile -b esp32:esp32:esp32s2 --output-dir .\bin
}
请注意,我将密码存储在 Github 中,以避免在脚本本身中暴露密码(即我的 Github 存储库
> 设置 >“秘密和变量”> 操作 > 秘密)