GitHub Actions (composite action): Get array of branches matching pattern

Issue

I’m trying to create an array of branches matching a pattern so I can work with them in a loop. I’m able to do this in a Linux emulator using a hard-coded string that matches the output I get from the git branch command, but I’m experiencing odd behavior in GitHub Actions. If I loop through the "array", the loop is performed 1 time per matching branch, but there’s no output from the $branch value.

- id: createMergeBranches      
  shell: bash
  run: |
        features=$(git branch -r | grep -i "feature" | sed -e "s/.*origin\///g; /merge/d" | tr "\n" " ")
        echo $features // space-separated string of branches in expected format
        eval "featureArray=($features)"

        # create merge branches            
        for branch in ${featureArray[@]}
        do
          mergeBranch="merge/${{ inputs.sourceBranch }}_$branch"
          mergeBranches+=($mergeBranch)
          echo "Feature Branch: $branch"
          echo "Merge Branch: $mergeBranch"
        done

Output (n times):

Feature Branch:
Merge Branch: merge/develop_

Later when I assign $featureArray to an env var, it’s empty – as is $mergeBranches

Solution

Upon changing the loop’s internal parameter name from branch to featureBranch, everything works as expected. I assume there’s a naming conflict with "branch" or something along those lines.

        features=$(git branch -r | grep -i "feature" | sed -e "s/.*origin\///g; /merge/d" | tr "\n" " ")
        echo $features // space-separated string of branches in expected format
        eval "featureArray=($features)"

        # create merge branches            
        for featureBranch in ${featureArray[@]}
        do
          mergeBranch="merge/${{ inputs.sourceBranch }}_$featureBranch"
          mergeBranches+=($mergeBranch)
          echo "Feature Branch: $featureBranch"
          echo "Merge Branch: $mergeBranch"
        done

Feature Branch: feature/foo
Merge Branch: merge/develop_feature/foo

Answered By – froggomad

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published