Sunforger

Sunforger

Submit code from Repository A to Repository B using GitHub Actions

GitHub Actions is a continuous integration service launched by GitHub. For tutorials, you can refer to the Getting Started with GitHub Actions or the official documentation.

We may want to trigger a workflow on repository A to submit (deploy) all or part of the code from repository A to another repository B, which requires consideration of different scenarios.

Repository A#

Whether repository A is public or private does not affect the entire workflow. When creating a workflow, GitHub automatically creates a secret to support repository access. The secret is retrieved from the context using ${{ secrets.GITHUB_TOKEN }} (example). Secret permissions can be adjusted as needed.

Repository B#

public#

Personal blog projects may need to deploy blog pages to GitHub Pages. In this case, we can maintain the blog source code in the private repository A and then define a workflow on repository A to compile and deploy the source code. At this point, repository B is a public repository.

# to do

This type also applies to the methods introduced in the following private section.

private#

Sometimes it may also be necessary to submit code to a private repository B.

This requires introducing a deploy key and submitting via SSH protocol.

First, you need to generate an SSH key, for example:

ssh-keygen -t ed25519

Configure the public key in Repository A > Settings > Secrets > New Repository Secret.

Configure the private key in Repository B > Settings > Deploy Keys > Add Deploy Key.

Then, construct the GitHub Action workflow in repository A as follows:

name: test

on:
  push:
    branches:
      - 'main'

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

    - name: Git init
      run: |
        git config user.name "${{ github.actor }}"
        git config user.email "${{ github.actor_id }}@users.noreply.github.com"
      ### or you can use GitHub Action bot as commit user
      # git config --local user.email "action@github.com"
      # git config --local user.name "GitHub Action"

    - name: Add SSH Key
      uses: webfactory/ssh-agent@v0.5.4
      with:
        ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}

    - name: Push to repo
      run: |
        git remote add target "git@github.com:${{ your repo path }}.git"
        git checkout --orphan temp
        git add --all
        git commit --allow-empty -m "your commit message"
        git push target temp:main --force

Just submit to GitHub for testing.


Reference: https://stackoverflow.com/questions/68590575/github-actions-remote-repo-issues

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.