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

Time:11-05

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

CodePudding user response:

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

  • Related