Home > Enterprise >  Github action on release - push fails
Github action on release - push fails

Time:10-01

I'm trying to update the version in my pom.xml in my maven project with a GitHub (release) action, with the value provided in the UI / Create release tag.

name: Publish package to GitHub Repository
on:
  release:
       types: [created]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2
        with:
         fetch-depth: '0'
      - name: Install Java and Maven
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Update version in pom.xml (Release only)
        run: mvn -B versions:set -DnewVersion=${{ github.event.release.tag_name }} -DgenerateBackupPoms=false
      - name: Commit and push changes to pom.
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "[email protected]"
          git add pom.xml
          git commit -m "Bumped version to ${{ github.event.release.tag_name }}"
          git push
      - name: Deploy to Github Package Registry
        env:
          GITHUB_USERNAME: ${{ github.actor }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: mvn --settings settings.xml --file pom.xml deploy

However, I get an error:

Run git config --global user.name "GitHub Actions"
[detached HEAD 5ec5465] Bumped version to 1.0.4
 1 file changed, 1 insertion( ), 1 deletion(-)
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

I don't really understand why this happens. I thought the git checkout would give me a local git repo with the default (main) branch, to which I commit and then push (which should work if there are no other changes?). Also, compering to this answer: https://stackoverflow.com/a/58257219/461499, the only difference seems to be the on config?

What am I doing wrong? (And what should I do instead?)

CodePudding user response:

By default, the checkout action will check out the reference that triggered the workflow.

From the documentation:

When checking out the repository that triggered a workflow, this defaults to the reference or SHA for that event.

In your case, the event is the creation of a release, which is not part of the Git repository.

If you want to check out a particular branch, you can do so by specifying the ref option:

steps:
  - name: Check out Git repository
    uses: actions/checkout@v2
    with:
      ref: main
      fetch-depth: '0'
  • Related