3

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 }}

sonic boom
  • 764
  • 4
  • 11
  • 26
  • Are you sure `GIT_TOKEN` contains a personal access token? Docs on how to create one: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token . Also make sure the token has the correct permissions. – Bjorn Jan 30 '22 at 10:22
  • @Bjorn, I've followed this docs only & basically gave it all the permissions. – sonic boom Jan 30 '22 at 10:47
  • Does it work on your own computer? I.e. add the remote with token to a local repository, and try to push? – Bjorn Jan 30 '22 at 14:02
  • 1
    You can't use the secret directly in the workflow like this. You need to use the `${{ secrets.GIT_TOKEN }}` stntax somewhere to retrieve the secret value. There are two ways to do it, or adding `env: GIT_TOKEN: ${{ secrets.GIT_TOKEN }}` at the step level to use it as you did in the run command, or using directly `${{ secrets.GIT_TOKEN }}` in the step run command. – GuiFalourd Jan 30 '22 at 15:47

1 Answers1

0

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
vsr
  • 1,025
  • 9
  • 17