简体   繁体   中英

Python script that calls vcvarsall.bat fails from GitLab CI

I have a Python script that works from cmd.exe and powershell.exe but not from a GitLab runner job on Windows.

def build():
    for c in ["Debug", "Release"]:
        subprocess.check_call(
            f'vcvarsall.bat x64 && \
            cmake -G "NMake Makefiles" \
                -B{os.path.dirname(os.path.realpath(__file__))}/src/x64/{c} \
                -S{os.path.dirname(os.path.realpath(__file__))}/src \
                -DCMAKE_BUILD_TYPE={c} & \
            cmake --build {os.path.dirname(os.path.realpath(__file__))}/src/x64/{c}',
    )

if __name__ == "__main__":
    build()

If I run the script from cmd.exe or powershell.exe like so: python build.py , everything works as expected - vcvarsall.bat sets up the environment correctly so the subsequent cmake call has all of the required environment variables.

Running the script from a GitLab runner job on Windows using the .gitlab-ci.yml below fails.

build:
  stage: build
  script:
    - python build.py
  tags:
    - windows

In the GitLab UI job stdout, I can confirm vcvarsall.bat is being run and cmake after that:

In the CMakeError.log , the following error is reported:

The above link error makes me think that running vcvarsall.bat from the GitLab runner job is not completely configuring the environment - it is changing the environment, but not enough to make the subsequent cmake complete successfully. I can see many differences between the environment configured by vcvarsall.bat when executed from the GitLab runner job and cmd.exe .

For example, the left is the environment defined when running the script from the GitLab runner job, and the right from cmd.exe :

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Why do these environmental discrepancies exist, and how can I fix them?

The GitLab runner jobs will not inherit user environment variables or profiles. For example, powershell on Windows runners is always run with the NoProfile switch.

You must add the variables explicitly in the runner configuration (or anywhere else env variables can be defined) or add them to system environment variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM