11

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 'myEmail@gmail.com'
        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.

Ilja
  • 44,142
  • 92
  • 275
  • 498

1 Answers1

13

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 'your-username@users.noreply.github.com'
          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 'your-username@users.noreply.github.com'
          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

peterevans
  • 34,297
  • 7
  • 84
  • 83
  • Hmm, I added this line before my `git add -A` step, changed `/username/repository` to reflect my repo, but am still getting an error, although now a different one `remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/MyOrg/MyRepo/'`. Thank you for linking your action, I will have a look through it. – Ilja Oct 06 '19 at 12:37
  • I updated the post to use the secret directly, not from an environment variable. Does that help? – peterevans Oct 06 '19 at 12:40
  • For anyone who likes to use as little bytes as possible, `x-access-token:` part can be ignored; `https://${{ secrets.GITHUB_TOKEN }}@github.com/username/repository` will work just fine. – Samira Oct 06 '19 at 14:38
  • @Samira I don't think that's true in all cases. I needed to clone a private repo within a github action, and it failed without the `x-access-token:` part. – ConorSheehan1 Nov 30 '19 at 23:19
  • 1
    @con-- Hah. Didn't use remote url editing for some time, but it shouldn't (sic!) be needed anymore as you can run `actions/checkout` with `token` argument nowadays. While waiting for [v2](https://github.com/actions/checkout/pull/70) i'm using `actions/checkout@e8bd1dffb6451bb0d84dbcd3ed059daca1371180` with `token` argument and it pushes just fine without touching any remote settings... at least on public repo (don't have any private which needs autocommits to confirm it works with them). – Samira Dec 01 '19 at 06:51
  • @Samira I just use that version fro Checkout and was able to achieve the commit and push, thanks, but Idk why I cant use v2 as written by peterevans, Also I'm working on pull_request – Alberto Rojas May 11 '20 at 22:30
  • @Samira: Please share the code for token (I can simply pass following), am I right here? with: github_token: ${{ secrets.GITHUB_TOKEN }} – amandeep1991 Jun 02 '20 at 08:16
  • @amandeep1991 [Here](https://github.com/rotators/Fo1in2/blob/fdf01b00b87320702e9e8055c5f6f26e1e1073d8/.github/workflows/schedule.yml#L93-L98), ignore `if`/`fetch-depth`. I'm using PAT there but it'll work just fine with `GITHUB_TOKEN`. – Samira Jun 02 '20 at 20:45
  • @Samira: Thanks I got the fully working code now ( I wanted to update the version in my bundle which gets uploaded to pypi.org). Same is below as one of the answers. – amandeep1991 Jun 03 '20 at 02:45