Support force pushing

This commit is contained in:
Michael Panchenko 2021-12-05 19:48:43 +01:00
parent 6e36e20d0e
commit 5fecbad518
3 changed files with 21 additions and 3 deletions

View File

@ -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`. 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) ### `API_TOKEN_GITHUB` (environment)
E.g.: E.g.:
`API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}` `API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}`

View File

@ -48,6 +48,10 @@ inputs:
description: '[Optional] The directory to wipe and replace in the target repository' description: '[Optional] The directory to wipe and replace in the target repository'
default: '' default: ''
required: false required: false
force:
description: "[Optional] If 'true', will force push and thus overwrite the target repo's history"
default: 'false'
required: false
runs: runs:
using: docker using: docker
@ -64,6 +68,7 @@ runs:
- '${{ inputs.target-branch }}' - '${{ inputs.target-branch }}'
- '${{ inputs.commit-message }}' - '${{ inputs.commit-message }}'
- '${{ inputs.target-directory }}' - '${{ inputs.target-directory }}'
- '${{ inputs.force }}'
branding: branding:
icon: git-commit icon: git-commit
color: green color: green

View File

@ -15,6 +15,7 @@ DESTINATION_REPOSITORY_USERNAME="$8"
TARGET_BRANCH="$9" TARGET_BRANCH="$9"
COMMIT_MESSAGE="${10}" COMMIT_MESSAGE="${10}"
TARGET_DIRECTORY="${11}" TARGET_DIRECTORY="${11}"
FORCE="${12}"
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
then 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 : 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" git diff-index --quiet HEAD || git commit --message "$COMMIT_MESSAGE"
if $FORCE; then
echo "[+] Force pushing git commit"
FORCE_FLAG="-f"
else
echo "[+] Pushing git commit" echo "[+] Pushing git commit"
FORCE_FLAG=""
fi
# --set-upstream: sets de branch when pushing to a branch that does not exist # --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"