From 992214329cc17706e8aa7c1a5a38839ef3e2c529 Mon Sep 17 00:00:00 2001 From: Nikolaos Karaolidis Date: Wed, 22 Mar 2023 18:02:55 +0200 Subject: [PATCH] Initial commit Signed-off-by: Nikolaos Karaolidis --- Dockerfile | 15 +++++++++++++++ README.md | 21 +++++++++++++++++++++ sync.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 sync.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..42499be --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..b6bf6df --- /dev/null +++ b/README.md @@ -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 | diff --git a/sync.sh b/sync.sh new file mode 100755 index 0000000..7b82ebe --- /dev/null +++ b/sync.sh @@ -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"