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

泄露秘密 提交于 2019-12-01 06:52:06

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/58257140/unable-to-commit-and-push-back-changes-made-by-github-action-invalid-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!