powershell 在gitlab中使用空格封装字符串ci yaml

0sgqnhkj  于 4个月前  发布在  Shell
关注(0)|答案(3)|浏览(52)

如何在GitLab CI YAML文件中指定包含空格的字符串文字?
例如,我使用如下方式调用gitlab-ci.yml文件中位于C:\Program Files(x86).\msbuild.exe的msbuild.exe

build:
   - C:\Program Files (x86)\...\msbuild.exe MySolution.sln

字符串
我尝试使用单引号和双引号来封装路径,但lint失败。
有什么想法吗?谢谢

qnyhuwrf

qnyhuwrf1#

这就是诀窍:

- . 'C:\Program Files (x86)\{...}'

字符串
请注意,PowerShell Dot sourcing operator .位于该行的开头。

rggaifut

rggaifut2#

这个link谈到了同样的问题。它已经有3年的历史了,从那时起GitLab改变了很多东西,链接中的答案不再相关。他们改变的事情之一是,当runner设置为shell类型时,它现在使用Windows PowerShell而不是shell.exe。这里有一个适合我的解决方案的例子:
1.准备一个build.bat文件,类似这样:

`"C:\Program Files (x86)\Microsoft Visual 
Studio\2017\Enterprise\MSBuild\15.0\Bin\amd64\MSBuild.exe" some_solution.sln /m 
/t:Build /p:Configuration=Release /p:Platform=x64`.

字符串
把它作为回购的一部分。
1.让.gitlab-ci.yml文件看起来像这样:

build_job:
  script: .\build.bat

mwg9r5ms

mwg9r5ms3#

你通常会这样做(env是可选的,但改进readability):

build:
  variables:
    DUMP_ENV: 'env:'
    vswhere: 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe'
  stage: build
  script:
    - dir ${DUMP_ENV}
    - . $env:vswhere -products *

字符串
以下是无效的YAML,即使这将是validpowershell语法:

- & $env:vswhere -products *


请考虑:

> "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products *
At line:1 char:72
+ ... iles (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products *
+                                                               ~~~~~~~~~
Unexpected token '-products' in expression or statement.
At line:1 char:82
+ ... iles (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products *
+                                                                         ~
Unexpected token '*' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken


同时:

> & "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products *
Visual Studio Locator version 2.7.1+180c706d56 [query version 2.11.65.22356]
Copyright (C) Microsoft Corporation. All rights reserved.

instanceId: 43cea1ed
installDate: 2/15/2021 10:35:51 AM
installationName: VisualStudio/16.11.19+32901.82
installationPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise
...


如果你使用了windows语法would have been的默认shell:

variables:
  nuget: 'c:\nuget\nuget.exe'
  msbuild: '"c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe"'

compile:
  stage: build
  script:
    - '%nuget% restore'
    - '%msbuild% ...'


注意,对于env变量,不能简单地使用:用途:

build:
  variables:
    vswhere: '"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"'


简单地说,因为ProgramFiles(x86)将被计算为before,所以该元素被加载。所以env var将导致:

vswhere                       "\Microsoft Visual Studio\Installer\vswhere.exe"


在这种情况下,您必须在脚本步骤中定义env变量:

build:
  stage: build
  script:
    - $env:vswhere="${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
    - . $env:vswhere -products *

相关问题