1

I have been trying to automate and to update the authors.md whenever there is a new pull request and update it using the github action and below is the code for it

name: Add Contributor to AUTHORS.md
on:
  pull_request:
    types:
      - opened
      - synchronize

jobs:
  add_contributor:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Check if contributor already exists
        id: check_contributor
        run: |
          exists=$(grep -c -F "$GITHUB_ACTOR" AUTHORS.md)
          if [[ $exists -gt 0 ]]; then
            echo "exists=true" >> $GITHUB_ENV
          else
            echo "exists=false" >> $GITHUB_ENV
          fi
      - name: Get contributor's full name
        id: get_full_name
        if: steps.check_contributor.outputs.exists == 'false'
        run: |
          username=$GITHUB_ACTOR
          access_token=$GITHUB_TOKEN
          full_name=$(curl -s -H "Authorization: token $access_token" "https://api.github.com/users/$username" | jq -r '.name')
          if [[ -z "$full_name" ]]; then
            full_name=$username
          fi
          echo "full_name=$full_name" >> $GITHUB_ENV
      - name: Append contributor to AUTHORS.md
        id: append_contributor
        if: steps.check_contributor.outputs.exists == 'false'
        run: |
          echo "| $full_name | $GITHUB_ACTOR |" >> AUTHORS.md
      - name: Commit and push changes
        if: steps.check_contributor.outputs.exists == 'false'
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add AUTHORS.md
          git commit -m "Add $GITHUB_ACTOR to AUTHORS.md"
          git push

but the github action is failing at the line exists=$(grep -c -F "$GITHUB_ACTOR" AUTHORS.md), can someone help and tell me how to resolve it

error i am getting

Run exists=$(grep -c -F "$GITHUB_ACTOR" AUTHORS.md)
Error: Process completed with exit code 1.

UPDATES:

name: Add Contributor to AUTHORS.md
on:
  pull_request:
    types:
      - opened
      - synchronize

jobs:
  add_contributor:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Check if contributor already exists
        id: check_contributor
        run: |
          exists=$(grep -c -F "$GITHUB_ACTOR" AUTHORS.md)
          if [[ $exists -gt 0 ]]; then
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi
      - name: Get contributor's full name
        id: get_full_name
        if: steps.check_contributor.outputs.exists == 'false'
        run: |
          username=$GITHUB_ACTOR
          access_token=$GITHUB_TOKEN
          full_name=$(curl -s -H "Authorization: token $access_token" "https://api.github.com/users/$username" | jq -r '.name')
          if [[ -z "$full_name" ]]; then
            full_name=$username
          fi
          echo "full_name=$full_name" >> $GITHUB_OUTPUT
      - name: Append contributor to AUTHORS.md
        id: append_contributor
        if: steps.check_contributor.outputs.exists == 'false'
        run: |
          full_name=${{ steps.get_full_name.outputs.full_name }}
          echo "| $full_name | $GITHUB_ACTOR |" >> AUTHORS.md
      - name: Commit and push changes
        if: steps.check_contributor.outputs.exists == 'false'
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add AUTHORS.md
          git commit -m "Add $GITHUB_ACTOR to AUTHORS.md"
          git push

try to do some changes and also tried set+x method it is not working so removed it .

also tried to store to github outputs

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • 1
    That must be exiting with a non-zero exit status in the absence of `$GITHUB_ACTOR` in `AUTHORS.md` due to the default Bash configuration, see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell. Using it directly inside `if` should fix this. Or, you can append `|| true` after that command and that'll be fine too. – Azeem Jun 30 '23 at 05:58
  • See https://unix.stackexchange.com/questions/330660/preventing-grep-from-causing-premature-termination-of-bash-e-script. – Azeem Jun 30 '23 at 05:59
  • Same goes for all the other commands in your workflow where the output is being assigned to a variable. You need to handle it for those instances too. – Azeem Jun 30 '23 at 06:01
  • Does this answer your question? [Github Actions: Why an intermediate command failure in shell script would cause the whole step to fail?](https://stackoverflow.com/questions/73066461/github-actions-why-an-intermediate-command-failure-in-shell-script-would-cause) – Azeem Jun 30 '23 at 06:03
  • still not able to understand what need to be done in my code TO REMOVE the error . – Garvit Singhal Jun 30 '23 at 06:10
  • Simply add `set +x` as the first line under `run` sections as mentioned in the above thread. That's one simple alternative that you can use. – Azeem Jun 30 '23 at 06:14
  • so i need to add it for all the run commands of all steps i am doing , right – Garvit Singhal Jun 30 '23 at 06:17
  • Yes, wherever you have `variable=$(command)`. Or, you may configure Bash and add a default for all `run` sections at workflow or job level. See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaultsrun. – Azeem Jun 30 '23 at 06:24
  • okk i will try the changes and run the github actions again and let see if anymore error comes or not. – Garvit Singhal Jun 30 '23 at 06:25
  • done the cahnges suggested by you but it is still not working – Garvit Singhal Jul 10 '23 at 11:02
  • Please edit your question and add what you tried under an UPDATE heading. – Azeem Jul 10 '23 at 11:18
  • updated the code – Garvit Singhal Jul 10 '23 at 11:30
  • That's the same workflow. Only change I see is the replacement of `GITHUB_ENV` with `GITHUB_OUTPUT`. – Azeem Jul 10 '23 at 12:28
  • i have also added the below line full_name=${{ steps.get_full_name.outputs.full_name } as it is also creating error as it was not accesible at that step, not able to find any other issue in my code then the changes i have done , means only 2 issues . and also tried the method of set +x but still same error so i have removed it – Garvit Singhal Jul 10 '23 at 12:52
  • That's part of how output parameter works. You need to add the shell related changes in your question. Without that it's hard to say anything. – Azeem Jul 10 '23 at 12:56
  • Instead of `exists=$(grep -c -F "$GITHUB_ACTOR" AUTHORS.md)`, use the command directly in `if` i.e. `if grep -c -F "$GITHUB_ACTOR" AUTHORS.md; then` and it should work. – Azeem Jul 10 '23 at 13:00

0 Answers0