skip to Main Content

I have a repo with a main branch and a testing branch. I make modifications in the testing branch, push, open a PR. This triggers the following workflow (borrowed from https://stackoverflow.com/a/73687159/659117) which creates a file (e.g. a PDF from latex files) and commits it:

name: Example workflow
on:

  pull_request:
    branches: [ main ]

jobs:
  example_job:
    runs-on: ubuntu-latest

    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
      - shell: bash
        run: |
          date > 1.txt
          git config user.name github-actions
          git config user.email [email protected]
          git add 1.txt
          git commit -m "updated"
          git push origin HEAD:${{ github.head_ref }}

So far, so good: the file 1.txt is created and added to the branch testing, and I can see the commit in the PR.

However, if I now make another change to the code (locally) and push it, the commit is rejected with the error:

hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.

I know I can override it by doing push -f, but it is not something I want to do on a regular basis. So I would I set up a workflow that does this, without running into this problem? Am I missing something here?

2

Answers


  1. You need to pull the changes on your local system the github-action made before commit/push. I think you already know that.

    In git there are local hooks, you could create a local pre-commit hook who automatically make a pull before commit.

    Is it only the date? You could enable Git keyword substitution like those in Subversion? "keyword substitution"

    Login or Signup to reply.
  2. Generated PDF in the repository is a bad idea. Git is for sourcecode. PDF is neither code nor source. Please create a clean webservice who generate the PDf and use the github-action to upload the txt to that webservice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search