Seeking for guidance - commit to repoA creates a branch from RepoB and makes a PR to RepoB

Hi,

I am new to Github Action and looking for guidance. We have a requirement:

Repo A
Repo B

repoA stores a template file
repoB stores artifacts generated from repoA’s template file

committing to Repo A shall trigger action that:

  1. do some work and generate a artifacts (some files)
  2. repoB stores the artifacts. RepoA’s newly generated artifacts shall be in a branch in repoB and PR to repoB’s main branch

Could I have some guidance if this is possible and how to start?

Thanks.

Could you clarify what you mean by this? Do you want to automatically mirror branches from A to B, without any pull request?

1 Like

thanks for the reply.

repoA stores a template file
repoB stores artifacts generated from repoA’s template file

Repo A’s action shall

  1. do some work and generate a artifacts (some files)
  2. repoB stores the artifacts. RepoA’s newly generated artifacts shall be in a branch in repoB and PR to repoB’s main branch

This is a rough outline of how you could design the workflow. Note that you need to use a repo scoped Personal Access Token to create the pull request in a remote repository.

This workflow goes in repo-a:

on:
  push:
    branches: [master]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # Checkout repo-a
      - uses: actions/checkout@v2

      # Steps that use the template to build artifacts go here

      # Save the artifact
      - uses: actions/upload-artifact@v2
        with:
          name: artifact
          path: path/to/artifact

      # Checkout the default branch of repo-b
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.PAT }}
          repository: owner/repo-b

      # Download the artifact to the correct path
      - uses: actions/download-artifact@v2
        with:
          name: artifact
          path: path/to/download/artifact

      # Raise a pull request from artifact-update to the default branch
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.PAT }}
          branch: artifact-update

If you prefer, instead of using upload-artifact and download-artifact you can just checkout repo-b to a different path that doesn’t overwrite repo-a and then copy the artifact to the right place. If you do that, make sure to use the path input of create-pull-request action so it’s pointing to where repo-b is checked out.

2 Likes

thank you. It is so detailed.
I am going to try now.
Thank you again.

Now I am thinking again…I wonder if I shall checkout repoB at the first place since repoA generates the new artifact and it will be sent to repoB in PR?

I am considering a scenario where:

  1. repoB has: A B C
  2. now repoA generates A B D
  3. if I download upload/download artifacts, won’t my path now contains: A B C D, but I just want repoB’s latest master to contain only: A B D

Thanks

You will need to checkout repo B to work with it. If it contains artifacts you don’t want any more you’ll have to delete them, like when working with git locally. If you want only the files from the artifact you can simply do something like

- name: Delete old artifacts
  run: |
    rm path/to/download/artifact/*

to delete all of them. I’m not familiar with the peter-evans/create-pull-request action, so I don’t know if it’ll pick up deletion automatically, or if you’ll need to use git rm.

thank you for the reply. I end up:

  1. create a folder under repoB current_artifact

In the workflow:

  1. repoA generate new artifact to new_artifact
  2. checkout repoB
  3. rsync new_artifact current_artifact
  4. PR to repoB
1 Like