From 82c7282c01fe70ac77e8952afd09edcf66172641 Mon Sep 17 00:00:00 2001 From: Grzegorz Poreba Date: Mon, 13 Dec 2021 12:15:25 +0100 Subject: [PATCH] Added creation of target branch if it doesn't already exist on target repo --- README.md | 2 ++ entrypoint.sh | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cf03e5f..1963ddd 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/entrypoint.sh b/entrypoint.sh index 0665382..ec930d5 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 .