Github actions - remote git push not working

Viewed 776

I'm using github actions to push some code in my other git repo.

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Push remote
        run: |
          git config user.name "myusername"
          git config user.email "myusername@gmail.com"
          git remote set-url origin "https://myusername:$GIT_TOKEN@github.com/myusername/AnotherRepo"
          git add mytext.txt
          git commit -m "Pushing mytext with remote push"
          git push

GIT_TOKEN is set as repo secrets of the current git repo. In this token I've provided all permissions.

Yet I'm getting error:

fatal: repository 'https://github.com/myusername/AnotherRepo.git/' not found.

From the answer here it seems it is a permission issue.

But both of these repos belong to me so why am I getting this error.

If I try to push file in the same source repo (where git action is running) then there is no issue.

So there is no issue in the action yaml.

What am I missing?

PS. I've also tried with ${{ secrets.GIT_TOKEN }}

1 Answers

If you want to push code in another repo inside your current repo's GHA you shall checkout your code in another folder (use path to achieve this) and use this folder to push your code (use working-directory to achieve this). E.g:

- name: Checkout repository B
  uses: actions/checkout@v2
  with:
      path: './folder-B'
      token: ${{secrets.GITHUB_TOKEN}}
- name: Push remote
  working-directory: './folder-B'
  run: |
      git config user.name "myusername"
      git config user.email "myusername@gmail.com"
      git remote set-url origin "https://myusername:$GIT_TOKEN@github.com/myusername/AnotherRepo"
      git add mytext.txt
      git commit -m "Pushing mytext with remote push"
      git push
Related