Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
cpylua committed Mar 16, 2021
0 parents commit f4e89a1
Show file tree
Hide file tree
Showing 8 changed files with 796 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2021-03-16

### Added
* Initial release.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM alpine

RUN apk add --no-cache git openssh-client && \
echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config

ADD *.sh /

ENTRYPOINT ["/entrypoint.sh"]
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Sync Git Branch Action

A GitHub Action for synchronizing a git branch to another location via SSH.

## Inputs

| Name | Description | Default | Required |
| -------------------- | ----------------------------------------- | ----------------------------------- | -------- |
| `source-branch` | Branch name in the source repository | | Yes |
| `destination-repo` | SSH URL of the destination repository | | Yes |
| `source-repo` | SSH URL of the source repository | The respository this action runs in | No |
| `destination-branch` | Branch name in the destination repository | Same as `source-branch` | No |

## Environment variables

`SSH_PRIVATE_KEY`: Create a [SSH key](https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key) **without** a passphrase which has access to both repositories.

On GitHub you should add the public key to repository "deploy keys".

Store [the private key as a secret](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) and use it in your workflow as seen in the example usage below.

## Example workflow

```yml
name: Sync main branch to Bitbucket

on: [push, delete, create]

jobs:
git-mirror:
runs-on: ubuntu-latest
steps:
- uses: zent-contrib/git-branch-sync-action@v1
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
with:
source-branch: main
destination-repo: "git@bitbucket.org:<org>/<repo>.git"
```
30 changes: 30 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Sync Git Branch Action"
description: "Action for synchronizing a git branch to another location (Bitbucket, GitHub, GitLab, etc.) using SSH."
branding:
icon: "copy"
color: "#155bd4"
inputs:
source-repo:
description: "SSH URL of the source repository. Defaults to the repository this actions runs in."
required: false
default: 'git@github.com:${{ github.repository }}.git'
source-branch:
description: "Branch name in source repository."
required: true
default: ""
destination-repo:
description: "SSH URL of the destination repository."
required: true
default: ""
destination-branch:
description: "Branch name in destination repository. Defaults to source-branch if not present."
required: false
default: ""
runs:
using: "docker"
image: "Dockerfile"
args:
- ${{ inputs.source-repo }}
- ${{ inputs.source-branch }}
- ${{ inputs.destination-repo }}
- ${{ inputs.destination-branch }}
15 changes: 15 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -e

if [[ -n "$SSH_PRIVATE_KEY" ]]
then
mkdir -p /root/.ssh
echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
chmod 600 /root/.ssh/id_rsa
fi

mkdir -p ~/.ssh
cp /root/.ssh/* ~/.ssh/ 2> /dev/null || true

sh -c "/sync-git-branch.sh $*"
18 changes: 18 additions & 0 deletions sync-git-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

set -e

SOURCE_REPO="$1"
SOURCE_BRANCH="$2"
DESTINATION_REPO="$3"
DESTINATION_BRANCH="${4:-$2}"
REPO_DIR="repo"

echo "SOURCE=$SOURCE_REPO:$SOURCE_BRANCH"
echo "DESTINATION=$DESTINATION_REPO:$DESTINATION_BRANCH"

# Only clone the branch we want to sync
git clone --single-branch --branch "$SOURCE_BRANCH" "$SOURCE_REPO" "$REPO_DIR"
cd "$REPO_DIR"
git remote add mirror "$DESTINATION_REPO"
git push mirror "$SOURCE_BRANCH:$DESTINATION_BRANCH"

0 comments on commit f4e89a1

Please sign in to comment.