Added creation of target branch if it doesn't already exist on target repo

This commit is contained in:
Grzegorz Poreba 2021-12-13 12:15:25 +01:00
parent e7314057a0
commit 82c7282c01
2 changed files with 16 additions and 1 deletions

View File

@ -2,6 +2,8 @@
When to use this GitHub Action? It is useful in case that you have a GitHub repository with a a directory that you want to push to another GitHub repository using GitHub Actions (automated on push, for example). It is also useful if using GitHub Actions you generate certain files that you want to push to another GitHub repository.
This fork creates `target-branch` if it doesn't exist on repository to which you push.
Flow:
The [example repository](https://github.com/cpina/push-to-another-repository-example) has a MarkDown file [main.md](https://github.com/cpina/push-to-another-repository-example/blob/main/main.md)), during the [GitHub Actions flow](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19) it executes [build.sh](https://github.com/cpina/push-to-another-repository-example/blob/main/build.sh) and the output/ directory (configurable via [source-directory](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L27) appears in the [output repository](https://github.com/cpina/push-to-another-repository-output).

View File

@ -26,13 +26,21 @@ then
USER_NAME="$DESTINATION_GITHUB_USERNAME"
fi
TARGET_BRANCH_EXISTS=true
CLONE_DIR=$(mktemp -d)
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
# Setup git
git config --global user.email "$USER_EMAIL"
git config --global user.name "$USER_NAME"
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
{
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
} || {
echo "Target branch doesn't exist, fetching main branch"
git clone --single-branch "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
TARGET_BRANCH_EXISTS=false
}
ls -la "$CLONE_DIR"
TEMP_DIR=$(mktemp -d)
@ -87,6 +95,11 @@ ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
if [ "$TARGET_BRANCH_EXISTS" = false ] ; then
echo "Creating branch $TARGET_BRANCH"
git checkout -b "$TARGET_BRANCH"
fi
echo "[+] Adding git commit"
git add .