mat
mat

Reputation: 2617

GitHub actions push to remote repo

How can I push some files that were generated by the runner (user1/repo1) to the main branch from another remote repo (user2/repo1) via GitHub actions?

Please note that:

This is what I have tried so far:

name: gitaction

on:
  schedule:
    - cron:  "*/30 * * * *"
  workflow_dispatch:
    
jobs: 
  genFileAndPush:
    runs-on: macOS-latest

    steps:
    - uses: actions/checkout@master
    - uses: r-lib/actions/setup-r@master
      with:
         r-version: '4.1.2'

    - name: Run R scripts and generate file
      run: |
        saveRDS(1:3, file = "file.rds")
      shell: Rscript {0}

    - name: Push to remote repository
      run: |
        git config --local user.name actions-user
        git config --local user.email "[email protected]"
        git add file.rds
        git commit -m "commit"
        git remote set-url origin https://[email protected]/user2/repo1.git
        git push -u origin main
      env:
        REPO_KEY: ${{secrets.ACCESS_TOKEN}}
        username: github-actions

It returns the following error:

remote: Permission to user2/repo1.git denied to github-actions[bot].
fatal: unable to access 'https://github.com/user2/repo1.git/': The requested URL returned error: 403
Error: Process completed with exit code 128.

What am I missing?


Edit
As suggested, I tried using GuillaumeFalourd/[email protected]:

name: gitaction

on:
  workflow_dispatch:
    
jobs: 
  genFileAndPush:
    runs-on: macOS-latest

    steps:
    - uses: actions/checkout@master
    - uses: r-lib/actions/setup-r@master
      with:
         r-version: '4.1.2'

    - name: Run R scripts and generate file
      run: |
        saveRDS(1:3, file = "file.rds")
      shell: Rscript {0}

    - uses: actions/[email protected]
    - uses: GuillaumeFalourd/[email protected]
      with:
        target_branch: main
        files: file.rds
        remote_repository: https://github.com/user2/repo1
        access_token: ${{secrets.ACCESS_TOKEN}}
        force: true

Although there were no error, the file was not pushed (because it was not detected?):

Run GuillaumeFalourd/[email protected]
Run CURRENT_BRANCH=${GITHUB_REF}
WARNING: No changes were detected. git commit push action aborted.

Upvotes: 1

Views: 1510

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 22890

There are some actions on the Github Marketplace that can help you with pushing files to other repositories.

The workflow would look like this:

name: gitaction

on:
  workflow_dispatch:
    
jobs: 
  genFileAndPush:
    runs-on: macOS-latest

    steps:
    - uses: actions/checkout@master
    - uses: r-lib/actions/setup-r@master
      with:
         r-version: '4.1.2'

    - name: Run R scripts and generate file
      run: |
        saveRDS(1:3, file = "file.rds")
      shell: Rscript {0}

    - uses: GuillaumeFalourd/[email protected]
      with:
        target_branch: main
        files: file.rds
        remote_repository: https://github.com/user2/repo1
        access_token: ${{secrets.ACCESS_TOKEN}}
        force: true

Otherwise, you can also perform the whole operation manually using command lines to clone the remote repository, copy the files from the local repo wherever you want on the remote repo, then push the new files to the remote repository.

Upvotes: 3

Related Questions