本文介绍了如何在gitlab-ci共享运行器上使用自定义Windows docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储库,该存储库由以下两个文件组成,该文件将构建一个安装了Visual Studio buildtools的Windows docker容器,并将其推送到gitlab为我的存储库提供的注册表中

i have a repository which consists of following two files, which builds an windows docker container with visual studio buildtools installed and pushes it to the registry provided by gitlab for my repository

./Dockerfile

# escape=`

FROM mcr.microsoft.com/windows/servercore:1809-amd64

RUN powershell mkdir .\TEMP\;`
    Invoke-WebRequest -Uri https://aka.ms/vs/16/release/vs_buildtools.exe -OutFile .\TEMP\vs_buildtools.exe; `
    .\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
    --installPath C:\BuildTools `
    --add Microsoft.VisualStudio.Component.VC.CMake.Project `
    --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
    --add Microsoft.VisualStudio.Component.Windows10SDK `
    --add Microsoft.VisualStudio.Component.Windows10SDK.18362


ENTRYPOINT ["C:\\BuildTools\\Common7\\Tools\\VsDevCmd.bat", "&&", "powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]

./.. gitlab-ci.yml

docker-build:  # Official docker image.
  tags: 
    - windows
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}" .
    - docker push "${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}"

到目前为止,这很完美

现在在另一个存储库中,我想使用此图像在其上构建我的应用程序

now in another repository i want to use this image to build my application on

./.. gitlab-ci.yml

build-app:
  image: registry.gitlab.com/valerij/windows-builder:master
  tags: 
    - windows
  stage: build
  script:
    - "docker images"
    - "dir C:\\BuildTools" 
    - "cl --version"

失败

Running with gitlab-runner 13.4.1 (e95f89a0)
  on windows-shared-runners-manager Hs8mheX5
Preparing the "custom" executor
Using Custom executor with driver autoscaler dev (6184f4a)...
Creating virtual machine for the job...
Virtual machine created!
Preparing environment
00:11
Running on PACKER-5F1153D4 via 
runner-hs8mhex5-wsrm-4464870f601125122949...
Getting source from Git repository
Fetching changes with git depth set to 50...
Initialized empty Git repository in C:/GitLab-Runner/builds/valerij/ci-test/.git/
Created fresh repository.
Checking out f7535d50 as master...
git-lfs/2.8.0 (GitHub; windows amd64; go 1.12.2; git 30af66bb)
Skipping Git submodules setup
Executing "step_script" stage of the job script
WARNING: Starting with version 14.0 the 'build_script' stage will be replaced with 'step_script': https://gitlab.com/gitlab-org/gitlab-runner/-/issues/26426
$ docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
mcr.microsoft.com/windows/servercore   ltsc2019            561b89eac394        7 months ago        3.7GB
$ dir C:\BuildTools
dir : Cannot find path 'C:\BuildTools' because it does not exist.
At line:1 char:1
+ dir C:\BuildTools
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\BuildTools:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
 
Cleaning up file based variables
ERROR: Job failed: exit status 1

现在我注意到该日志缺少了另一个项目熟悉的内容

Now I notice that the log is missing anything along the lines familiar from another project

Pulling docker image node:latest ...
Using docker image sha256:2d840844f8e7594a542b30eaed0a3451f5d84b9f85d091d09abc8e0ae75c48e4 for node:latest with digest node@sha256:60a3bda0eb90be8fa78830f284671d4c231e91878bbe5bd1c953aedda141014a ...

在我看来,Windows运行程序会忽略 image 关键字.

It seems to me that the windows runner ignores the image keyword.

如何强制Windows运行程序使用我的自定义docker映像在其中执行CI?

How do I force the windows runner to use my custom docker image to perform the CI in?

推荐答案

简短的回答:不能,但是您可以解决它

Short answer: you can't, but you might be able to work around it

共享的Windows运行程序不处理 image services ,因此您不能以这种方式进行设置.不过,您仍然应该能够拉出另一个图像,然后在其中正常执行命令/脚本.

Shared windows runners do not process image or services, so you can't set it that way. You should still be able to pull the other image and then execute commands / scripts inside it normally though.

例如

build-app:
  tags: 
    - windows
  stage: build
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - "docker pull $CI_REGISTRY_IMAGE"
    - "docker images"
    - "docker run -v %cd%:%cd% -w %cd% $CI_REGISTRY_IMAGE cl --version"

您可能需要根据用例调整安装,路径等

You may need to adjust the mounts, path, etc. to your use case

这篇关于如何在gitlab-ci共享运行器上使用自定义Windows docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 22:36