无法提交和推回 github 操作所做的更改(无效用户)

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

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

    - 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

但是,即使我正在配置用户和电子邮件,我仍然会收到以下错误。

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

[error]Process completed with exit code 128.

请注意,它在 macOS-latest 上运行并使用预打包的 git。

actions/checkout@v2

版本 2 的结帐解决了分离的 HEAD 状态问题并简化了推送到源的过程。

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 作用域 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

我成功使用的解决方法是按如下方式设置遥控器:

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

您可能还需要结帐。您可以从 GITHUB_REF:

中提取分支名称
git checkout "${GITHUB_REF:11}"

这里有一个完整的例子来演示。

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

另请参阅此相关问答。