From 33585ea95a845ddde81c6ef4d61dceff5ee5d0a9 Mon Sep 17 00:00:00 2001 From: Damien Cornu Date: Tue, 25 Apr 2023 14:31:00 +0200 Subject: [PATCH] Create branch if needed option --- action.yml | 9 ++++++++- entrypoint.sh | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 815415f..30d8e28 100644 --- a/action.yml +++ b/action.yml @@ -48,7 +48,13 @@ inputs: description: '[Optional] The directory to wipe and replace in the target repository' default: '' required: false - + create-target-branch-if-needed: + type: boolean + description: >- + [Optional] create target branch if not exist. Defaults to `false` + default: false + required: false + runs: using: docker image: Dockerfile @@ -64,6 +70,7 @@ runs: - '${{ inputs.target-branch }}' - '${{ inputs.commit-message }}' - '${{ inputs.target-directory }}' + - '${{ inputs.create-target-branch-if-needed }}' branding: icon: git-commit color: green diff --git a/entrypoint.sh b/entrypoint.sh index 80d003e..bacb784 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,6 +15,7 @@ DESTINATION_REPOSITORY_USERNAME="${8}" TARGET_BRANCH="${9}" COMMIT_MESSAGE="${10}" TARGET_DIRECTORY="${11}" +CREATE_TARGET_BRANCH_IF_NEEDED="${12}" if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] then @@ -70,6 +71,12 @@ git config --global user.name "$USER_NAME" { git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR" +} || { + if [ "$CREATE_TARGET_BRANCH_IF_NEEDED" = 'true' ] ; then + git clone --single-branch --depth 1 "$GIT_CMD_REPOSITORY" "$CLONE_DIR" + else + false + fi } || { echo "::error::Could not clone the destination repository. Command:" echo "::error::git clone --single-branch --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR" @@ -137,6 +144,11 @@ echo "[+] Set directory is safe ($CLONE_DIR)" # TODO: review before releasing it as a version git config --global --add safe.directory "$CLONE_DIR" + +if [ "$CREATE_TARGET_BRANCH_IF_NEEDED" = 'true' ] ; then + git checkout -b "$TARGET_BRANCH" +fi + echo "[+] Adding git commit" git add .