Extract character from matrix variable

I have a matrix variable setup like this -

strategy:
      matrix:
        compiler: [gcc-5, gcc-6, gcc-8, gcc-9, clang-4, clang-7, clang-8, clang-9, clang-10, clang-11]

How do I extract the numbers from this?

I tried doing -

if [ "${{ matrix.compiler }}" = *"gcc"* ]; then
    echo "CC=gcc-${{ matrix.compiler }:4:1}" >> $GITHUB_ENV
    echo "CXX=g++-${{ matrix.compiler }:4:1}" >> $GITHUB_ENV
else
    echo "CC=clang-${{ matrix.compiler }:5:1}" >> $GITHUB_ENV
    echo "CXX=clang++${{ matrix.compiler }:5:1}" >> $GITHUB_ENV
fi

where I try to get the 4th and the 5th character from the string as ${{ matrix.compiler }:4:1} and ${{ matrix.compiler }:5:1} but that doesn’t work :frowning:

1 Like

I’m new at GitHub Actions so I don’t know the exact answer; but, I did a little research and …

You can’t use BASH commands/logic within your YML workflow.

StackOverflow (https://stackoverflow.com/questions/60916931/github-action-does-the-if-have-an-else) says there is no IF-THEN-ELSE.

If is actions is of the form (got this from (run step on (if branch/tag is))

if: contains(github.ref, “release”)
steps:

  • run: echo “I only run if the branch has release in its name!”

You’ll need to restructure your logic … I think.

This link has lots of useful documentation(https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions)

Store the compiler value from matrix as an env variable and use that.

- name: Set compiler    
  run: |
    if [[ "$compiler" =~ ^gcc* ]]; then
      echo "CC=gcc-${compiler:4}" >> $GITHUB_ENV
      echo "CXX=g++-${compiler:4}" >> $GITHUB_ENV
    else
      echo "CC=clang-${compiler:6}" >> $GITHUB_ENV
      echo "CXX=clang++-${compiler:6}" >> $GITHUB_ENV
    fi
  env:
    compiler: ${{ matrix.compiler }}