Search code examples
gitshellgithub-actions

Not stable on using GitHub CLI and Git with GitHub Actions


I have a shell script written to check if a branch exists in repositories based on a keyword.

Snippets of the script:

for repo_name in ${repository[@]}; do
  gh repo clone $org_name/$repo_name >/dev/null 2>&1
  cd $repo_name
  if [[ -n "$(git branch -r | grep -w $keyword)" ]]; then
    echo "Branch does exist in $org_name/$repo_name"
  else
    echo "Branch does not exist in $org_name/$repo_name"
  fi
  cd ..
  rm -rf $repo_name
done

I written a GitHub Actions workflow to continuously execute this script for other purpose.

Now I used a branch named ABC to test this script. It works fine in my local, printed the result that the branch does exist in the repository. However, it doesn't work on one of my repository running the GitHub Actions workflow. It just showed me that it does not exist. The funny thing is i tried to create a new repository, setting up same script and workflow, it can show the result correctly like my local.

For example, there are branches named ABC in two repository R1 and R2 respectively. I executed the same script in local, and another two repository R3 and R4 stored the workflow. The result showed as below,

  1. local: Branch ABC does exist in R1 and R2
  2. R3 workflow: Branch ABC does exist in R1 but not in R2
  3. R4 workflow: Branch ABC does exist in R1 and R2

R3 and R4 used same private key and GitHub Apps to authenticate to the repository during workflow execution.

Can anyone tell me how is it possible? Is it something related to my shell script?


Solution

  • Like @Jim commented, you don't need to clone them all.

    Can use git ls-remote, with more params:

    git ls-remote --heads ${URL} ${KEYWORD}
    

    to verify if that branch exists.


    So the script would be something like:

    KEYWORD=my_repo
    
    for repo_name in ${repositories[@]}; do
      URL=gh:$org_name/$repo_name  # see note [1] below.
    
      if [[ -n $(git ls-remote --heads ${URL} ${KEYWORD}) ]] ; then
        echo "Branch does exist in $org_name/$repo_name"
    
      else
        echo "Branch does not exist in $org_name/$repo_name"
    
    done
    
    

    Note

    1. I use ssh to connect to Github with ssh config like this:
    Host gh
      Hostname ssh.github.com
      User git
      Port 443
      IdentityFile ~/.ssh/id_rsa_my_key
    

    You may need to change the URL to a proper value.


    1. the git ls-remote command seems not return 1 when the branch is not found.

    You can use [[ -n ... ]] (like the example above) or use grep -q ${KEYWORD} in your if conditions.




    Edited - get branch list with gh api

    If you can use gh api, can install jq then filter branches without cloning like this:

    gh api repos/$org_name/$repo_name/branches | jq -r '.[].name' | grep -w ${KEYWORD}
    
    # I prefer grep -q in if statements
    # e.g.
    # if gh api repos/$org_name/$repo_name/branches | jq -r '.[].name' | grep -q ${KEYWORD} 
    # then 
    # ...
    # fi
    

    To install jq, see: https://command-not-found.com/jq