RamblingCookieMonster / BuildHelpers

Helper functions for PowerShell CI/CD scenarios
MIT License
214 stars 47 forks source link

Get-BuildVariable fails on VSTS Hosted Agent #100

Closed joranm closed 5 years ago

joranm commented 5 years ago

Platform/Agent Azure DevOps/Hosted VS2017

Executing (Get-BuildVariable).BuildSystem

Results in

2019-01-29T13:56:40.5200748Z ##[section]Starting: Run tests
2019-01-29T13:56:40.5551552Z ==============================================================================
2019-01-29T13:56:40.5551629Z Task         : PowerShell
2019-01-29T13:56:40.5551683Z Description  : Run a PowerShell script on Windows, macOS, or Linux.
2019-01-29T13:56:40.5551742Z Version      : 2.140.2
2019-01-29T13:56:40.5551778Z Author       : Microsoft Corporation
2019-01-29T13:56:40.5551836Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
2019-01-29T13:56:40.5551873Z ==============================================================================
2019-01-29T13:56:41.9698156Z Generating script.
2019-01-29T13:56:42.0692862Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\3caeb402-476f-4939-831d-8ef6f412e707.ps1'"
2019-01-29T13:56:43.7073590Z Invoke-Git : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Message'. Specified 
2019-01-29T13:56:43.7073984Z method is not supported.
2019-01-29T13:56:43.7074326Z At C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.7\Public\Get-BuildVariable.ps1:180 char:17
2019-01-29T13:56:43.7074643Z + ...             Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $(  ...
2019-01-29T13:56:43.7074919Z +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2019-01-29T13:56:43.7075323Z     + CategoryInfo          : InvalidArgument: (:) [Invoke-Git], ParameterBindingException
2019-01-29T13:56:43.7075752Z     + FullyQualifiedErrorId : CannotConvertArgument,Invoke-Git
2019-01-29T13:56:43.7075935Z  
2019-01-29T13:56:43.8439576Z ##[error]PowerShell exited with code '1'.
2019-01-29T13:56:43.8854289Z ##[section]Finishing: Run tests
peppekerstens commented 5 years ago

confirmed

workaround; I tried using version 1.1.4 of BuildHelpers. This version works. Looking at the code I can only see this difference within Get-BuildVariable(s).ps1;

1.1.4

        'BUILD_SOURCEVERSION' {
            if($WeCanGit)
            {
                Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )"
                break
            } # VSTS (https://www.visualstudio.com/en-us/docs/build/define/variables#)
        }

2.0.7

        'SYSTEM_TEAMPROJECT' {
            if($WeCanGit)
            {
                Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )"
                break
            } # VSTS (https://www.visualstudio.com/en-us/docs/build/define/variables#)
        }

I did not try/test later versions (used 1.1.4 as a fixed version in builds until now)

As of yet, it is not clear to me why the error pops up;

  1. as far as i can tell the logic in both cases only checks on the existence of a certain environment variable. Both exist in VSTS/AzureDevOps. There is no other obvious difference in code...
  2. The error message itself makes no sense at all; Invoke-Git does not have a Message parameter...?!?
devblackops commented 5 years ago

I get this same error on a hosted Windows 2016 agent:

Invoke-Git : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Message'. Specified 
method is not supported.
At C:\Users\VssAdministrator\Documents\WindowsPowerShell\Modules\BuildHelpers\2.0.7\Public\Get-BuildVariable.ps1:180 
char:17
+ ...             Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $(  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Git], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Invoke-Git

I DO NOT get this error when the same build script is run on the hosted macos agent.

AOsborn commented 5 years ago

The error is with the Invoke-Git from the SYSTEM_TEAMPROJECT environment variable. The git command gives an error and Line 149 of Invoke-Git has it sending an array/object to Write-Error, which is invalid. This results in the "Cannot convert 'System.Object[]' to the type 'System.String'" exception.

The -split on Line 126 converts the String to a String[], which would also throw an error when written out, then the "Where-Object {$_}" on the same line converts it to an Object[].

I think changing line 149 to allow arrays will fix this: $stderr | Write-Error

The git error being thrown is a separate issue, here's the error: fatal: ambiguous argument 'TestProject': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'

The command Invoke-Git runs which generates the error is this: git.exe log --format=%B -n 1 TestProject

AOsborn commented 5 years ago

I should have looked at open PRs. PR #101 fixes the "Cannot convert" error. As a workaround to the git error I edited line 184 of Get-BuildVariable to remove the environment variable output: Invoke-Git @IGParams -Arguments "log --format=%B -n 1"

FISHMANPET commented 5 years ago

I have a maybe related issue, running on a VSTS hosted VS2017 agent with GitHub Enterprise (rather than regular GitHub) as a source, and I get the following error from Set-BuildEnvironment with no parameters

Invoke-Git : fatal: ambiguous argument 'RepoName': unknown revision or path not in the working tree.
At C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.8\Public\Get-BuildVariable.ps1:184 char:17
+ ...             Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $(  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-Git

It's executing line 184 which tells me it's in the 'SYSTEMTEAMPROJECT' switch. I'm not sure what the Environment is supposed to represent at that point, `"$( (Get-Item -Path "ENV:$").Value )"run on my local console is just the entire Environment, so I'm not sure what it's supposed to represent in this switch. It's accurately showing my Git Repo name in the ambiguous argument message. I know that's some kind of Git message but I'm not exactly sure what could be different here vs running a hosted VSTS agent with public Github, which I've been able to do successfully with the sameSet-BuildEnvironment` command

FISHMANPET commented 5 years ago

Nope I just built a project from public github and the same issue above happened, I'm going to open a new issue on that since I think it's slightly different from this one?