question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting shell default for Windows hosts in cross-platform matrix

See original GitHub issue

So, I’m kind of banging my head against a wall with something, and I wanted to see if anyone has any clever solutions.

I have a project which builds in CMake, and the cmake commands are actually all the same whether running on Linux, macOS, or Windows. I’ve worked very hard to keep things that way, in fact, so that the build tooling could be as platform-agnostic as possible.

So, I have a workflow written with build steps that are largely shared between all platforms in the matrix. (Each OS has its own dependency step, gated with an if: ${{ runner.os == 'Linux' }} (or 'Windows' or 'macos'), but that’s the only non-shared config.)

To differentiate the -G argument to cmake, which selects the generator to use for the build system, I’m using action-cond from @haya14busa which works great:

     - name: Select CMake generator
        uses: haya14busa/action-cond@v1
        id: generator
        with:
          cond: ${{ runner.os == 'Windows' }}
          if_true: 'MinGW Makefiles'
          if_false: 'Unix Makefiles'
      - name: Build
        run: |
          cmake -B build -S . -G "${{ steps.generator.outputs.value }}" ...

The problem is, I can’t think of any way to do the same thing for the MSYS2 shell. If I wanted to take advantage of the “Default shell” option from the README, I’d end up setting it as the default for every shell, including the ones that run on Linux and macOS, which obviously won’t work! I’ve tried 100 different ways to conditionalize the setting, and they all failed:

  1. Add an if: to the defaults: section of the config: Syntax error
  2. Add another haya14busa/action-cond@v1 step that sets either msys2 {0} or bash, and use shell: ${{ steps.select_shell.outputs.value }} in subsequent steps: Syntax error, apparently the steps object is not accessible from the metaconfiguration of subsequent steps.
  3. Capture the output into an environment variable, the same way I can with CC: ${{ matrix.compiler }}:
    env:
      RUNSHELL: ${{ steps.select_shell.outputs.value }}
    steps:
       - name: Select shell...
         id: select_shell
         with:
          cond: ${{ runner.os == 'Windows' }}
          if_true: 'msys2 -c'
          if_false: 'bash -c'
         ...
      - name: Build
        run: |
          $RUNSHELL 'cmake...'
    
    (In my defense, I never expected that one to work, and it doesn’t. Same issue with accessing steps before it’s defined.)
  4. The same thing, but set the env in every step that needs it:
    steps:
       - name: Select shell...
         id: select_shell
         with:
          cond: ${{ runner.os == 'Windows' }}
          if_true: 'msys2 -c'
          if_false: 'bash -c'
         ...
      - name: Build
        env:
          RUNSHELL: ${{ steps.select_shell.outputs.value }}
        run: |
          $RUNSHELL 'cmake...'
    
    That actually came close to working, believe it or not! Actually worked on Linux and macOS. Blew up in the default Powershell on Windows, though.
  5. Set up a completely separate job config before the build job, with the same matrix, and have it relay the results of action-cond into an outputs. Have the second job needs: the first, and set shell: ${{ needs.job1.outputs.shell }} on each step: Syntax error, it seems the needs context isn’t any more accessible for defining steps than the steps context is.

At this point I feel like I’ve exhausted all possibilities. Any suggestions, or do I really have to take my nice, cross-platform workflows and duplicate all of the run steps, just so I can get Windows to run them using the correct shell? 😞

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14

github_iconTop GitHub Comments

3reactions
eyal0commented, Jan 10, 2021

Check it out, windows/macos/ubuntu in a unified list of GitHub workflow steps: https://github.com/msys2/setup-msys2/issues/98#issuecomment-757397694

2reactions
einecommented, Jan 7, 2021
  strategy:
    matrix:
      include:
        - { os: windows, shell: msys2 },
        - { os: ubuntu,  shell: bash  },
        - { os: macos,   shell: bash  },
  runs-on: ${{ matrix.os }}
  defaults:
    run:
      shell: ${{ matrix.shell} {0}
  steps:

WRT selecting the CMake generator, I don’t really understand why you are using an specific Action for that, given that all you need to do is set an environment variable. Unless the Action is aware of how MSYS2 works, the envvar won’t be visible. You’d need to inherit the system PATH in msys2 shells, which is discouraged.

      - name: Build
        run: |
          CMAKE_GENERATOR='Unix Makefiles'
          if [ "${{ matrix.org }}" = "windows" ]; then
            CMAKE_GENERATOR='MSYS Makefiles'
          fi
          cmake -B build -S . -G "$CMAKE_GENERATOR" ...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for Configuring Reflection Secure Shell (SSH)
This document provides information about the best practices to use when configuring secure, encrypted communications between a trusted host and ...
Read more >
Windows Container Version Compatibility | Microsoft Learn
Version compatibility for containers built from different versions of Windows Server and Windows.
Read more >
Creating Dynamic Test Matrices using Github Actions - Part 2
To create a matrix cell we add a HashTable to the $Jobs array. Each key in the HashTable appears as a matrix variable...
Read more >
Options - cibuildwheel - Read the Docs
Static configuration works across all CI systems, and can be used locally if you run cibuildwheel --platform linux . This is preferred, but...
Read more >
Compose file version 3 reference - Docker Documentation
For instance, 0444 represents world-readable. The default is 0444 . Configs cannot be writable because they are mounted in a temporary filesystem, so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found