Posts Github Action to push to another repository - Usage and Example
Post
Cancel

Github Action to push to another repository - Usage and Example

In this walkthrough, we will push the changes made on one github repo to another repo using github action - github-action-push-to-another-repository.

Here’s the Use Case

Suppose there are two comparable repositories and we aim to transfer code to the main repository from splitted repository without revealing the main repository. This is a sensible approach when a new developer needs to be familiarized with the repository, but the repository owner does not want to reveal all the information about it initially.

Let’s start with this case scenario.

github-action-push-to-another-repository

Few things to take note of:

  • Repo A
    • Repo A is a parent repo where Dev. A and Dev. B are working since Mesozoic era.
    • It is the main parent repo where we would want to push the code from Repo B.
  • Repo B
    • This repo is the splitted repo from Repo A where owner of the Repo A would like to strip features and provide this repo to new developer without disclosing what Repo A is all about.
  • Final Working Github Action Yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
name: CI

on:
  push:
    branches: [ dev ]
  pull_request:
    branches: [ dev ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Push pages
        id: push_pages
        uses: cpina/github-action-push-to-another-repository@ssh-deploy-key
        env:
          SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
          GH_USERNAME: ${{ secrets.GH_USERNAME }}
          USER_EMAIL: ${{ secrets.USER_EMAIL }}
          DESTINATION_REPO_NAME: ${{ secrets.DESTINATION_REPO_NAME }}
        with:
          source-directory: 'pages'
          destination-github-username: '${{ secrets.GH_USERNAME }}'
          destination-repository-name: '${{ secrets.DESTINATION_REPO_NAME }}'
          user-email: ${{ secrets.USER_EMAIL }}
          commit-message: See ORIGIN_COMMIT from $GITHUB_REF
          target-branch: dev
          target-directory: 'pages/dashboard'
      - name: Push assets
        id: push_assets
        uses: cpina/github-action-push-to-another-repository@ssh-deploy-key
        env:
          SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
          GH_USERNAME: ${{ secrets.GH_USERNAME }}
          USER_EMAIL: ${{ secrets.USER_EMAIL }}
          DESTINATION_REPO_NAME: ${{ secrets.DESTINATION_REPO_NAME }}
        with:
          source-directory: 'assets'
          destination-github-username: '${{ secrets.GH_USERNAME }}'
          destination-repository-name: '${{ secrets.DESTINATION_REPO_NAME }}'
          user-email: ${{ secrets.USER_EMAIL }}
          commit-message: See ORIGIN_COMMIT from $GITHUB_REF
          target-branch: dev
          target-directory: 'assets/'
      - name: Test changes move from test-push to test-pull
        run: echo $DESTINATION_CLONED_DIRECTORY

Note: We are using github action cpina/github-action-push-to-another-repository to push the changes. If you need further exploration follow the link for more detailed documentation.

Breaking down YAML action configuration

When to execute the action?

1
2
3
4
5
6
7
8
name: CI

on:
  push:
    branches: [ dev ]
  pull_request:
    branches: [ dev ]

How are we executing the action?

  • Either using ssh-deploy-key
  • Or, GitHub Access Token

  • Each name record can be configured to push directory or file
  • Here, we are pushing pages/ directory, if you see complete yaml we are pushing pages/ and assets/ directories to the Repo A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
      - name: Push pages
        id: push_pages
        uses: cpina/github-action-push-to-another-repository@ssh-deploy-key
        env:
          SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
          GH_USERNAME: ${{ secrets.GH_USERNAME }}
          USER_EMAIL: ${{ secrets.USER_EMAIL }}
          DESTINATION_REPO_NAME: ${{ secrets.DESTINATION_REPO_NAME }}
        with:
          source-directory: 'pages'
          destination-github-username: '${{ secrets.GH_USERNAME }}'
          destination-repository-name: '${{ secrets.DESTINATION_REPO_NAME }}'
          user-email: ${{ secrets.USER_EMAIL }} # Email of the commit author
          commit-message: See ORIGIN_COMMIT from $GITHUB_REF
          target-branch: dev # To which branch we are sending the commits to
          target-directory: 'pages/dashboard' # Make sure this repository exists on the parent repo

Example: Here’s the working github repo that mimics above mentioned repos

Repo ARepo B
try-pull-another-repotry-push-another-repo
-This is where we keep github yaml workflow
-.github/workflows/ exists here
Dev A. and Dev B. are working hereNew Dev is working here
Note: To make any changes on these shared directories Dev A. and Dev B.
must work on Repo B and contribute from there,
because each time the code is pushed, target directories are wiped
 
  
This post is licensed under CC BY 4.0 by the author.