How to convert powershell exe calls to YAML for git CI/CD - powershell

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.

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.

Related

cannot find sqlpackage in Azure pipelines Microsoft hosted agent

I want to use sqlpackage in a Powershell task within an Azure pipeline. I'm using a Microsoft hosted windows-2022 agent.
When I run sqlpackage inside the Powershell task I get:
sqlpackage : The term 'sqlpackage' is not recognized as the name of a cmdlet, function, script file, or operable program
According to the Microsoft documentation, sqlpackage is included in the Microsoft-hosted Windows agents but I can't figure out where is sqlpackage located in the agent.
I tried running this script to determine the sqlpackage version from the Microsoft documentation but it doesn't work:
- script: sqlpackage.exe /version
workingDirectory: C:\Program Files\Microsoft SQL Server\160\DAC\bin\
displayName: 'get sqlpackage version'
And I tried a different file path that a StackOverflow post used and it didn't work either:
- script: sqlpackage.exe /version
workingDirectory: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\150\
displayName: 'get sqlpackage version'
Both instances I received a not found workingDirectory error.
How do I access sqlpackage and other tools that come pre-installed on Microsoft-hosted agents?
When you use Windows-2022 agent, the sqlpackage.exe indeed locate in the path: C:\Program Files\Microsoft SQL Server\160\DAC\bin\
If you are using PowerShell task, you need to run the following script to use the sqlpackage.exe: .\SqlPackage.exe argument
- powershell: '.\SqlPackage.exe /version'
workingDirectory: 'C:\Program Files\Microsoft SQL Server\160\DAC\bin\'
displayName: 'PowerShell Script'
The script in the doc:Azure Pipelines are for the Command Line task in Azure Pipeline.

GitLab CI Runner Windows executes Powershell command differently to local Powershell

I have a Powershell command that starts a program with some arguments and waits for the execution to end.
If I execute that command manually in Powershell everything works as expected.
If I execute the same command through a GitLab Runner on the same machine, the process is never actually started and the CI job succeeds immediately.
The shell command:
Start-Process -Passthru -FilePath "C:\Program Files\CODESYS 3.5.17.30\CODESYS\Common\CODESYS.exe" -ArgumentList "--runscript='codesys_automation.py' --profile='CODESYS V3.5 SP17 Patch 3' --noUI" | Wait-Process
The gitlab-ci.yml job (executed by a runner with access to the codesys.exe):
configure-plc:
tags:
- codesys
stage: configure-plc
script:
- Start-Process -Passthru -FilePath "C:\Program Files\CODESYS 3.5.17.30\CODESYS\Common\CODESYS.exe" -ArgumentList "--runscript='codesys_automation.py' --profile='CODESYS V3.5 SP17 Patch 3' --noUI" | Wait-Process
How do I make the runner behave just like the manual Poweshell and let it print out the programms stdout output?
Try this
configure-plc:
tags:
- codesys
stage: configure-plc
script:
- '& "C:\Program Files\CODESYS 3.5.17.30\CODESYS\Common\CODESYS.exe" --runscript=codesys_automation.py --profile=CODESYS V3.5 SP17 Patch 3 --noUI'

Running cmd /c from PowerShell with spaces in filepath

I am trying to run the following command in PowerShell
PS C:\Users\Administrator> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat && nuget restore && msbuild mywebapp.sln /p:DeployOnBuild=true /p:PublishedProfile=ServerFolderProfile"
This produces the error
'C:\Program' is not recognized as an internal or external command.
My paths have spaces and I'm running several commands seperated by && which is messing everything up. I have tried putting quotes all over the place but I can't get it to work.
If i run just the first part of the command
cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat"
it works fine. But I can't get the other commands to work too.
According to the docs for cmd.exe:
If /C or /K is specified, then the remainder of the command line is processed as an immediate command in the new shell. Multiple commands separated by the command separator & or && are accepted if surrounded by quotes
So I just had to change my command to:
PS C:\Users\Administrator> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat" "&&" nuget restore "&&" msbuild mywebapp.sln /p:DeployOnBuild=true /p:PublishedProfile=ServerFolderProfile

What is the right syntax for launching an executable in Appveyor?

I'm trying to start the Azure Storage Emulator in an AppVeyor instance but I can't find the right syntax. I've tried this:
- ps: "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start
and this:
- ps: & "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start
and this:
- "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start
All fail with various unhelpful YAML parsing errors.
- cmd: '"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start'
or simple:
- '"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start'
or with PowerShell:
- ps: '& "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start'
Easiest way to avoid YAML parsing errors is to do initial configuration in UI and then press Export YAML. GitHub search for appveyor.yml <something> often brings useful results too.

CSPack and CSRun for running site in azure emulator from powershell

I have spend some time trying to get the cspack and csrun command working to run a website locally in the azure emulator.
So far this is what I get, but its not working
I use psake
Task StartAzureEmulator {
& 'C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.2\bin\cspack' sitename.azure\ServiceDefinition.csdef /out:Sitename.csx /role:sitename;sitename /sites:Vola;Web;Web /copyOnly
& 'C:\Program Files\Microsoft SDKs\Windows Azure\Emulator\csrun' sitename.csx sitename.Azure\ServiceConfiguration.Local.cscfg /useiisexpress /launchbrowser
}
Basically I am not very impressed with how the azure commandline tools works with powershell.
Has anyone got example of this working?
In PowerShell the ; character is a statement separator. You can escape it by preceding it with a backtick or if you are V3 or higher you can user --% to switch PowerShell into a simpler (dumber) parser mode. Try this:
Task StartAzureEmulator {
& 'C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.2\bin\cspack' --% sitename.azure\ServiceDefinition.csdef /out:Sitename.csx /role:sitename;sitename /sites:Vola;Web;Web /copyOnly
& 'C:\Program Files\Microsoft SDKs\Windows Azure\Emulator\csrun' --% sitename.csx sitename.Azure\ServiceConfiguration.Local.cscfg /useiisexpress /launchbrowser
}