Search code examples
bashazure-devopsscriptingazure-pipelines

Why is my Azure Pipeline not able to run a basic Git add


I'm trying to commit a change that I make to a build file in my build process. This job runs on MacOS as a Bash task. All sorts of other scripts and tasks are working correctly, but trying to execute these git commands is giving back a 127 error.

- 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'))

What I get back is the following error(s):

========================== 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'.

Pretty vague, but here are some things I've tried:

  - 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'))
  • Making sure to checkout the code first. I've used some mostly pre-defined Git steps for this, but that git checkout script works dandy.
  • Running a very similar add on the same file on my machine locally. The casing for that path needed to be fixed, but then it worked.
  • Using echo path to determine if Git is even in my path, as you can see before the commands. Looks like it's not, but then, wouldn't it be saying git itself is not a recognised command, rather than add, commit and push?
  • I've tried Googling error code 127 and using Git on a MacOS agent in Azure, but to no avail. People don't seem to be having quite the same problem as me.

Any help would be most pleasant!


Solution

  • The reason this wasn't working is because I copied the script from Microsoft Teams. This was a big mistake. It seemed to be adding invisible characters which were causing my error messages. So the moral of the story, always paste it into an editor where you can see invisible characters first. And hopefully don't copy it from Teams in the first place!