防止在启动Docker容器时Gitlab将-NoProfile添加到PowerShell

时间:2020-10-02 08:51:12

标签: powershell docker xamarin msbuild gitlab

1。创建Docker容器时如何防止Gitlab添加-NoProfile?

2。将Powershell与-NoProfile和不与-一起使用有什么区别?

3。如果我运行powershell -NoProfile以某种方式加载/运行默认配置文件并恢复设置-NoProfile标志的效果,有什么办法吗?

这些问题背后的故事:

在Gitlab使用Powershell -NoProfile启动的Docker容器中使用MSBuild(Visual Studio构建工具命令)时,无法构建Xamarin应用。

我为CI创建了一个docker镜像,如果我手动运行该容器,一切都会正常工作,但是当它在Gitlab Runner运行期间运行时,它会失败(更确切地说,msbuild / t:SignAndroidPackage会失败,因为某些文件确实无法生成)。我检查了一下(在gitlab-ci.yml中睡了1个小时,并连接到运行器计算机上的容器),发现Gitlab使用PowerShell -NoProfile启动了容器... -NoProfile),我转载了该问题。

错误是:

Could not find a part of the path 'C:\builds\gitlab\HMI.Framework\KDI\sub\HostApplicationToolkit\sub\KBle\KBle\sub\XamarinBluetooth\Source\Plugin.BLE.Android\Resources\Resource.Designer.cs'

这里缺少Resource.Designer.cs(应该在构建过程中自动生成)

这是dockerFile:

# escape=`

# Use the latest Windows Server Core image with .NET Framework 4.8.
FROM mcr.microsoft.com/dotnet/framework/sdk:3.5-windowsservercore-ltsc2019

ENV VS_STUDIO_INSTALL_LINK=https://download.visualstudio.microsoft.com/download/pr/befdb1f9-8676-4693-b031-65ee44835915/c541feeaa77b97681f7693fc5bed2ff82b331b168c678af1a95bdb1138a99802/vs_Community.exe
ENV VS_INSTALLER_PATH=C:\TEMP\vs2019.exe
ENV ANDROID_COMPILE_SDK=29 

# Restore the default Windows shell for correct batch processing.
SHELL ["cmd", "/S", "/C"]

ADD $VS_STUDIO_INSTALL_LINK $VS_INSTALLER_PATH
RUN %VS_INSTALLER_PATH% --quiet --wait --norestart --nocache --includeRecommended --includeOptional`
   --add Microsoft.VisualStudio.Workload.NetCrossPlat `
   --add Microsoft.VisualStudio.Workload.XamarinBuildTools `
 || IF "%ERRORLEVEL%"=="3010" EXIT 0 
RUN del %VS_INSTALLER_PATH%
 
# set some util paths
RUN setx JAVA_HOME "c:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\"
RUN setx path "%path%;c:\Program Files (x86)\Android\android-sdk\tools\bin;c:\Program Files (x86)\Android\android-sdk\platform-tools"

# update android SDK with API 29
RUN echo y| sdkmanager "platforms;android-%ANDROID_COMPILE_SDK%"

#ENTRYPOINT ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]

这是gitlab-ci.yml

image: visualstudio2019-xamarin-ci:1.0-windowsservercore-ltsc2019

stages:
  - build
  - test
  
variables:
  GIT_SUBMODULE_STRATEGY: 'recursive'


build_kdi:
   stage: build
   only:
    - CI_CD
   tags:
    - docker-windows
   script:
    - '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" ./src/KDI/KDI.sln /t:Restore /p:AndroidBuildApplicationPackage=true /p:Configuration=Release'
    - '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" /p:Configuration=Release /t:SignAndroidPackage /property:SolutionDir="C:\builds\gitlab\HMI.Framework\KDI\src\KDI\" /p:AndroidSigningKeyStore="C:\builds\gitlab\HMI.Framework\KDI\distribution\Android\KrohneAndroidKeystore.keystore" .\src\KDI\KDI\KDI.Android\KDI.Android.csproj'

    
run_bb_tests:
   stage: test
   only:
    - CI_CD
   tags:
    - docker-windows
   script:
    - docker-ci/install_and_run_bb_tests.bat "phone_ip" "5555" arg1 arg2 || true
    - adb pull /storage/emulated/0/Android/data/myApp/files/Exports/BBT_Exports/BBTests_CI C:\test_results
   artifacts:
    paths:
     - c:\test_results\BBTests_CI #expire in 1 month by default

如果我使用以下命令启动此映像: docker run -it myImage powershell 一切正常,但是如果我运行 docker run -it myImage powershell -NoProfile ,则msbuild失败在构建xamarin应用程序的某个步骤中

1 个答案:

答案 0 :(得分:1)

  1. 在创建docker容器时如何防止Gitlab添加-NoProfile?

显然您无法... but there is a feature request in place

  1. 将Powershell与-NoProfile和不与-一起使用有什么区别?

PowerShell配置文件只是一个文件,其中包含特定用户的内容,例如自定义功能。例如,如果我要在我的PowerShell配置文件中使用Function Banana(){Write-Host "Awesome"},例如$ Home [My] Documents \ PowerShell \ Profile.ps1。每当我打开PowerShell时,它都会自动加载该功能。因此,我可以打开PowerShell,键入banana,并将字符串Awesome写入stdout。如果我要使用-NoProfile启动powershell,然后输入banana,我将得到CommandNotFoundExceptionsource

  1. 如果我运行powershell -NoProfile以某种方式加载/运行默认配置文件并恢复设置-NoProfile标志的效果,有什么办法吗?

也许。我只说也许是因为我还没有亲自测试过,但是从理论上讲它应该可以工作。如果配置文件位于容器中,那么您要做的就是将其作为点源。

. "C:\path\to\powershell\profile.ps1"

.ps1文件的源采购将在当前范围内在其中运行任何文件,包括加载功能(如果存在)。 dot source docs

我在Gitlab方面的专业知识有限,但是由于我在Windows容器,PowerShell方面的总体经验以及PowerShell配置文件的性质,我怀疑分析是此问题的根本原因,但我没有有足够的信息/证据可以肯定地说。希望这会有所帮助!