我正在尝试提交对构建过程中的构建文件所做的更改。此作业在 MacOS 上作为 Bash 任务运行。所有其他脚本和任务都正常工作,但尝试执行这些 git 命令会返回 127 错误。
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo $PATH
git add $(System.DefaultWorkingDirectory)/MyFilePath/my.file
git commit -m "update file [skip ci]"
git push origin
workingDirectory: '$(Build.SourcesDirectory)'
displayName: 'Git commit'
continueOnError: true
condition: and(succeeded(), eq(variables.ChangeFile, 'true'))
我得到的是以下错误:
========================== Starting Command Output ===========================
##[debug]which '/bin/bash'
##[debug]found: '/bin/bash'
##[debug]/bin/bash arg: /Users/runner/work/_temp/7d35e773-5913-44d8-b4fd-0d22787e7680.sh
##[debug]exec tool: /bin/bash
##[debug]arguments:
##[debug] /Users/runner/work/_temp/7d35e773-5913-44d8-b4fd-0d22787e7680.sh
/bin/bash /Users/runner/work/_temp/7d35e773-5913-44d8-b4fd-0d22787e7680.sh
/Users/runner/hostedtoolcache/NuGet/6.2.1/x64:/usr/local/lib/ruby/gems/2.7.0/bin:/usr/local/opt/[email protected]/bin:/usr/local/opt/pipx_bin:/Users/runner/.cargo/bin:/usr/local/opt/curl/bin:/usr/local/bin:/usr/local/sbin:/Users/runner/bin:/Users/runner/.yarn/bin:/Users/runner/Library/Android/sdk/tools:/Users/runner/Library/Android/sdk/platform-tools:/Users/runner/Library/Android/sdk/ndk-bundle:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/usr/bin:/bin:/usr/sbin:/sbin:/Users/runner/.dotnet/tools:/Users/runner/.ghcup/bin:/Users/runner/hostedtoolcache/stack/2.7.5/x64
git: 'add /Users/runner/work/1/s/MyFilePath/my.file' is not a git command. See 'git --help'.
/Users/runner/work/_temp/7d35e773-5913-44d8-b4fd-0d22787e7680.sh: line 3: git commit -m update file [skip ci]: command not found
/Users/runner/work/_temp/7d35e773-5913-44d8-b4fd-0d22787e7680.sh: line 4: git push origin: command not found
##[debug]Exit code 127 received from tool '/bin/bash'
##[debug]STDIO streams have closed for tool '/bin/bash'
##[error]Bash exited with code '127'.
相当模糊,但这里有一些我尝试过的事情:
- checkout: self
persistCredentials: true
clean: true
displayName: Checkout from git
- script: git checkout $(Build.SourceBranchName)
workingDirectory: $(Build.SourcesDirectory)
displayName: Checkout branch from git to receive commits on
failOnStderr: false
condition: and(succeeded(), eq(variables.ChangeFile, 'true'))
任何帮助将是最愉快的!
要调用 git add,不是需要做一些事情吗?:
代理的当前目录不需要是git repo文件夹,即包含“.git”文件夹的文件夹吗?您没有表明您的脚本可以确保或验证这一点。
AFAICT,git add 的路径规范不允许像“/Users/runner/work/1/s/MyFilePath/my.file”示例那样使用前导斜杠。我很难找到路径规范的好文档,但我找不到任何使用前导斜杠的示例。我建议在本地计算机上尝试 git add 。
从问题输出来看,git 似乎将整个后者视为命令参数。我认为问题出在您的操作系统类型中的
$(System.DefaultWorkingDirectory)
(您基于Mac操作系统)。
这应该有效:
trigger:
- none
pool:
vmImage: 'windows-latest'
steps:
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
from encodings import utf_8
import os
rootpath = "./MyFilePath"
dictpath = "./MyFilePath2"
for filename in os.listdir(rootpath):
with open(rootpath+'/'+filename, encoding='gb2312') as f:
with open(dictpath+'/'+filename, "w+", encoding='utf-8') as f1:
for line in f:
f1.write(line)
- task: Bash@3
inputs:
targetType: 'inline'
script: |
git config --global user.email "[email protected]"
git config --global user.name "Bowman Zhu(In Microsoft Agent)"
git add '$(System.DefaultWorkingDirectory)/MyFilePath2/my.file'
git commit -m 1
成功:
我的回购结构:
这不起作用的原因是我从 Microsoft Teams 复制了脚本。这是一个很大的错误。它似乎添加了不可见的字符,导致了我的错误消息。因此,故事的寓意是,始终将其粘贴到编辑器中,您可以首先看到隐形角色。希望一开始就不要从 Teams 复制它!