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

标签 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运行器工作期间运行时,它会失败(更确切地说,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,则在构建xamarin应用程序时,msbuild会失败

最佳答案

  1. How can I prevent Gitlab from adding -NoProfile when creating the docker container?

显然您不能... but there is a feature request in place
  1. What's the difference between using powershell with -NoProfile and without it?

PowerShell配置文件只是一个文件,其中包含特定用户的东西,例如自定义函数。例如,如果我要在PowerShell配置文件中添加Function Banana(){Write-Host "Awesome"},例如$ Home [My] Documents \ PowerShell \ Profile.ps1。每当我打开PowerShell时,它都会自动加载该功能。因此,我可以打开PowerShell,输入banana并将字符串Awesome写入stdout。如果我要使用-NoProfile启动powershell并输入banana,我会得到CommandNotFoundException
source
  1. Is there any way if I run powershell -NoProfile to somehow load/run the default profile and to revert the effect of setting -NoProfile flag?

也许。我只说也许是因为我还没有亲自测试过,但是从理论上讲它应该可以工作。如果配置文件位于容器中,则您要做的就是将其作为点源。
. "C:\path\to\powershell\profile.ps1"
点源.ps1文件将在当前作用域内的其中任何文件运行,包括加载功能(如果存在)。
dot source docs
我在Gitlab方面的专业知识有限,但是由于我在Windows容器,PowerShell方面的总体经验以及PowerShell配置文件的性质,我怀疑配置文件是此问题的根本原因,但我没有足够的信息/证据可以肯定地说。希望这会有所帮助!

关于powershell - 防止在启动Docker容器时Gitlab将-NoProfile添加到PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64168705/

相关文章:

python - 我无法为 python 安装 'pip'

powershell - 使用Get-Date创建目录正在制作子文件夹?

powershell - 如何显示 Visual Studio Online 构建任务 Write-Verbose 语句?

Python 脚本只在 Docker 中产生僵尸进程

Xamarin - 当前上下文中不存在名称 'Resource'

ios - 在 Visual Studio 2015 上安装 iOS 应用程序时出现问题抛出 Xamarin Mac Agent

powershell - 在哪里下载Windows PowerShell v2(2.0)RTM?

docker - 无法连接到 unix :///var/run/docker. socks 上的 Docker 守护进程。 docker 守护进程是否正在运行?在 Dockerfile 中

amazon-web-services - docker 不创建文件

Android - 获取对 Manifest 中定义的 BroadcastReceiver 的引用