Initial commit

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2023-03-22 18:02:55 +02:00
commit 992214329c
3 changed files with 77 additions and 0 deletions

15
Dockerfile Normal file
View File

@@ -0,0 +1,15 @@
FROM alpine
RUN apk add --no-cache git
RUN mkdir /git
COPY sync.sh .
RUN chmod +x sync.sh
RUN adduser -D -u 1000 -s /bin/sh app
RUN chown -R app:app /git
USER app
WORKDIR /git
CMD ["/sync.sh"]

21
README.md Normal file
View File

@@ -0,0 +1,21 @@
# git-dual-sync
A simple docker image that syncs one git remote to another.
## Environment Variables
| Variable | Description |
| -------------- | ---------------------------------- |
| `GIT_ORIGIN` | The origin remote to sync to |
| `GIT_UPSTREAM` | The upstream remote to sync from |
| `GIT_BRANCH` | The branch to sync |
| `GIT_NAME` | The name to use for the commit |
| `GIT_EMAIL` | The email to use for the commit |
| `GIT_USER` | The username to use for the commit |
| `GIT_PASS` | The password to use for the commit |
## Volumes
| Volume | Description |
| ------ | -------------------------- |
| `/git` | The git repository to sync |

41
sync.sh Executable file
View File

@@ -0,0 +1,41 @@
#!/bin/bash
if [ -z "$GIT_ORIGIN" ] || [ -z "$GIT_UPSTREAM" ] || [ -z "$GIT_BRANCH" ] || [ -z "$GIT_NAME" ] || [ -z "$GIT_EMAIL" ] || [ -z "$GIT_USER" ] || [ -z "$GIT_PASS" ]; then
echo "Environment variables not set"
exit 1
fi
git config --global user.name "$GIT_NAME"
git config --global user.email "$GIT_EMAIL"
PLAIN_URL=$(echo "$GIT_ORIGIN" | sed 's/https:\/\///')
AUTH_URL="https://$GIT_USER:$GIT_PASS@$PLAIN_URL"
WORKDIR="./git"
mkdir -p "$WORKDIR"
if [ ! -d "$WORKDIR/.git" ]; then
echo "Cloning repo"
git clone "$AUTH_URL" "$WORKDIR"
fi
cd "$WORKDIR"
if [ -z "$(git remote | grep origin)" ]; then
git remote add origin "$GIT_ORIGIN"
elif [ "$(git config --get remote.origin.url)" != "$GIT_ORIGIN" ]; then
git remote set-url origin "$GIT_ORIGIN"
fi
if [ -z "$(git remote | grep upstream)" ]; then
git remote add upstream "$GIT_UPSTREAM"
elif [ "$(git config --get remote.upstream.url)" != "$GIT_UPSTREAM" ]; then
git remote set-url upstream "$GIT_UPSTREAM"
fi
git fetch origin
git fetch upstream
git pull origin "$GIT_BRANCH"
git checkout "$GIT_BRANCH"
git merge upstream/"$GIT_BRANCH" --no-edit
git push $AUTH_URL "$GIT_BRANCH"