Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wei committed Jul 2, 2019
0 parents commit f90c2d1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM alpine

LABEL "com.github.actions.name"="Git Sync Action"
LABEL "com.github.actions.description"="🔃 Sync between two independent repositories"
LABEL "com.github.actions.icon"="git-branch"
LABEL "com.github.actions.color"="black"

LABEL "repository"="http://github.com/wei/git-sync"
LABEL "homepage"="http://github.com/wei/git-sync"
LABEL "maintainer"="Wei He <[email protected]>"

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

ADD *.sh /

ENTRYPOINT ["/entrypoint.sh"]
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Git Sync

A GitHub Action for syncing between two independent repositories using **force push**.


## Features
* Sync branches between two Github repositories
* Sync branches to/from a remote repository
* Github action can be triggered on a timer or on push


## Usage

### Github Actions
```
action "repo-sync" {
uses = "wei/git-sync@master"
args = "$SOURCE_REPO $SOURCE_BRANCH $DESTINATION_REPO $DESTINATION_BRANCH"
env = {
SOURCE_REPO = ""
SOURCE_BRANCH = ""
DESTINATION_REPO = ""
DESTINATION_BRANCH = ""
}
secrets = ["SSH_PRIVATE_KEY"]
}
```
`SSH_PRIVATE_KEY` can be omitted if using authenticated HTTPS repo clone urls like `https://username:[email protected]/username/repository.git`.

### Docker
```
docker run --rm -e "SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)" $(docker build -q .) \
$SOURCE_REPO $SOURCE_BRANCH $DESTINATION_REPO $DESTINATION_BRANCH
```

## Author
[Wei He](https://github.com/wei) _[email protected]_


## License
[MIT](https://wei.mit-license.org)
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 "/git-sync.sh $*"
39 changes: 39 additions & 0 deletions git-sync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

set -e

SOURCE_REPO=$1
SOURCE_BRANCH=$2
DESTINATION_REPO=$3
DESTINATION_BRANCH=$4

if ! echo $SOURCE_REPO | grep '.git'
then
if [[ -n "$SSH_PRIVATE_KEY" ]]
then
SOURCE_REPO="[email protected]:${SOURCE_REPO}.git"
GIT_SSH_COMMAND="ssh -v"
else
SOURCE_REPO="https://github.com/${SOURCE_REPO}.git"
fi
fi
if ! echo $DESTINATION_REPO | grep '.git'
then
if [[ -n "$SSH_PRIVATE_KEY" ]]
then
DESTINATION_REPO="[email protected]:${DESTINATION_REPO}.git"
GIT_SSH_COMMAND="ssh -v"
else
DESTINATION_REPO="https://github.com/${DESTINATION_REPO}.git"
fi
fi

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

git clone "$SOURCE_REPO" --origin source && cd `basename "$SOURCE_REPO" .git`
git checkout "$SOURCE_BRANCH"
git show --name-status
git remote add destination "$DESTINATION_REPO"
git remote -v
git push destination "${SOURCE_BRANCH}:${DESTINATION_BRANCH}" -f

0 comments on commit f90c2d1

Please sign in to comment.