DEV Community

Cover image for Automating Code Updates on a Secondary Git Platform with GitHub Actions
Nasrul Hazim Bin Mohamad
Nasrul Hazim Bin Mohamad

Posted on

Automating Code Updates on a Secondary Git Platform with GitHub Actions

GitHub Actions provides a powerful way to automate workflows directly from your GitHub repository. In this blog post, we will explore a simple yet effective GitHub Action workflow designed to push changes from a repository to a secondary Git platform. This workflow is particularly useful for automating deployments or syncing changes across multiple repositories.

Overview

The GitHub Action described in this post will:

  1. Trigger on a push to the 1.0 branch.
  2. Checkout the repository.
  3. Set up Git with a predefined username and email.
  4. Add a remote repository with credentials stored in GitHub Secrets.
  5. Push the changes to the remote repository.

Let's break down the GitHub Action step-by-step.

The GitHub Action Workflow

Below is the full GitHub Action workflow configuration. Replace the placeholders with your specific repository details.

name: Push to Secondary Repo

on:
  push:
    branches:
      - 1.0

jobs:
  push-to-secondary:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Set up Git
      run: |
        git config --global user.name 'github-actions'
        git config --global user.email 'github-actions@github.com'

    - name: Add remote repository with credentials
      env:
        SECONDARY_REPO_USERNAME: ${{ secrets.SECONDARY_REPO_USERNAME }}
        SECONDARY_REPO_PASSWORD: ${{ secrets.SECONDARY_REPO_PASSWORD }}
      run: |
        git remote add secondary "http://${{ secrets.SECONDARY_REPO_USERNAME }}:${{ secrets.SECONDARY_REPO_PASSWORD }}@example.com/path/to/your/repo.git"

    - name: Push changes
      run: |
        git push secondary 1.0
Enter fullscreen mode Exit fullscreen mode

To set up secrets in your GitHub repository, follow these steps:

  1. Navigate to Your Repository:
    Go to your GitHub repository where you want to set the secrets.

  2. Go to Settings:
    Click on the "Settings" tab at the top of the repository page.

  3. Access Secrets:
    In the left sidebar, click on "Secrets and variables" and then select "Actions".

  4. Add a New Secret:
    Click the "New repository secret" button.

  5. Enter Secret Name and Value:

    • In the "Name" field, enter the name of your secret (e.g., SECONDARY_REPO_USERNAME).
    • In the "Value" field, enter the corresponding value (e.g., your username for the secondary Git platform).
  6. Save the Secret:
    Click "Add secret" to save it.

Repeat the process to add other secrets, such as SECONDARY_REPO_PASSWORD.

For more detailed information, you can refer to the official GitHub documentation: Creating and Storing Secrets.

This guide will help you securely store and use secrets in your GitHub Actions workflows.

Detailed Breakdown

1. Triggering the Workflow

on:
  push:
    branches:
      - 1.0
Enter fullscreen mode Exit fullscreen mode

The on keyword specifies the event that triggers the workflow. In this case, the workflow triggers on a push to the 1.0 branch.

2. Defining the Job

jobs:
  push-to-secondary:
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

The jobs section defines a single job named push-to-secondary that runs on the latest Ubuntu environment.

3. Checkout Repository

steps:
  - name: Checkout Repository
    uses: actions/checkout@v4
    with:
      fetch-depth: 0
Enter fullscreen mode Exit fullscreen mode

This step uses the actions/checkout action to checkout the repository. The fetch-depth: 0 ensures the entire history is fetched, which is necessary for some Git operations.

4. Set up Git

- name: Set up Git
  run: |
    git config --global user.name 'github-actions'
    git config --global user.email 'github-actions@github.com'
Enter fullscreen mode Exit fullscreen mode

This step configures Git with a username and email. These settings are necessary for making commits or pushing changes.

5. Add Remote Repository with Credentials

- name: Add remote repository with credentials
  env:
    SECONDARY_REPO_USERNAME: ${{ secrets.SECONDARY_REPO_USERNAME }}
    SECONDARY_REPO_PASSWORD: ${{ secrets.SECONDARY_REPO_PASSWORD }}
  run: |
    git remote add secondary "http://${{ secrets.SECONDARY_REPO_USERNAME }}:${{ secrets.SECONDARY_REPO_PASSWORD }}@example.com/path/to/your/repo.git"
Enter fullscreen mode Exit fullscreen mode

This step adds a remote repository named secondary. The credentials for accessing this repository are stored securely in GitHub Secrets and accessed via environment variables.

6. Push Changes

- name: Push changes
  run: |
    git push secondary 1.0
Enter fullscreen mode Exit fullscreen mode

The final step pushes the changes to the 1.0 branch of the secondary remote repository.

Conclusion

By leveraging GitHub Actions, you can automate the process of updating code on a secondary Git platform, ensuring your repositories are always in sync across different environments. This workflow is highly customizable and can be adapted to fit various deployment or synchronization scenarios.

Implement this GitHub Action in your repository, replace the placeholders with your specific details, and enjoy a seamless automation experience!

Top comments (0)