Search code examples
gitgithubgithub-actions

Merging two git repository into another with Github actions


I'm trying to merge 2 repository into a third one with the help of GitHub Actions. Note that the 3 repositories are private.

I tried to do it using secrets, but when I try to merge from the third repository, it cannot find any of the two other. Here is the workflows of the repositories:

Any of the first two:

name: Push to Third repo

on:
  push:
    branches:
      - main

jobs:
  trigger:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Trigger the third repository
        run: |
          curl -X POST \
          -H "Authorization: token ${{ secrets.MY_SECRET_TOKEN }}" \
          -H "Accept: application/vnd.github.v3+json" \
          https://api.github.com/repos/MyOrganisation/ThirdRepo/dispatches \
          -d '{"event_type": "event_name_first"}'

And for the third one:

name: Merge Changes to Third

on:
  repository_dispatch:
    types: 
      - event_name_first
      - event_name_second

jobs:
  merge:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Configure git
        run: |
          git config --global user.name "github-actions"
          git config --global user.email "[email protected]"

      - name: Add remote repository
        run: |
          git remote add first https://github.com/MyOrganisation/first.git
          git remote add second https://github.com/MyOrganisation/second.git

      - name: Fetch latest changes from first or second
        run: |
          if [[ "${{ github.event.action }}" == "event_name_first" ]]; then
            git fetch first main
            git merge first/main --no-edit
          elif [[ "${{ github.event.action }}" == "event_name_second" ]]; then
            git fetch second main
            git merge second/main --no-edit
          fi

      - name: Push to main
        run: |
          git push origin main

An this is the error:

remote: Repository not found.
fatal: repository 'https://github.com/MyOrganisation/first.git/' not found
Error: Process completed with exit code 128.

I am sure of the names of every url. I suspect a missing permission on the secret token, even thought I gave it all repo and workflow rights.


Solution

  • Your third workflow does not have permission to access any other repo but the repo where it is running. Token provided by GitHub Actions to each workflow is scoped to the repo where the workflow is defined.

    You need to obtain Personal Access Token for each other repo. If you are a member of the first and second repos, you can use your own token. Otherwise you need a token from a member of both repos or two tokens from two different members.

    The easiest way to apply these tokens with your implementation is the following:

    git remote add first https://${{ secrets.TOKEN1 }}@github.com/MyOrganisation/first.git
    git remote add second https://${{ secrets.TOKEN2 }}@github.com/MyOrganisation/second.git
    

    These secrets must be defined in the third repo.