【问题标题】:How to push to another repository in github actions?如何在 github 操作中推送到另一个存储库?
【发布时间】:2023-03-13 16:05:01
【问题描述】:

在 Github Actions 中,我尝试对工作流所属的存储库之外的不同存储库进行一些更改。请参阅此代码:

      - name: Generate API module
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |

          git clone https://user:$GITHUB_TOKEN@github.com/owner/my-repo # This works

          cd my-repo
          git config user.name "user"
          git config user.email "dev@example.com"

          git checkout -b api-version-$COMMIT

          touch new-file.sh
          git add new-file.sh
          git commit -m "Add new file"

          git remote -v # Prints:
          # origin ***github.com/owner/my-repo.git (fetch)
          # origin ***github.com/owner/my-repo.git (push)

          git push --set-upstream origin api-version-$COMMIT # This does not work
          git push --set-upstream https://user:$GITHUB_TOKEN@github.com/owner/my-repo api-version-$COMMIT # This does not work either

我可以很好地克隆所有者/repo 存储库。我可以签出新分支,添加文件并提交更改。当我尝试将更改推送到上游时会出现问题。这根本不起作用,我收到此错误:

remote: Repository not found.
fatal: repository 'https://github.com/owner/my-repo/' not found

我只是猜测它与身份验证有关。 GITHUB_TOKEN 是个人访问令牌,具有所有可能的权限。

什么给了?

【问题讨论】:

    标签: git github-actions


    【解决方案1】:

    GITHUB_TOKEN 也是一个系统变量,参见文档here。您可能会在您的秘密和系统秘密之间遇到一些冲突。将您的密码重命名为其他名称可能会起作用。

    【讨论】:

    • 谢谢,但很遗憾没有。如果变量被命名为其他名称,则结果相同。
    • 您确定令牌具有所有必需的权限吗?
    • 肯定的,重新创建了几次!我从来不知道出了什么问题。我最终做的是将所有代码合并到一个仓库中。
    【解决方案2】:

    不确定你是否能够解决这个问题,但我想我会发布一个潜在的解决方案,供未来遇到同样问题的人使用,就像我一样。 我遇到了同样的问题,最终证明是我试图推送的仓库缺乏编辑权限。 不幸的是,来自 GitHub (remote: Repository not found.) 的错误消息对于指出真正的问题确实没有帮助(我认为这是出于安全目的而混淆的问题,但是如果该帐户已经能够clone repo 并已阅读访问它——混淆的意义何在?这显然是一个已经通过身份验证的用户的授权问题??‍♂️)。 无论哪种方式 - 我都能够通过获得对 repo 的写访问权来解决这个问题。 获得所需权限后 - 我能够成功推送到该仓库。

    【讨论】:

    • 从未想过。我创建了一个 monorepo 以使其工作。
    【解决方案3】:

    有一个额外的配置可以在AUTHORIZATION 标头中静默传递$GITHUB_TOKEN,它与另一个repo git auth 交互不良。

    您需要在推送到另一个 repo 之前将其删除:

    git config --unset-all http.https://github.com/.extraheader
    

    解决方案取自另一个 SO 线程 Auth error trying to copy a repo with Github Actions

    【讨论】:

      【解决方案4】:

      对此有一个动作:

      name: Push File(or Dir) to another repository
      
      on: push
      
      jobs:
        copy-file:
          runs-on: ubuntu-latest
          steps:
          - name: Checkout
            uses: actions/checkout@v2
      
          - name: Push to another repo
            uses: dmnemec/copy_file_to_another_repo_action@main
            env:
              API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }} 
            with:
              source_file: 'test2.md'
              destination_repo: 'another_repo/release-test'
              destination_folder: 'test-dir' # optional
              user_email: 'example@email.com'# your email
              user_name: 'dmnemec'           # your login
              commit_message: 'A custom message for the commit'
      

      https://github.com/dmnemec/copy_file_to_another_repo_action

      【讨论】:

        猜你喜欢
        • 2021-01-01
        • 1970-01-01
        • 2021-04-09
        • 2012-06-10
        • 1970-01-01
        • 1970-01-01
        • 2012-08-25
        • 1970-01-01
        • 2013-11-18
        相关资源
        最近更新 更多