action-git-commit-push/action.yml

118 lines
3.8 KiB
YAML
Raw Normal View History

2021-11-16 13:47:29 -03:00
name: 'Git Commit Push Action'
2021-11-16 13:47:29 -03:00
description: 'GitHub Action to commit & push changes made in workflows (all os supported).'
inputs:
email:
description: Committer's email address
required: true
default: ${{ github.actor }}@users.noreply.github.com
name:
description: Committer's username
required: true
default: ${{ github.actor }}
commit_message:
description: Commit message
required: true
default: Commit performed using Push and Commit action
target_branch:
description: Branch to push the changes back to
required: true
default: ${{ github.ref }}
files:
description: Files to add separated by space
required: true
default: .
remote_repository:
description: Repository url to push the code to
required: true
default: origin
access_token:
description: Token used to push the code
required: true
default: ${{ github.token }}
force:
description: Whether to perform force push
required: true
default: '0'
empty:
description: Whether to allow empty commit
required: false
default: '0'
tags:
description: Whether to use --tags
required: false
default: '0'
outputs:
diff-output:
description: "Assert output"
value: ${{ steps.assert.outputs.result }}
runs:
using: "composite"
steps:
- name: Git push and commit
run: |
2021-11-18 11:52:48 -03:00
CURRENT_BRANCH=${GITHUB_REF}
case $CURRENT_BRANCH in "refs/heads/"*)
CURRENT_BRANCH=$(echo "$CURRENT_BRANCH" | sed "s@refs/heads/@@")
esac
TARGET_BRANCH=${{ inputs.target_branch }}
case $TARGET_BRANCH in "refs/heads/"*)
TARGET_BRANCH=$(echo "$TARGET_BRANCH" | sed "s@refs/heads/@@")
esac
if [ "${{ inputs.force }}" != "0" ]; then
FORCE='--force'
fi
if [ "${{ inputs.empty }}" != "0" ]; then
EMPTY='--allow-empty'
fi
if [ "${{ inputs.tags }}" != "0" ]; then
TAGS='--tags'
fi
echo "machine github.com" > "$HOME/.netrc"
echo " login $GITHUB_ACTOR" >> "$HOME/.netrc"
echo " password ${{ inputs.access_token }}" >> "$HOME/.netrc"
echo "machine api.github.com" >> "$HOME/.netrc"
echo " login $GITHUB_ACTOR" >> "$HOME/.netrc"
echo " password ${{ inputs.access_token }}" >> "$HOME/.netrc"
git config --local user.email "${{ inputs.email }}"
git config --local user.name "${{ inputs.name }}"
2021-12-21 09:52:13 -03:00
if [[ `git status --porcelain` ]]; then
2022-01-06 16:10:04 -03:00
REGEX="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+)$"
if [[ ${{ inputs.remote_repository }} =~ $REGEX ]]; then
PROTOCOL=${BASH_REMATCH[1]}
SEPARATOR=${BASH_REMATCH[2]}
HOSTNAME=${BASH_REMATCH[3]}
DESTINATION_OWNER=${BASH_REMATCH[4]}
DESTINATION_REPOSITORY=${BASH_REMATCH[5]}
DESTINATION_REPOSITORY=${DESTINATION_REPOSITORY//.git/ }
REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY
fi
2022-01-06 16:11:53 -03:00
git add ${{ inputs.files }} -v
git commit -m "${{ inputs.commit_message }}" $EMPTY
2021-12-21 09:52:13 -03:00
git branch push-and-commit-action-${{ github.run_id }}-${{ github.job }}
2022-01-06 16:16:07 -03:00
git fetch "$REMOTE_URL" "$CURRENT_BRANCH"
2021-12-21 09:52:13 -03:00
git checkout "$CURRENT_BRANCH"
git merge push-and-commit-action-${{ github.run_id }}-${{ github.job }}
git branch -d push-and-commit-action-${{ github.run_id }}-${{ github.job }}
2022-01-06 16:10:04 -03:00
git push "$REMOTE_URL" "$CURRENT_BRANCH:$TARGET_BRANCH" --follow-tags $FORCE $TAGS
2021-12-21 09:52:13 -03:00
2021-12-21 09:49:45 -03:00
else
2021-12-21 09:58:49 -03:00
echo "WARNING: No changes were detected. git commit push action aborted."
2021-12-21 09:49:45 -03:00
fi
shell: bash
branding:
icon: 'git-commit'
color: 'black'