Run GitHub Actions on forked repository for pushing certain changes

Hi everyone,

This is more of a question for everyone. I want to run GitHub Action on the forked repo for a particular PR. The action should push something on the forked repo before merging the PR. Is this possible?
I know that the base repo will not have the permission to push in the forked repo so we need to run the actions of forked repo for this use case. So how could we do that?

I was trying with the following yaml:

name: Actions-CI
on:
  issue_comment:
    types: [created]

jobs:
  tests:
    if: contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/run-e2e')
    runs-on: ubuntu-latest
    steps:

      #Using the last commit id of pull request
      - uses: octokit/request-action@v2.x
        id: get_PR_commits
        with:
          route: GET /repos/:repo/pulls/:pull_number/commits
          repo: ${{ github.repository }}
          pull_number: ${{ github.event.issue.number }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: set commit to output
        id: getcommit
        run: | 
           prsha=$(echo $response | jq '.[-1].sha'  | tr -d '"')
           echo "::set-output name=sha::$prsha" 
        env: 
          response:  ${{ steps.get_PR_commits.outputs.data }}
          
# checkout on the lastest commit 
      - uses: actions/checkout@v2
        with:
          ref: ${{steps.getcommit.outputs.sha}}

# setup golang
      - uses: actions/setup-go@v2
        with:
          stable: 'false'
          go-version: '1.14.0'

# run embedmd (this will write something in the file)
      - name: Installing and Running embedmed
        run: |
          go get github.com/campoy/embedmd
          cd docs
          embedmd -w $(find *.md)


      - name: Commit files
        run: |
          git config --local user.email "githubactions@github.com"
          git config --local user.name "Myname"
          git commit -s -m "Run embedmed" -a
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

Thanks !

Hi @uditgaurav,

When you create a PR from forked repo to base repo, the PR is created on the BASE repo not forked repo. Therefore, if you comment to PR, the ‘issue_comment’ will only be triggered on base repo(if you put it on base repo default branch), not on forked repo.

On forked repo, you need to enable github action on ‘actions’ tab firstly. And then you can create a workflow (eg: event push…etc) on forked repo, to commit changes before PR merge.

If you don’t have forked repo access token, you cannot commit to forked repo.

If you have forked repo token, on base repo, you can checkout forked branch(get info from Github Context), commit the changes to forked repo before PR merged.

Thanks

1 Like