Skip to content

Commit f4e89a1

Browse files
committed
initial release
0 parents  commit f4e89a1

File tree

8 files changed

+796
-0
lines changed

8 files changed

+796
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [1.0.0] - 2021-03-16
9+
10+
### Added
11+
* Initial release.

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM alpine
2+
3+
RUN apk add --no-cache git openssh-client && \
4+
echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config
5+
6+
ADD *.sh /
7+
8+
ENTRYPOINT ["/entrypoint.sh"]

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Sync Git Branch Action
2+
3+
A GitHub Action for synchronizing a git branch to another location via SSH.
4+
5+
## Inputs
6+
7+
| Name | Description | Default | Required |
8+
| -------------------- | ----------------------------------------- | ----------------------------------- | -------- |
9+
| `source-branch` | Branch name in the source repository | | Yes |
10+
| `destination-repo` | SSH URL of the destination repository | | Yes |
11+
| `source-repo` | SSH URL of the source repository | The respository this action runs in | No |
12+
| `destination-branch` | Branch name in the destination repository | Same as `source-branch` | No |
13+
14+
## Environment variables
15+
16+
`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.
17+
18+
On GitHub you should add the public key to repository "deploy keys".
19+
20+
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.
21+
22+
## Example workflow
23+
24+
```yml
25+
name: Sync main branch to Bitbucket
26+
27+
on: [push, delete, create]
28+
29+
jobs:
30+
git-mirror:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: zent-contrib/git-branch-sync-action@v1
34+
env:
35+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
36+
with:
37+
source-branch: main
38+
destination-repo: "git@bitbucket.org:<org>/<repo>.git"
39+
```

action.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "Sync Git Branch Action"
2+
description: "Action for synchronizing a git branch to another location (Bitbucket, GitHub, GitLab, etc.) using SSH."
3+
branding:
4+
icon: "copy"
5+
color: "#155bd4"
6+
inputs:
7+
source-repo:
8+
description: "SSH URL of the source repository. Defaults to the repository this actions runs in."
9+
required: false
10+
default: 'git@github.com:${{ github.repository }}.git'
11+
source-branch:
12+
description: "Branch name in source repository."
13+
required: true
14+
default: ""
15+
destination-repo:
16+
description: "SSH URL of the destination repository."
17+
required: true
18+
default: ""
19+
destination-branch:
20+
description: "Branch name in destination repository. Defaults to source-branch if not present."
21+
required: false
22+
default: ""
23+
runs:
24+
using: "docker"
25+
image: "Dockerfile"
26+
args:
27+
- ${{ inputs.source-repo }}
28+
- ${{ inputs.source-branch }}
29+
- ${{ inputs.destination-repo }}
30+
- ${{ inputs.destination-branch }}

entrypoint.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
if [[ -n "$SSH_PRIVATE_KEY" ]]
6+
then
7+
mkdir -p /root/.ssh
8+
echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
9+
chmod 600 /root/.ssh/id_rsa
10+
fi
11+
12+
mkdir -p ~/.ssh
13+
cp /root/.ssh/* ~/.ssh/ 2> /dev/null || true
14+
15+
sh -c "/sync-git-branch.sh $*"

sync-git-branch.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
SOURCE_REPO="$1"
6+
SOURCE_BRANCH="$2"
7+
DESTINATION_REPO="$3"
8+
DESTINATION_BRANCH="${4:-$2}"
9+
REPO_DIR="repo"
10+
11+
echo "SOURCE=$SOURCE_REPO:$SOURCE_BRANCH"
12+
echo "DESTINATION=$DESTINATION_REPO:$DESTINATION_BRANCH"
13+
14+
# Only clone the branch we want to sync
15+
git clone --single-branch --branch "$SOURCE_BRANCH" "$SOURCE_REPO" "$REPO_DIR"
16+
cd "$REPO_DIR"
17+
git remote add mirror "$DESTINATION_REPO"
18+
git push mirror "$SOURCE_BRANCH:$DESTINATION_BRANCH"

0 commit comments

Comments
 (0)