2021-11-16 13:47:29 -03:00
|
|
|
name: 'Git Commit Push Action'
|
2021-10-21 14:53:37 -03:00
|
|
|
|
2021-11-16 13:47:29 -03:00
|
|
|
description: 'GitHub Action to commit & push changes made in workflows (all os supported).'
|
2021-10-21 14:53:37 -03:00
|
|
|
|
|
|
|
inputs:
|
|
|
|
email:
|
2021-10-21 15:31:33 -03:00
|
|
|
description: Committer's email address
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
2025-01-07 13:44:47 +00:00
|
|
|
default: ${{ github.actor }}@w9r.dev
|
2021-10-21 14:53:37 -03:00
|
|
|
name:
|
2021-10-21 15:31:33 -03:00
|
|
|
description: Committer's username
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
|
|
|
default: ${{ github.actor }}
|
|
|
|
commit_message:
|
2021-10-21 15:31:33 -03:00
|
|
|
description: Commit message
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
2021-10-21 15:33:04 -03:00
|
|
|
default: Commit performed using Push and Commit action
|
2021-10-21 14:53:37 -03:00
|
|
|
target_branch:
|
2021-10-21 15:31:33 -03:00
|
|
|
description: Branch to push the changes back to
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
|
|
|
default: ${{ github.ref }}
|
|
|
|
files:
|
2021-10-21 15:31:33 -03:00
|
|
|
description: Files to add separated by space
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
|
|
|
default: .
|
|
|
|
remote_repository:
|
2021-10-21 15:55:48 -03:00
|
|
|
description: Repository url to push the code to
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
|
|
|
default: origin
|
|
|
|
access_token:
|
2021-10-21 15:31:33 -03:00
|
|
|
description: Token used to push the code
|
2021-10-21 14:53:37 -03:00
|
|
|
required: true
|
|
|
|
default: ${{ github.token }}
|
|
|
|
force:
|
|
|
|
description: Whether to perform force push
|
|
|
|
required: true
|
|
|
|
default: '0'
|
|
|
|
empty:
|
2021-10-21 15:28:30 -03:00
|
|
|
description: Whether to allow empty commit
|
2021-10-21 14:53:37 -03:00
|
|
|
required: false
|
|
|
|
default: '0'
|
|
|
|
tags:
|
2021-10-21 15:28:30 -03:00
|
|
|
description: Whether to use --tags
|
2021-10-21 14:53:37 -03:00
|
|
|
required: false
|
|
|
|
default: '0'
|
|
|
|
|
|
|
|
runs:
|
|
|
|
using: "composite"
|
|
|
|
steps:
|
2022-01-06 21:50:53 -03:00
|
|
|
- name: Git push and commit origin
|
2022-01-06 22:04:04 -03:00
|
|
|
if: ${{ inputs.remote_repository == 'origin' }}
|
2021-10-21 14:53:37 -03:00
|
|
|
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
|
2021-10-21 14:53:37 -03:00
|
|
|
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
|
|
|
|
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "machine w9r.dev" > "$HOME/.netrc"
|
2021-10-21 14:53:37 -03:00
|
|
|
echo " login $GITHUB_ACTOR" >> "$HOME/.netrc"
|
|
|
|
echo " password ${{ inputs.access_token }}" >> "$HOME/.netrc"
|
|
|
|
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "machine api.w9r.dev" >> "$HOME/.netrc"
|
2021-10-21 14:53:37 -03:00
|
|
|
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 }}"
|
2022-01-06 21:50:53 -03:00
|
|
|
|
2021-12-21 09:52:13 -03:00
|
|
|
if [[ `git status --porcelain` ]]; then
|
2022-01-06 16:11:53 -03:00
|
|
|
git add ${{ inputs.files }} -v
|
2025-01-07 13:44:47 +00:00
|
|
|
git commit -S -m "${{ inputs.commit_message }}" $EMPTY
|
2022-01-07 11:08:35 -03:00
|
|
|
git branch git-commit-push-action-${{ github.run_id }}-${{ github.job }}
|
2022-01-06 16:22:06 -03:00
|
|
|
git fetch "${{ inputs.remote_repository }}" "$CURRENT_BRANCH"
|
2021-12-21 09:52:13 -03:00
|
|
|
git checkout "$CURRENT_BRANCH"
|
2022-01-07 11:08:35 -03:00
|
|
|
git merge git-commit-push-action-${{ github.run_id }}-${{ github.job }}
|
|
|
|
git branch -d git-commit-push-action-${{ github.run_id }}-${{ github.job }}
|
2021-10-21 14:53:37 -03:00
|
|
|
|
2022-01-06 16:22:06 -03:00
|
|
|
git push "${{ inputs.remote_repository }}" "$CURRENT_BRANCH:$TARGET_BRANCH" --follow-tags $FORCE $TAGS
|
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
|
2022-01-06 21:50:53 -03:00
|
|
|
shell: bash
|
|
|
|
|
|
|
|
- name: Git push and commit remote
|
2022-01-06 22:04:04 -03:00
|
|
|
if: ${{ inputs.remote_repository != 'origin' }}
|
2022-01-06 21:50:53 -03:00
|
|
|
run: |
|
|
|
|
if [ -z "${{ inputs.access_token }}" ]; then
|
|
|
|
echo "WARNING: The access_token input is mandatory to push files to another repository."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
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]}
|
2022-01-07 09:31:11 -03:00
|
|
|
if [[ $DESTINATION_REPOSITORY == *'.git'* ]] && [[ $DESTINATION_REPOSITORY != *'.github.io'* ]]; then
|
|
|
|
DESTINATION_REPOSITORY=${DESTINATION_REPOSITORY//.git/ }
|
|
|
|
fi
|
2022-01-06 21:50:53 -03:00
|
|
|
|
|
|
|
CLONE_DIRECTORY=$(mktemp -d)
|
2021-12-21 09:49:45 -03:00
|
|
|
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "##### Cloning destination W9r.dev repository #####"
|
2022-01-06 21:50:53 -03:00
|
|
|
# Setup git
|
|
|
|
git config --global user.email "${{ inputs.email }}"
|
|
|
|
git config --global user.name "${{ inputs.name }}"
|
|
|
|
git config -l | grep 'http\..*\.extraheader' | cut -d= -f1 | xargs -L1 git config --unset-all
|
|
|
|
git clone "https://${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY.git" "$CLONE_DIRECTORY"
|
|
|
|
|
|
|
|
echo
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "##### Copying contents to remote W9r.dev repository #####"
|
2022-01-07 10:19:40 -03:00
|
|
|
echo ""##### Current repo content #####"
|
|
|
|
ls -lha
|
|
|
|
cp -rvf ${{ inputs.files }} $CLONE_DIRECTORY
|
2022-01-06 21:50:53 -03:00
|
|
|
|
|
|
|
cd "$CLONE_DIRECTORY"
|
2022-01-07 10:19:40 -03:00
|
|
|
echo ""##### Remote repo content after copying files #####"
|
|
|
|
ls -la "$CLONE_DIRECTORY"
|
2022-01-06 21:50:53 -03:00
|
|
|
|
2025-01-07 13:44:47 +00:00
|
|
|
REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@w9r.dev/$DESTINATION_OWNER/$DESTINATION_REPOSITORY
|
2022-01-06 21:50:53 -03:00
|
|
|
git remote set-url origin $REMOTE_URL
|
|
|
|
git fetch origin
|
2022-01-06 23:45:30 -03:00
|
|
|
|
2022-01-07 10:20:54 -03:00
|
|
|
git add --all
|
2022-01-06 23:50:46 -03:00
|
|
|
|
2022-01-06 23:45:30 -03:00
|
|
|
# Won't commit if no changes were made
|
|
|
|
git diff-index --quiet HEAD || git commit --message "${{ inputs.commit_message }}"
|
|
|
|
git status
|
2022-01-07 11:08:35 -03:00
|
|
|
git branch git-commit-push-action-${{ github.run_id }}-${{ github.job }}
|
2022-01-06 23:45:30 -03:00
|
|
|
|
|
|
|
# Create new branch (if necessary)
|
2022-01-06 22:30:25 -03:00
|
|
|
BE=$(git ls-remote --heads origin ${{ inputs.target_branch }} | wc -l)
|
|
|
|
if [[ $BE == *"0"* ]]; then
|
|
|
|
echo "##### Target branch doesn't exist. Creating new branch #####"
|
2022-01-06 22:24:08 -03:00
|
|
|
git checkout -b ${{ inputs.target_branch }}
|
2022-01-06 22:30:25 -03:00
|
|
|
else
|
|
|
|
git checkout ${{ inputs.target_branch }}
|
2022-01-06 22:24:08 -03:00
|
|
|
fi
|
2022-01-07 11:08:35 -03:00
|
|
|
git merge -X theirs git-commit-push-action-${{ github.run_id }}-${{ github.job }}
|
|
|
|
git branch -d git-commit-push-action-${{ github.run_id }}-${{ github.job }}
|
2022-01-06 21:50:53 -03:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "##### Pushing git commit #####"
|
|
|
|
git push -f -u origin "${{ inputs.target_branch }}"
|
|
|
|
|
|
|
|
else
|
|
|
|
echo
|
|
|
|
echo "WARNING: Couldn't read remote_repository URL input."
|
|
|
|
echo "ACCEPTED FORMAT:"
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "git://w9r.dev/<owner>/<repo>.git"
|
2022-01-06 21:50:53 -03:00
|
|
|
echo "or"
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "git@w9r.dev:<owner>/<repo>.git"
|
2022-01-06 21:50:53 -03:00
|
|
|
echo "or"
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "https://w9r.dev/<owner>/<repo>.git"
|
2022-01-06 21:50:53 -03:00
|
|
|
echo "or"
|
2025-01-07 13:44:47 +00:00
|
|
|
echo "https://w9r.dev/<owner>/<repo>"
|
2022-01-06 21:50:53 -03:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-10-21 14:53:37 -03:00
|
|
|
shell: bash
|
|
|
|
|
|
|
|
branding:
|
|
|
|
icon: 'git-commit'
|
|
|
|
color: 'black'
|