Search code examples
gitgithubgithub-actionsworkflow

How can I commit and push changes to a repository in concurrent Github Actions Workflow runs?


I have a Github Actions workflow that takes a value from a file (config.json) in my repository, increments it and pushes the changes to the repository.

config.json file:

{
    "sequence_number": 1
}

The sequence_number will be incremented by 1.

When I run the workflow once, it does the job correctly.

When I run two workflows back to back, the runs get queued. The first run again pushes the changes to the file correctly (I can see the updated value after the first run), but then during the (queued) second run I get the following error:

 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/repo_x'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

My workflow looks something like this:

name: workflow

concurrency:
  group: ${{ github.workflow }}

...

jobs:
  extracting-value-from-config-file:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
  
    - name: Reading The JSON Configuration File & Extracting The Value
      id: extracted-value
      run: |
        json=$(cat config.json)
        number=$(echo "$json" | jq -r '.sequence_number')
...

  updating-config-file:
    needs: extracting-value-from-config-file
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - run: |
          echo "`jq '.sequence_number=${{ needs.extracting-value-from-config-file.outputs.SEQUENCE_NUMBER }}+1]' config.json`" > config.json
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add .
          git commit -m "AUTO updated sequence number"
          git push

Basically, the error during the second run says that it has not pulled the latest version yet, but I do a git pull (actions/checkout@v4) in both of the workflow jobs.

Does someone know what I do wrong here?

I tried doing all sorts of pulling requests, but somehow it never pulls the latest version of the config file.


Solution

  • I solved the problem, but I am not sure whether this solution os a good one.

    This is what I changed in the code:

    jobs:
      extracting-value-from-config-file:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v4
        - run: |
            git config user.name github-actions # added
            git config user.name github-actions@github.com # added
            git fetch -all # added
            git reset --hard origin/main # added
    
    ...
    
      updating-config-file:
        needs: extracting-value-from-config-file
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: |
              git fetch --all # added
              git reset --hard origin/main # added
              echo "`jq '.sequence_number=${{ needs.extracting-value-from-config-file.outputs.SEQUENCE_NUMBER }}+1]' config.json`" > config.json
              git config user.name github-actions
              git config user.email github-actions@github.com
              git add .
              git commit -m "AUTO updated sequence number"
              git push