1

I am having difficulty understanding the syntax of yaml. I want to convert the below power shell commands for restoring nuget packages and building visual studio solution to yaml. I am not able to get it correct, pls help.

PowerShell Commnds:

$ProjectPath = "e:\GitExperiment\SGen"
Set-Location $ProjectPath
$MSBuildPath = "C:\Program Files (x86)\Microsoft VisualStudio\2019\BuildTools\MSBuild\Current\Bin"
$NugetExePath = "C:\Program Files (x86)\NuGet\nuget.exe"
& $NugetExePath restore $ProjectPath\SGen.sln
& $MSBuildPath\MSBuild.exe $ProjectPath\SGen.sln /t:Build /p:Configuration=Release /p:TargetFramework=v4.7.2  /p:SkipPostSharp=True /p:RunCodeAnalysis=False

YAML

stages:
    - BUILD
    - UNITTEST
    - DEPLOY
BUILD_RestoreNugetPackages:
    script:
        - '$ProjectPath = e:\GitExperiment\SGen"'
        - 'Set-Location $ProjectPath'
        - '$MSBuildPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe"'
        - '$NugetExePath = "C:\Program Files (x86)\NuGet\nuget.exe"'
        -  '"$NugetExePath restore $ProjectPath\SGen.sln"'
    stage: BUILD
    tags:
        - ci
BUILD_SolutionBuild:
    script:
        - "& $MSBuildPath $ProjectPath\\SGen.sln /t:Build /p:Configuration=Release /p:TargetFramework=v4.7.2  /p:SkipPostSharp=True /p:RunCodeAnalysis=False"
    stage: BUILD
    tags:
        - ci

I tried using quotes and double quotes and escaping characters in yaml. But couldn't get the commands right. Please help.

manjuv
  • 175
  • 3
  • 12

1 Answers1

1

While a gitlab-ci runner does support Powershell, I would rather:

  • put those commands in a script (versioned with the rest of the sources)
  • call that script from your YAML directives

    powershell -noprofile -noninteractive -executionpolicy Bypass -file my-script.ps1
    

That way, you don't have to deal with trying to escape any special characters.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for the answer. As u suggested I created three ps1 files with the commands to restore nuget, build and deploy. Placed them in the same folder on git as that of the yml file. But I get the below error. – manjuv Nov 14 '19 at 13:36
  • $ powershell -noprofile -noninteractive -executionpolicy Bypass -command RestoreNuget.ps1 RestoreNuget.ps1 : The term 'RestoreNuget.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + RestoreNuget.ps1 + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (RestoreNuget.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – manjuv Nov 14 '19 at 13:36
  • @manjuv Sorry, I have edited my answer: this should be `-file`, not `-command` (see https://gist.github.com/ishu3101/0bdb1ec60a1371b07414bd0ab28f4227 or https://stackoverflow.com/a/44429681/6309 as an example) – VonC Nov 14 '19 at 15:28