如何在Github Actions中将代码推送到另一个存储库?

23
在 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

我可以成功克隆所有者/存储库。我可以检出新分支,添加文件并提交更改。问题发生在我尝试将更改推送到上游时。完全不起作用,并且我会收到此错误:

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

我猜想这可能与身份验证有关。 GITHUB_TOKEN 是个人访问令牌,拥有所有可能的权限。

怎么回事?

6个回答

9

在IT技术方面有一个额外的配置,可以将$GITHUB_TOKEN静默传递到AUTHORIZATION头中,在与另一个存储库的git auth产生冲突时需要注意。

在将代码推送到另一个存储库之前,您需要将其删除:

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

本解决方案摘自另一个SO线程(点击此处查看)


4

您需要创建一个具有存储库权限的GitHub私人访问令牌,并将其存储在一个名为ACTIONS_GITHUB_TOKEN的密钥中,该密钥位于您正在运行工作流/操作的存储库中。

然后将此令牌传递给checkout操作:

push:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v3
      with:
        repository: <repo name>
        ref: 'master'
        token:  ${{ secrets.ACTIONS_GITHUB_TOKEN }}
    - name: setup git config
      run: |
        git config user.name "GitHub Actions Bot"
        git config user.email "<>"
    - <make changes and commit >
    - run: git push origin master

4

不确定您是否已经解决了这个问题,但是我觉得我会发布一个潜在的解决方案,供未来遇到同样问题的人参考,就像我一样。

我遇到了同样的问题,最终发现问题是我试图推送到的仓库缺少编辑权限。

不幸的是,GitHub 的错误信息(remote: Repository not found.)对于指出真正的问题非常无用(我猜这是出于安全目的而进行的混淆操作,但如果该帐户已经能够clone该仓库并获得了读取权限,那么混淆的目的是什么呢?显然,这是已经验证用户的授权问题 ‍♂️)。

不管怎样,在获得写入仓库的权限后,我成功解决了此问题。

在获得所需权限之后,我能够成功地将内容推送到该仓库中。


从来没有想到过。我创建了一个单体库以使其工作。 - Bjørn Olav Jalborg

3
# .github/workflows/deploy.yml
name: Deploy GitHub Pages to external repository

on:
  # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on
  #  push:
  #    branches: [ "main" ]
  push:
    tags:
      - "*"

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x]

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: yarn

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Build website
        run: yarn build

      - name: Deploy to external repository
        uses: cpina/github-action-push-to-another-repository@main
        env:
          # ️ <internal repository>, run GitHub Action.
          #  <external repository>, receiving Artifacts.
          #
          # Way 1: using Personal Access Token
          # @see https://cpina.github.io/push-to-another-repository-docs/setup-using-personal-access-token.html#setup-personal-access-token
          # 1.1 Generate Personal Access Token: <external repository>/<Avatar>/Settings/Developer settings/Personal access tokens/Generate new token
          #     Select `No expiration` and Check `✅️ repo    Full control of private repositories`, Generate and then Copy the Token
          # 1.2 Then make the token available to the GitHub Action: <internal repository>/Settings/Secrets/Actions/New repository secret
          #     Name: EXTERNAL_REPOSITORY_PERSONAL_ACCESS_TOKEN, Value Paste the Token
          API_TOKEN_GITHUB: ${{ secrets.EXTERNAL_REPOSITORY_PERSONAL_ACCESS_TOKEN }}
          # Way 2: using SSH deploy keys
          # @see https://cpina.github.io/push-to-another-repository-docs/setup-using-ssh-deploy-keys.html#setup-ssh-deploy-keys
          # 2.1 Generate an SSH key in terminal (Leave the passphrase empty)
          # 2.2 Add public key in the external repository: <external repository>/Settings/Deploy keys/Add deploy key
          #     Name: DEPLOY_PUBLIC_KEY, Value Paste the public key. Enable "✅️ Allow write access"
          # 2.3 Add private key in the source repository: <external repository>/Settings/Secrets/Actions/New repository secret
          #     Name: DEPLOY_PRIVATE_KEY, Value Paste the private key.
          # SSH_DEPLOY_KEY: ${{ secrets.DEPLOY_PRIVATE_KEY }}

        with:
          # GitHub Action output files
          source-directory: public/
          destination-github-username: <external org/user name>
          destination-repository-name: <external repository>
          user-email: <your email>
          # It defaults to `main`
          target-branch: "gh-pages"

      - name: Test get variable exported by push-to-another-repository
        run: echo $DESTINATION_CLONED_DIRECTORY

1

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


谢谢,但遗憾的是不行。如果变量名叫其他名称也会得到相同结果。 - Bjørn Olav Jalborg
你确定该令牌拥有所有必需的权限吗? - Davide D'Alto
积极的是,我已经多次重新创建它!我从未弄清楚问题出在哪里。最终我做的是将所有代码合并到一个仓库中。 - Bjørn Olav Jalborg

-1

这里有一个操作:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接