无法提交和推回 github 操作所做的更改(无效用户) [英] Unable to commit and push back changes made by github action (invalid user)

查看:13
本文介绍了无法提交和推回 github 操作所做的更改(无效用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的操作工作流程结束时有以下步骤.它在代码推送到 master 时运行,其想法是在此操作完成后将 github 操作更改的所有文件提交回 master.

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.

致命:无法读取 'https://github.com' 的用户名:设备不是已配置

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

注意,这在 macOS-latest 上运行,并使用预打包的 git.

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

推荐答案

actions/checkout@v2

第 2 版结帐解决了分离的 HEAD 状态问题并简化了推送到原点.

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

如果您需要推送事件来触发其他工作流程,请使用 repo 范围内的 个人访问令牌.

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(原始答案)

问题在于 actions/checkout@v1 操作使 git 存储库处于分离的 HEAD 状态.有关更多详细信息,请参阅此问题:https://github.com/actions/checkout/issues/6

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

您可能还需要结帐.您可以从 GITHUB_REF 中提取分支名称:

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

顺便说一句,我已经写了一个 GitHub 动作,它可以帮助你实现你想做的事情.它将在工作流程期间在本地进行任何更改,将它们提交到新分支并提出拉取请求.https://github.com/peter-evans/create-pull-request

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

另请参阅此相关问题和答案.从 GitHub 操作推送到源

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

这篇关于无法提交和推回 github 操作所做的更改(无效用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆