首页 > 解决方案 > 如何将 powershell exe 调用转换为 git CI/CD 的 YAML

问题描述

我很难理解 yaml 的语法。我想将以下用于恢复 nuget 包和构建 Visual Studio 解决方案的 power shell 命令转换为 yaml。我无法正确解决,请帮忙。

PowerShell 命令:

$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

我尝试在 yaml 中使用引号和双引号以及转义字符。但无法正确执行命令。请帮忙。

标签: powershellgitlabyamlgitlab-cidevops

解决方案


虽然gitlab-ci 跑步者确实支持 Powershell,但我宁愿:

  • 将这些命令放在脚本中(与其余源一起版本化)
  • 从您的 YAML 指令中调用该脚本

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

这样,您就不必处理试图转义任何特殊字符的问题。


推荐阅读