This commit is contained in:
OriolFilter 2022-01-16 17:29:42 +01:00
commit 22b5f7cdd7
6 changed files with 210 additions and 0 deletions

23
Dockerfile Executable file
View File

@ -0,0 +1,23 @@
FROM alpine:3.15.0
RUN apk add --no-cache openssh git bash
ADD ./credential_helper.sh /scripts/
ADD ./main.sh /scripts/
RUN chmod +x /scripts/main.sh /scripts/credential_helper.sh
WORKDIR /GIT_DIR
ENV __CREDENTIAL_HELPER_SCRIPT_PATH="/scripts/credential_helper.sh"
ENV GIT_REMOTE_BRANCH='master'
ENV GIT_PASSWORD=''
ENV GIT_USERNAME=''
ENV GIT_REPO_URL=''
ENV GIT_FORCE_PUSH=''
#ENV SKIP_SSL_CHECK=''
ENV GIT_COMMIT_MESSAGE='.'
ENV GIT_COMMIT_AUTHOR=''
ENV GIT_COMMIT_EMAIL=''
ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
CMD ["/scripts/main.sh"]

82
README.md Executable file
View File

@ -0,0 +1,82 @@
## Usage
1. Mount the folders/files you want to push into a repository **in** the **/GIT_DIR** directory.
2. Finally, set the environment variables required.
3. Optional, configure crontab to have periodical push in case you want to use it as a backup (ie. factorio server public world backups).
#### Notes
In case of wanting to push the content of a directory, just mount it **as** **/GIT_DIR**
### usage?
Use a **env** file with a simple env config
```text
GIT_REPO_URL=https://gitserver.url/user/repo_name.git
GIT_USERNAME=username
GIT_PASSWORD=TOKEN (or password...)
GIT_REMOTE_BRANCH=dev
GIT_FORCE_PUSH=yes
```
```shell
_time=`date "+%Y-%M-%d"` && \
docker run -v "$(pwd)/folder_to_push:/GIT_DIR" -e GIT_COMMIT_MESSAGE="Commit done at $_time" --env-file env.file -it testing/git_backup:latest
```
In this scenario we are storing the required information to push in a **env** file
## Git ignore
Is as simple to create a **.gitignore** file and place it in the folder that you want to back up.
#### In case of **.gitignore** being inside the folder to push
```shell
# .gitignore
ignored_file
```
```shell
docker run -v "$(pwd)/folder_to_push:/GIT_DIR" -e GIT_COMMIT_MESSAGE="Commit done at $_time" --env-file env.file -it testing/git_backup:latest
```
#### In case of having to push multiple directories/files
```shell
docker run -v "/path_to/file_to_push1:/GIT_DIR/file_to_push1" "/path_to/file_to_push2:/GIT_DIR/file_to_push2" "/path_to/gitignore:/GIT_DIR/.gitignore" -e GIT_COMMIT_MESSAGE="Commit done at $_time" --env-file env.file -it testing/git_backup:latest
```
## Environment
### Configurable
|ENV|DEFAULT value|DESCRIPTION|
|---|---|---|
|GIT_REPO_URL||Url from the repository|
|GIT_USERNAME||Username or email from the git server|
|GIT_PASSWORD||Token (or password, which is not recommended) from the git server|
|GIT_FORCE_PUSH||Used to force the push, to enable it add any content|
|GIT_REMOTE_BRANCH|master|Branch to push|
|GIT_COMMIT_MESSAGE|.|Message used during the commit|
|GIT_COMMIT_AUTHOR||On empty will use "Name"|
|GIT_COMMIT_EMAIL||On empty will commit as "<>"|
|---|---|---|
|SKIP_SSL_CHECK|---|disabled|
|GIT_TAG|---|disabled|
|GIT_TAG_MESSAGE|---|disabled|
|TAR_CONTENT|---|disabled|
**---** means that it's empty
### Not Configurable
##### This exists as a documentation
|ENV|DEFAULT|DESCRIPTION|
|---|---|---|
|__CREDENTIAL_HELPER_SCRIPT_PATH | /scripts/credential_helper.sh"| Points to the script used to automate git authentication (credential manager) |
## Docker compose
...

3
credential_helper.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
echo username="$GIT_USERNAME"
echo password="$GIT_PASSWORD"

10
docker-compose.yaml Normal file
View File

@ -0,0 +1,10 @@
services:
backup:
image: testing/git_backup
build:
context: ./
dockerfile: Dockerfile
volumes:
- ./tmpf:/GIT_DIR:rw
env_file:
- ./env.file

8
env.file Executable file
View File

@ -0,0 +1,8 @@
GIT_REPO_URL=git.server.url
GIT_USERNAME=username
GIT_PASSWORD=token_passw
GIT_REMOTE_BRANCH=dev
GIT_FORCE_PUSH=yes
GIT_COMMIT_AUTHOR=
GIT_COMMIT_EMAIL=

84
main.sh Executable file
View File

@ -0,0 +1,84 @@
#!/bin/bash
#!/usr/bin/env bash
# https://gist.github.com/mihow/9c7f559807069a03e302605691f85572
#########################
#### Script env
##
_FORCE_PUSH=""
env
#########################
#### Validate username and password exists
##
if [ -z "${GIT_USERNAME}" ];
then
echo "Please set up a username in the environment"
exit 127
fi
if [ -z "${GIT_PASSWORD}" ];
then
echo "Please set up a password in the environment"
exit 127
fi
if [ -z "${GIT_REPO_URL}" ];
then
echo "Please set up a url to push the environment in the environment"
exit 127
fi
#echo "${GIT_REPO_URL:-not found}"
# Remove .git
rm -rfv ./.git
## TMP
# Create file to keep updating repo
date > README.md
# Tar files in folder (Unless specified otherwise in the config)
#cp -rv /BACKUP_DIR /__BACKUP_DIR || exit 127
#cd /__BACKUP_DIR || exit 127
# Git init
git init
# Git add
git add .
#???
git config user.email "${GIT_COMMIT_EMAIL:-.}"
# Git commit
echo "email $GIT_COMMIT_EMAIL"
echo "author $GIT_COMMIT_AUTHOR"
git commit --author="${GIT_COMMIT_AUTHOR:-Name} ${GIT_COMMIT_EMAIL:-<>}" -m "${GIT_COMMIT_MESSAGE}"
# Git add remote tag
git remote add origin "${GIT_REPO_URL}"
## Git tag
#if [ -n "${GIT_TAG}" ]
# then
#
# git tag -a "${GIT_TAG}" -m "${GIT_TAG_MESSAGE:-$GIT_TAG_NAME}"
# _FORCE_PUSH='-f'
#fi
# Set up credential helper script to use environment variables
chmod +x "$__CREDENTIAL_HELPER_SCRIPT_PATH"
git config credential.helper "/bin/bash ${__CREDENTIAL_HELPER_SCRIPT_PATH}"
# Git push
if [ -n "${GIT_FORCE_PUSH}" ]
then
_FORCE_PUSH='-f'
fi
git push ${_FORCE_PUSH} origin "HEAD:${GIT_REMOTE_BRANCH}"
#bash