Mirroring Your Code Repository To External Targets
Hey folks! Let's talk about keeping your code safe and sound. Specifically, we're diving into how to automatically mirror your Komunumo repository's source code from the main branch to two external Git servers. This is super important for backups, redundancy, and making sure your code is accessible from multiple places. We'll be using the yesolutions/mirror-action GitHub Action to make this happen, which simplifies the whole process. So, get ready to learn how to set up automatic mirroring to codeberg.org and git.fihlon.swiss.
Setting Up the Mirroring Process
Understanding the Goal
The main goal here is straightforward: duplicate your code on multiple platforms. Think of it like having copies of your notes so that if one copy gets lost, you still have the others. We want to ensure that every time you push changes to the main branch of your Komunumo repository, those changes are automatically reflected on both codeberg.org and git.fihlon.swiss. This means that if something happens to GitHub, you still have your code safely backed up elsewhere. This approach also helps to improve your project's overall resilience.
Choosing the Right Tools
We will be leaning on the yesolutions/mirror-action GitHub Action. It is designed to make mirroring Git repositories as easy as possible. You can find it here. The beauty of this action lies in its automation capabilities. Once set up, the action will automatically run whenever you push changes to your main branch. This saves you from having to manually sync your repositories and ensures that your backups are always up-to-date. In addition to the automatic triggers on push, we'll also set up a manual trigger, allowing you to manually kick off the mirroring process if needed. This is useful for situations such as after target maintenance.
Step-by-Step Guide
- Workflow Creation: Start by creating a new GitHub Actions workflow file in your repository. This file will define the steps that the
mirror-actionwill follow. You'll typically place this file in the.github/workflows/directory of your repository. If the directories don't exist, feel free to create them! - Trigger Configuration: Specify the events that should trigger the workflow. In our case, we want the workflow to run on every
pushto themainbranch and also allow for manual triggering. This way, you get the best of both worlds – automated updates and the flexibility to initiate the process manually when needed. - Action Configuration: Configure the
yesolutions/mirror-actionto mirror yourmainbranch to the designated targets:codeberg.organdgit.fihlon.swiss. This involves providing the necessary information, such as the Git URLs of the target repositories and the authentication details (more on this below). - Authentication Setup: This is a crucial step! We'll use SSH keys to authenticate with the target Git servers. Store your private keys as GitHub secrets (e.g.,
MIRROR_CODEBERG_SSH_KEY,MIRROR_FIHLON_SSH_KEY). Add the corresponding public keys to the target repositories with write permissions. This method gives you an extra layer of security and is generally the best practice. - Security and Verification: Make sure you handle host verification to prevent man-in-the-middle attacks. It's really important to confirm that you are connecting to the correct server. Ensure the mirror completes successfully and verify the latest commit on the
mainbranch of both target repositories to confirm everything is working smoothly.
Deep Dive into Requirements and Configurations
Branch and Target Specifics
It is super important to only mirror the main branch. This prevents unnecessary data from being transferred and keeps the mirrored repositories clean. The workflow should target two separate destinations: codeberg.org and git.fihlon.swiss. Each mirror target will have its own unique configuration. The beauty of mirroring only the main branch is that you are only replicating the current, active version of your code. It keeps things simple and makes it easy to track changes.
Authentication and Security Best Practices
Security is paramount, and using SSH keys is the recommended way to go. Here’s a detailed breakdown:
- Deploy Keys: Generate separate deploy keys for each target. This practice follows the principle of least privilege, meaning that each key has access only to what it needs. This minimizes the impact if one key gets compromised.
- GitHub Secrets: Store your private keys as GitHub secrets. Never commit your private keys directly into your repository! GitHub secrets provide a secure way to store sensitive information, such as SSH keys. Use descriptive names for your secrets like
MIRROR_CODEBERG_SSH_KEYandMIRROR_FIHLON_SSH_KEYso you know what each secret is used for. - Public Key Installation: Add the corresponding public keys to the target repositories. Make sure the public keys have write permissions. This enables the mirror action to push changes to the repositories.
- Host Key Verification: Implement host key verification to enhance security. It ensures that you are connecting to the correct server. You can add the host keys of the target servers to your
known_hostsfile or configure your SSH settings to verify the host keys. This prevents man-in-the-middle attacks.
Preserving Commit History
The yesolutions/mirror-action is designed to preserve commit authorship and timestamps. This is really crucial for maintaining the integrity of your code history. This means the mirrored repositories will retain the original author and commit dates. This will help with debugging and understanding the development history of your project.
Manual Dispatch
Purpose of Manual Dispatch
In addition to the automated push trigger, it's really beneficial to have a manual dispatch option. Manual dispatch provides the flexibility to rerun the mirroring process whenever needed. This is useful for various scenarios, such as when the target servers have maintenance or when you want to resynchronize the repositories for any reason.
Implementing Manual Triggers
Configuring a manual dispatch is straightforward. You add the workflow_dispatch event to your workflow file. This enables the