Github Actions中Python脚本配置cmake/ninja路径后Unix系统识别失败
类Unix系统中Github Actions无法识别Ninja命令问题
我正在用Python脚本配合Github Actions搭建C++项目的构建工作流,已配置工作流在windows-latest、ubuntu-latest和macos-latest上运行构建脚本。目前卡在下载cmake和ninja的步骤:脚本仅在Windows上成功,Ubuntu和macOS中,尽管已下载二进制文件并将路径导出到GITHUB_PATH,系统仍无法识别ninja命令,但cmake却能正常工作,两者的安装步骤完全一致。
工作流脚本
name: Build Matrix #### # triggers for workflow #### on: [push, pull_request, workflow_dispatch] ### # preset enviroment variables ### env: CMAKE_VERSION: 3.24.3 NINJA_VERSION: 1.11.1 BUILD_TYPE: Release CCACHE_VERSION: 4.7.3 ### # Jobs: filter_path, build ### ### jobs: ### # paths-filter: ## Tracks any changes in the specified directories and outputs `exec_build` ## if `exec_build` is true, then build job is launched. ### paths-filter: runs-on: ubuntu-latest outputs: exec_build: ${{ steps.filter.outputs.workflows }} steps: - uses: actions/checkout@v2 - uses: dorny/paths-filter@v2 id: filter with: filters: | workflows: - '.github/workflows/build_project.yaml' - 'CMakeLists.txt' - 'Doxyfile' - '.gitignore' - 'include/**' - 'src/**' - 'test/**' - 'scripts/**' - 'examples/**' - 'utils/**' - 'libs/' build: name: ${{ matrix.config.name }} runs-on: ${{ matrix.config.os }} needs: paths-filter strategy: fail-fast: false matrix: config: - { name: "Windows Latest MSVC", artifact: "Windows-MSVC.7z", os: windows-latest, cc: "cl", cxx: "cl", environment_script: "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvars64.bat" } - { name: "Ubuntu Latest GCC", artifact: "Linux.7z", os: ubuntu-latest, cc: "gcc", cxx: "g++" } - { name: "macOS Latest Clang", artifact: "macOS.7z", os: macos-latest, cc: "clang", cxx: "clang++" } if: needs.paths-filter.outputs.exec_build == 'true' steps: # checkout the repo into the workspace - uses: actions/checkout@v3 # setup python for scripting - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.10' cache: 'pip' - name: Install pip packages id: install-packages run: | pip install -r scripts/requirements.txt # download ninja and cmake - name: Download Ninja and CMake shell: bash run: | python scripts/download-ninja-cmake.py # For debugging cat $GITHUB_PATH cmake --version ninja --version
Python脚本(scripts/download-ninja-cmake.py)
import subprocess from os import environ, path from detect_os import detect_os from setup_cmake import get_url as get_cmake_url from setup_ninja import get_url as get_ninja_url from extract_arcv import extract_here from github_env import GITHUB_WORKSPACE, GITHUB_PATH, RUNNER_OS from download import download from command_runner import command CMAKE_VERSION = environ.get('CMAKE_VERSION') NINJA_VERSION = environ.get('NINJA_VERSION') print(f"Detected OS: {RUNNER_OS})") # See Github enviroment variables print("Enviroment: ") subprocess.run(["env"]) """Notes * GITHUB_WORKSPACE, GITHUB_PATH, RUNNER_OS enviroment variables that I creating using GITHUB_VARIABLE = os.environ['GITHUB_VARIABLE'] * get_cmake_url(version : str) -> str and get_ninja_url(version : str) -> str: puts together the url used to download cmake and ninja respectively * download(url : str) -> str downloads cmake and ninja then it outputs the absolute path of the downloaded archive. * extract_here(compressed_file : str) -> str: behaves similar the GUI `extract here` in ubuntu, and it outputs the absolute path to the extracted content * command(cmd:str): is just an abstraction for the function: subprocess.run(cmd, shell=True) """ # # extract_here print(f"Downloading CMake v{CMAKE_VERSION}") url = get_cmake_url(CMAKE_VERSION) print(f"CMake Download URL: {url}") cmake_out_arcv = download(url) print(f"Downloaded: {cmake_out_arcv}") print(f"Extracting {cmake_out_arcv}: ") cmake_dir = extract_here(cmake_out_arcv) print(f"Extracted: {cmake_dir}") if RUNNER_OS == "Windows": cmake_dir = f"cmake-{CMAKE_VERSION}-windows-x86_64/ cmake-{CMAKE_VERSION}-windows-x86_64/bin" elif RUNNER_OS == "Linux": cmake_dir = f"cmake-{CMAKE_VERSION}-linux-x86_64/cmake-{CMAKE_VERSION}-linux-x86_64/bin" elif RUNNER_OS == "macOS": cmake_dir = f"cmake-{CMAKE_VERSION}-macos-universal/cmake-{CMAKE_VERSION}-macos-universal/CMake.app/Contents/bin" cmake_dir = path.join(GITHUB_WORKSPACE, cmake_dir) # ------------------- print(f"Downloading Ninja v{NINJA_VERSION}") ninja_url = get_ninja_url(NINJA_VERSION) print(f"Ninja Download URL: {ninja_url}") ninja_out_arcv = download(ninja_url) print(f"Downloaded: {ninja_out_arcv}") print(f"Extracting {ninja_out_arcv}: ") ninja_dir = extract_here(ninja_out_arcv) print(f"Extracted: {ninja_dir}") if RUNNER_OS != "windows": # Exports both ninja_dir and cmake_dir command(f'echo "{ninja_dir}:{cmake_dir}" >> $GITHUB_PATH ') command(f"chmod +x {ninja_dir}/ninja") command(f"chmod +x {cmake_dir}/cmake")
Ubuntu-latest输出日志
Run python scripts/download-ninja-cmake.py: Detected OS: Linux Downloading CMake v3.24.3 CMake Download URL: https://github.com/Kitware/CMake/releases/download/v3.24.3/cmake-3.24.3-linux-x86_64.tar.gz .. download progress logs .. Downloaded: /home/runner/work/cxx_project_template/cxx_project_template/cmake-3.24.3-linux-x86_64.tar.gz Extracting /home/runner/work/cxx_project_template/cxx_project_template/cmake-3.24.3-linux-x86_64.tar.gz: Extracted: /home/runner/work/cxx_project_template/cxx_project_template/cmake-3.24.3-linux-x86_64 Downloading Ninja v1.11.1 .. download progress logs .. Downloaded: /home/runner/work/cxx_project_template/cxx_project_template/ninja-linux.zip Extracting /home/runner/work/cxx_project_template/cxx_project_template/ninja-linux.zip: Extracted: /home/runner/work/cxx_project_template/cxx_project_template/ninja-linux cmake version 3.25.1 CMake suite maintained and supported by Kitware (kitware.com/cmake). /home/runner/work/_temp/6470193b-ec2e-4d49-bddf-10d580c046a4.sh: line 4: ninja: command not found Error: Process completed with exit code 127.
更新
执行脚本后GITHUB_PATH的内容:
/home/runner/work/cxx_project_template/cxx_project_template/ninja-linux:/home/runner/work/cxx_project_template/cxx_project_template/cmake-3.24.3-linux-x86_64/cmake-3.24.3-linux-x86_64/bin
奇怪的是Windows上即使未导出cmake和ninja的路径也能正常工作,但在类Unix系统中却失败了。
内容的提问来源于stack exchange,提问作者Zaki
相关产品推荐
相关产品推荐

