Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to commit and push back changes made by github action (invalid user)

I have following step at the end of my action workflow. It runs when code is pushed to master, the idea is to commit all files changed by github action back to master once this action finishes.

    - name: Commit and push changes
      run: |
        git config --global user.name 'myGithubUserName'
        git config --global user.email '[email protected]'
        git add -A
        git commit -m "Increased build number [skip ci]"
        git push -u origin HEAD

However I keep getting following error, even though I am configuring user and email.

fatal: could not read Username for 'https://github.com': Device not configured

[error]Process completed with exit code 128.

Note, this runs on macOS-latest and uses git that comes prepackaged with it.

like image 751
Ilja Avatar asked Oct 06 '19 12:10

Ilja


Video Answer


1 Answers

actions/checkout@v2

Version 2 of checkout resolves the detached HEAD state issue and simplifies pushing to origin.

name: Push commit
on: push
jobs:
  report:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Create report file
        run: date +%s > report.txt
      - name: Commit report
        run: |
          git config --global user.name 'Your Name'
          git config --global user.email '[email protected]'
          git commit -am "Automated report"
          git push

If you need the push event to trigger other workflows, use a repo scoped Personal Access Token.

      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.PAT }}

actions/checkout@v1 (original answer)

The problem is that the actions/checkout@v1 action leaves the git repository in a detached HEAD state. See this issue about it for more detailed information: https://github.com/actions/checkout/issues/6

The workaround I have used successfully is to setup the remote as follows:

git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/username/repository

You may also need to checkout. You can extract the branch name from the GITHUB_REF:

git checkout "${GITHUB_REF:11}"

Here is a complete example to demonstrate.

name: Push commit example
on: push
jobs:
  report:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Create report file
        run: date +%s > report.txt
      - name: Commit report
        run: |
          git config --global user.name 'Your Name'
          git config --global user.email '[email protected]'
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
          git checkout "${GITHUB_REF:11}"
          git commit -am "Automated report"
          git push

By the way, I have written a GitHub action which may help you achieve what you want to do. It will take any changes made locally during a workflow, commit them to a new branch and raise a pull request. https://github.com/peter-evans/create-pull-request

Also see this related question and answer. Push to origin from GitHub action

like image 169
peterevans Avatar answered Nov 15 '22 19:11

peterevans