Skip to content

[executor][bash] use filepath over path

Ritesh Khadgaray requested to merge ritzk/gitlab-runner:win_bash into main

What does this MR do?

Switches bash executor to use filepath over path. filepath uses OS native path("/" on UNIX, "" on Windows), and path uses "/" This will ensure that Absolute filepath validation works correctly with Bash shell on Windows and Linux/Mac.

Why was this MR needed?

Allows one to use "bash" shell with shell(Windows) and docker-windows executor. This is not meant for WSL bash ( which runs the application under Linux), but for running bash shell executor under Windows environment.

  • /c/xyz on git bash (msys/cygwin) would be C:\xyz
  • c:/xyz on git bash (msys/cygwin) would be C:\xyz
  • /c/c: on git bash (msys/cygwin) would be C:\c:\xyz
  • $PWD\C:\xyz on git bash (msys/cygwin) would translate to /c/c:/xyz

This breaks build

  • PWD -> C: under windows, or /c/ under bash
  • mkdir could create this under C:\builds...
  • print would try writing to "C:\C:\builds...

Code snippet

mkdir -p "c:/builds/repo-path/project.tmp"
printf ... > "$PWD/c:/builds/repo-path/project/CI_SERVER_TLS_CA_FILE"

What's the best way to test this MR?

run the executor with shell "bash". Ensure bash is available in PATH when using shell executor.

[[runners]]
  name = "shell runner"
  executor = "shell"
  shell = "bash"
...

[[runners]]
  name = "docker runner"
  executor = "docker-windows"
  shell = "bash"
  [runners.docker]
    # built on top of mcr.microsoft.com/windows/nanoserver:ltsc2022 with bash
    image = "example.com/base"
    # helper image with bash
    helper_image = "example.com/windows/gitlab_helper:2022.6.23.git2b62653a"
    # helper_image = "registry.gitlab.com/gitlab-org/gitlab-runner/gitlab-runner-helper:x86_64-latest-servercore21H1"

For docker-windows executor, You will need to use a custom gitlab helper image with bash as a part of the build. The below config has just enough to get moving.

# escape=`
#
# gitlab helper config
# https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/dockerfiles/runner-helper/Dockerfile.x86_64_servercore
#
# msys2 does not work on nanoserver
# https://github.com/docker/for-win/issues/2920
#
FROM mcr.microsoft.com/powershell:lts-windowsservercore-ltsc2022 as base

USER ContainerAdministrator

SHELL ["pwsh", "-Command"]

ENV GITDIR 'C:\Git'

# Install Chocolatey
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; `
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
    iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

RUN  RefreshEnv.cmd `
    && echo 'Installing git"'`
    && choco install -y git `
       --install-arguments='/Dir=C:\Git' `
       --params '/DefaultBranchName:main /GitAndUnixToolsOnPath /NoAutoCrlf /SChannel /NoShellIntegration /Symlinks /PseudoConsoleSupport' 

RUN [System.Environment]::SetEnvironmentVariable('GITDIR', 'C:\Git', 'Machine'); `
    $oldpath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine'); `
    $newpath = $oldpath.insert(0, 'C:\Git\bin\;C:\Git\cmd;C:\Git\usr\bin\;'); `
    [System.Environment]::SetEnvironmentVariable('Path', $newpath, 'Machine'); `
    $Env:PATH = $newpath; `
    Remove-Variable -Name oldpath; `
    Remove-Variable -Name newpath

RUN New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" ` -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force `
    && &C:\Git\bin\git config --system core.longpaths true `
    && &C:\Git\bin\git config --system core.eol lf `
    && &C:\Git\bin\git config --system safe.directory '*' `
    && &C:\Git\bin\git config --system credential.helper wincred 

ENTRYPOINT ["C:\\Git\\bin\\sh.exe", "-c"]
# ENTRYPOINT ["C:\\Git\\usr\\bin\\bash.exe", "-c" ]
CMD [ "bash" ]

FROM base as gitlab_helper

SHELL ["pwsh", "-Command"]

ARG PKG_URL_GL_HELPER="https://gitlab.com/ritzk/gitlab-runner/-/jobs/3452158072/artifacts/raw/out/binaries/gitlab-runner-helper/gitlab-runner-helper.x86_64-windows.exe"

RUN Invoke-WebRequest $Env:PKG_URL_GL_HELPER  -OutFile C:\Git\usr\bin\gitlab-runner-helper.exe

ENTRYPOINT ["C:\\Git\\bin\\sh.exe", "-c"]
CMD [ "bash" ]

What are the relevant issue numbers?

#4139 #4725 #1233 (closed)

Edited by Ritesh Khadgaray

Merge request reports