首页 > 解决方案 > 减少 GitHub Actions 步骤中的样板文件

问题描述

最近,GitHub删除了几个默认安装在其运行器上的 C 和 C++ 编译器。

现在我需要在构建步骤中根据需要安装这些编译器。我使用以下两个步骤,一个用于 C,一个用于 C++。

      - name: Install C Compiler
        if: ${{ startsWith(matrix.c_compiler, 'gcc-') || startsWith(matrix.c_compiler, 'clang-') }}
        run: |
          sudo apt-get update
          sudo apt-get install -y ${{ matrix.c_compiler }}
      - name: Install C++ Compiler
        if: ${{ startsWith(matrix.cpp_compiler, 'g++-') || startsWith(matrix.cpp_compiler, 'clang++-') }}
        run: |
          sudo apt-get update
          sudo apt-get install -y ${{ matrix.cpp_compiler }}

if条件检查我们是否实际使用版本化编译器,gcc-8而不是默认的gcc(不需要安装1)。

我想减少这个样板。例如,这两个步骤几乎相同,除了它们检查的变量matrix.c_compilervs matrix.cpp_compiler、它们的名称和编译器的预期形式gcc-vs g++-

有没有办法避免这种重复?


1 ...实际上尝试安装默认编译器并非无害:install clang clang++在 GitHub 运行器上失败。

标签: github-actions

解决方案


推荐阅读