How to push to another repository in github actions?

Viewed 11243

In Github Actions, I'm trying to make some changes to a different repo than the repository the workflow belongs to. See this code:

      - 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

I can clone the owner/repo repository just fine. I can checkout the new branch, add the file and commit the changes. The problem occurs when I try to push the changes upstream. That doesn't work at all, and I get this error:

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

I'm just guessing that it has something to do with authentication. The GITHUB_TOKEN is a personal access token and has all possible permissions.

What gives?

6 Answers

There is an extra configuration to silently pass $GITHUB_TOKEN in AUTHORIZATION header, which interacts badly with another repo git auth.

You need to remove it before pushing to another repo:

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

Solution taken from another SO thread Auth error trying to copy a repo with Github Actions

Not sure if you were able to solve that yet, but figured I'll post one potential solution for future folks that come across that same issue, as did I. I had the same issue, which eventually turned out to be lack of edit permissions on the repo to which I was trying to push. Unfortunately the error message from GitHub (remote: Repository not found.) was really unhelpful in pointing out the real issue (I assume it's a matter of obfuscation for security purposes, but then- if that account was already able to clone the repo and have read access to it- what's the point of obfuscation? This is obviously an authorization issue for an already authenticated user ‍♂️). Either way- I was able to solve this on my end by gaining write access to the repo. After getting the needed permissions - I was able to successfully push into that repo.

GITHUB_TOKEN is also a system variable, see the doc here. You might be running into some conflict between your secret and the system one. Renaming your secret to something else might work.

you need to create a GitHub Private Access Token with permissions for repositories and store it in a secret, let's say ACTIONS_GITHUB_TOKEN in the repository you are running your workflow/action in.

then pass this token to the checkout action:

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
# .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

There is a action for this:

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

Related