From 5fecbad518f01d5909109fa086043c7a3cb75821 Mon Sep 17 00:00:00 2001 From: Michael Panchenko Date: Sun, 5 Dec 2021 19:48:43 +0100 Subject: [PATCH] Support force pushing --- README.md | 4 ++++ action.yml | 7 ++++++- entrypoint.sh | 13 +++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cf03e5f..3890184 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,10 @@ The directory to wipe and replace in the target repository. Defaults to wiping The string `ORIGIN_COMMIT` is replaced by `$REPOSITORY_URL@commit`. +### `force` (argument) [optional] +If "true", will force push to the target repo. Use with caution + + ### `API_TOKEN_GITHUB` (environment) E.g.: `API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}` diff --git a/action.yml b/action.yml index 5237bb6..4f66ed9 100644 --- a/action.yml +++ b/action.yml @@ -48,7 +48,11 @@ inputs: description: '[Optional] The directory to wipe and replace in the target repository' default: '' required: false - + force: + description: "[Optional] If 'true', will force push and thus overwrite the target repo's history" + default: 'false' + required: false + runs: using: docker image: Dockerfile @@ -64,6 +68,7 @@ runs: - '${{ inputs.target-branch }}' - '${{ inputs.commit-message }}' - '${{ inputs.target-directory }}' + - '${{ inputs.force }}' branding: icon: git-commit color: green diff --git a/entrypoint.sh b/entrypoint.sh index 9d0b836..0fdbcbd 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,6 +15,7 @@ DESTINATION_REPOSITORY_USERNAME="$8" TARGET_BRANCH="$9" COMMIT_MESSAGE="${10}" TARGET_DIRECTORY="${11}" +FORCE="${12}" if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] then @@ -102,6 +103,14 @@ echo "[+] git diff-index:" # git diff-index : to avoid doing the git commit failing if there are no changes to be commit git diff-index --quiet HEAD || git commit --message "$COMMIT_MESSAGE" -echo "[+] Pushing git commit" +if $FORCE; then + echo "[+] Force pushing git commit" + FORCE_FLAG="-f" +else + echo "[+] Pushing git commit" + FORCE_FLAG="" +fi # --set-upstream: sets de branch when pushing to a branch that does not exist -git push "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH" +git push "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" \ + --set-upstream "$TARGET_BRANCH" \ + "$FORCE_FLAG"