【问题标题】:How to use a custom windows docker container on gitlab-ci shared runner如何在 gitlab-ci 共享运行器上使用自定义 windows docker 容器
【发布时间】:2020-12-08 12:38:09
【问题描述】:

我有一个包含以下两个文件的存储库,它构建了一个安装了 Visual Studio buildtools 的 windows docker 容器并将其推送到 gitlab 为我的存储库提供的注册表

./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}"

到目前为止效果很好

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

./.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

现在我注意到日志缺少与另一个项目相似的任何内容

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

在我看来,windows runner 忽略了image 关键字。

如何强制 windows 运行器使用我的自定义 docker 映像来执行 CI?

【问题讨论】:

    标签: windows docker gitlab-ci


    【解决方案1】:

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

    共享 Windows 运行器 do not process image or services,所以你不能这样设置。不过,您仍然应该能够拉取另一个图像,然后在其中正常执行命令/脚本。

    例如,

    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"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2021-10-17
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      相关资源
      最近更新 更多