feat: initial commit

Signed-off-by: Stephan Linz <linz@li-pro.net>
This commit is contained in:
Stephan Linz 2023-03-02 18:30:02 +01:00
commit fcc2850caf
No known key found for this signature in database
GPG Key ID: 579B34AFDE6AB439
5 changed files with 178 additions and 0 deletions

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM alpine
LABEL "repository"="http://github.com/tiacsys/git-rebase"
LABEL "homepage"="http://github.com/tiacsys/git-rebase"
LABEL "maintainer"="TiaC Systems <info@tiac-systems.net>"
RUN apk add --no-cache git openssh-client && \
echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config
ADD *.sh /
ENTRYPOINT ["/entrypoint.sh"]

91
README.md Normal file
View File

@ -0,0 +1,91 @@
# Git Rebase
A GitHub Action for branch rebasing in independent repository using **force push**.
## Features
- Rebase branches on a GitHub repository
- Rebase branches on a remote repository
- GitHub action can be triggered on a timer or on push or manually
## Usage
> Always make a full backup of your repo (`git clone --mirror`) before using this action.
### GitHub Actions
```yml
# .github/workflows/git-rebase.yml
on: push
jobs:
git-rebase:
runs-on: ubuntu-latest
steps:
- name: git-rebase
uses: tiacsys/git-rebase@v3
with:
repo: "org/repository"
source_branch: "next"
destination_branch: "main"
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} # optional
```
##### Using shorthand
You can use GitHub repo shorthand like `username/repository`.
##### Using ssh
> The `ssh_private_key` must be supplied if using ssh clone urls.
```yml
repo: "git@github.com:username/repository.git"
```
or
```yml
repo: "git@gitlab.com:username/repository.git"
```
##### Using https
> The `ssh_private_key` can be omitted if using authenticated https urls.
```yml
repo: "https://username:personal_access_token@github.com/username/repository.git"
```
#### Set up deploy keys
> You only need to set up deploy keys if repository is private and ssh clone url is used.
- Generate ssh key for the repository, leave passphrase empty (note that GitHub deploy keys must be unique for a repository)
```sh
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```
- In GitHub, either:
- add the unique public keys (`key_name.pub`) to _Repo Settings > Deploy keys_ for each repository respectively and allow write access for the destination repository
or
- add the single public key (`key_name.pub`) to _Personal Settings > SSH keys_
- Add the private key(s) to _Repo > Settings > Secrets_ for the repository containing the action (`SSH_PRIVATE_KEY`)
### Docker
```sh
$ docker run --rm -e "SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)" $(docker build -q .) \
$SOURCE_REPO $SOURCE_BRANCH $DESTINATION_REPO $DESTINATION_BRANCH
```
## Author
[TiaC Systems](https://tiac-systems.net/) _info@tiac-systems.net_
## License
[MIT](LICENSE)

28
action.yml Normal file
View File

@ -0,0 +1,28 @@
name: Git Rebase Action
author: TiaC Systems <info@tiac-systems.net>
description: 🔝 Rebase branch on independent repository
branding:
icon: 'git-pull-request'
color: 'gray-dark'
inputs:
repo:
description: GitHub repo slug or full url
required: true
source_branch:
description: Branch name to rebase from
required: true
destination_branch:
description: Branch name to rebase to
required: true
ssh_private_key:
description: SSH key used to authenticate with ssh urls provided (optional if public or https url with authentication)
required: false
runs:
using: 'docker'
image: 'Dockerfile'
env:
SSH_PRIVATE_KEY: ${{ inputs.ssh_private_key }}
args:
- ${{ inputs.repo }}
- ${{ inputs.source_branch }}
- ${{ inputs.destination_branch }}

14
entrypoint.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
set -e
if [[ -n "$SSH_PRIVATE_KEY" ]]; then
mkdir -p /root/.ssh
echo "$SSH_PRIVATE_KEY" | sed 's/\\n/\n/g' >/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-rebase.sh $*"

33
git-rebase.sh Executable file
View File

@ -0,0 +1,33 @@
#!/bin/sh
set -e
REPO=$1
SOURCE_BRANCH=$2
DESTINATION_BRANCH=$3
if ! echo ${REPO} | grep -Eq ':|@|\.git\/?$'; then
if [[ -n "${SSH_PRIVATE_KEY}" || -n "${SOURCE_SSH_PRIVATE_KEY}" ]]; then
REPO="git@github.com:${REPO}.git"
GIT_SSH_COMMAND="ssh -v"
else
REPO="https://github.com/${REPO}.git"
fi
fi
echo "SOURCE=${REPO}:${SOURCE_BRANCH}"
echo "DESTINATION=${REPO}:${DESTINATION_BRANCH}"
git clone "${REPO}" /root/repo --origin remote-repo && cd /root/repo
# Pull all branches references down locally so subsequent commands can see them
git fetch remote-repo '+refs/heads/*:refs/heads/*' --update-head-ok
# Print out all branches
git --no-pager branch -a -vv
git config --local user.name "$(git log -n 1 --pretty=format:%cn ${SOURCE_BRANCH})"
git config --local user.email "$(git log -n 1 --pretty=format:%ce ${SOURCE_BRANCH})"
git rebase --onto "${DESTINATION_BRANCH}" "${DESTINATION_BRANCH}" "${SOURCE_BRANCH}"
git push remote-repo HEAD:"${SOURCE_BRANCH}" -f