如何在 github 操作中推送到另一个存储库?

Bjø*_*org 7 git github-actions

在 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
Run Code Online (Sandbox Code Playgroud)

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

remote: Repository not found.
fatal: repository 'https://github.com/owner/my-repo/' not found
Run Code Online (Sandbox Code Playgroud)

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

是什么赋予了?

Vla*_*lav 10

有一个额外的配置可以静默传入$GITHUB_TOKENheader AUTHORIZATION,这与另一个 repo git auth 交互很差。

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

git config --unset-all http.https://github.com/.extraheader
Run Code Online (Sandbox Code Playgroud)

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


Ali*_*ssa 8

您需要创建一个具有存储库权限的 GitHub 私有访问令牌并将其存储在秘密中,假设ACTIONS_GITHUB_TOKEN在您正在运行工作流程/操作的存储库中。

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

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
Run Code Online (Sandbox Code Playgroud)


山茶树*_*葡萄树 5

# .github/workflows/deploy.yml\nname: Deploy GitHub Pages to external repository\n\non:\n  # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on\n  #  push:\n  #    branches: [ "main" ]\n  push:\n    tags:\n      - "*"\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    strategy:\n      matrix:\n        node-version: [18.x]\n\n    steps:\n      - uses: actions/checkout@v3\n\n      - name: Use Node.js ${{ matrix.node-version }}\n        uses: actions/setup-node@v3\n        with:\n          node-version: ${{ matrix.node-version }}\n          cache: yarn\n\n      - name: Install dependencies\n        run: yarn install --frozen-lockfile\n\n      - name: Build website\n        run: yarn build\n\n      - name: Deploy to external repository\n        uses: cpina/github-action-push-to-another-repository@main\n        env:\n          # \xef\xb8\x8f <internal repository>, run GitHub Action.\n          #  <external repository>, receiving Artifacts.\n          #\n          # Way 1: using Personal Access Token\n          # @see https://cpina.github.io/push-to-another-repository-docs/setup-using-personal-access-token.html#setup-personal-access-token\n          # 1.1 Generate Personal Access Token: <external repository>/<Avatar>/Settings/Developer settings/Personal access tokens/Generate new token\n          #     Select `No expiration` and Check `\xe2\x9c\x85\xef\xb8\x8f repo    Full control of private repositories`, Generate and then Copy the Token\n          # 1.2 Then make the token available to the GitHub Action: <internal repository>/Settings/Secrets/Actions/New repository secret\n          #     Name: EXTERNAL_REPOSITORY_PERSONAL_ACCESS_TOKEN, Value Paste the Token\n          API_TOKEN_GITHUB: ${{ secrets.EXTERNAL_REPOSITORY_PERSONAL_ACCESS_TOKEN }}\n          # Way 2: using SSH deploy keys\n          # @see https://cpina.github.io/push-to-another-repository-docs/setup-using-ssh-deploy-keys.html#setup-ssh-deploy-keys\n          # 2.1 Generate an SSH key in terminal (Leave the passphrase empty)\n          # 2.2 Add public key in the external repository: <external repository>/Settings/Deploy keys/Add deploy key\n          #     Name: DEPLOY_PUBLIC_KEY, Value Paste the public key. Enable "\xe2\x9c\x85\xef\xb8\x8f Allow write access"\n          # 2.3 Add private key in the source repository: <external repository>/Settings/Secrets/Actions/New repository secret\n          #     Name: DEPLOY_PRIVATE_KEY, Value Paste the private key.\n          # SSH_DEPLOY_KEY: ${{ secrets.DEPLOY_PRIVATE_KEY }}\n\n        with:\n          # GitHub Action output files\n          source-directory: public/\n          destination-github-username: <external org/user name>\n          destination-repository-name: <external repository>\n          user-email: <your email>\n          # It defaults to `main`\n          target-branch: "gh-pages"\n\n      - name: Test get variable exported by push-to-another-repository\n        run: echo $DESTINATION_CLONED_DIRECTORY\n
Run Code Online (Sandbox Code Playgroud)\n


Luc*_*cas 1

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