Home > Back-end >  Why is my Azure Pipeline not able to run a basic Git add
Why is my Azure Pipeline not able to run a basic Git add

Time:07-15

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!

CodePudding user response:

To call git add, aren't a couple of things required?:

  1. Doesn't the agent's current directory need to be a git repo folder, i.e. a folder containing a ".git" folder? You're not showing that your script ensures or verifies this.

  2. AFAICT, git add's pathspec doesn't allow a leading slash like your "/Users/runner/work/1/s/MyFilePath/my.file" example. I have difficulty finding a good doc for the pathspec, but I can't find any examples using a leading slash. I'd suggest experimenting with git add on your local machine.

CodePudding user response:

From the issue output, It seems that git treats the entire latter as a command parameter. I think the issue comes from the $(System.DefaultWorkingDirectory) in your OS type(You are based on Mac OS).

This should work:

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

Success:

enter image description here

My repo structure:

enter image description here

  • Related