action-git-commit-push/action.yml
oliver e718ff01f8
All checks were successful
Action test on Ubuntu / ubuntu-minimal (push) Has been skipped
Action test on Ubuntu / ubuntu-nochanges (push) Successful in 4s
Action test on Ubuntu / ubuntu-full (push) Successful in 5s
Update action.yml
2025-01-07 13:44:47 +00:00

180 lines
6.5 KiB
YAML

name: 'Git Commit Push Action'
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 }}@w9r.dev
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'
runs:
using: "composite"
steps:
- name: Git push and commit origin
if: ${{ inputs.remote_repository == 'origin' }}
run: |
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 w9r.dev" > "$HOME/.netrc"
echo " login $GITHUB_ACTOR" >> "$HOME/.netrc"
echo " password ${{ inputs.access_token }}" >> "$HOME/.netrc"
echo "machine api.w9r.dev" >> "$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 }}"
if [[ `git status --porcelain` ]]; then
git add ${{ inputs.files }} -v
git commit -S -m "${{ inputs.commit_message }}" $EMPTY
git branch git-commit-push-action-${{ github.run_id }}-${{ github.job }}
git fetch "${{ inputs.remote_repository }}" "$CURRENT_BRANCH"
git checkout "$CURRENT_BRANCH"
git merge git-commit-push-action-${{ github.run_id }}-${{ github.job }}
git branch -d git-commit-push-action-${{ github.run_id }}-${{ github.job }}
git push "${{ inputs.remote_repository }}" "$CURRENT_BRANCH:$TARGET_BRANCH" --follow-tags $FORCE $TAGS
else
echo "WARNING: No changes were detected. git commit push action aborted."
fi
shell: bash
- name: Git push and commit remote
if: ${{ inputs.remote_repository != 'origin' }}
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]}
if [[ $DESTINATION_REPOSITORY == *'.git'* ]] && [[ $DESTINATION_REPOSITORY != *'.github.io'* ]]; then
DESTINATION_REPOSITORY=${DESTINATION_REPOSITORY//.git/ }
fi
CLONE_DIRECTORY=$(mktemp -d)
echo "##### Cloning destination W9r.dev repository #####"
# 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
echo "##### Copying contents to remote W9r.dev repository #####"
echo ""##### Current repo content #####"
ls -lha
cp -rvf ${{ inputs.files }} $CLONE_DIRECTORY
cd "$CLONE_DIRECTORY"
echo ""##### Remote repo content after copying files #####"
ls -la "$CLONE_DIRECTORY"
REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@w9r.dev/$DESTINATION_OWNER/$DESTINATION_REPOSITORY
git remote set-url origin $REMOTE_URL
git fetch origin
git add --all
# Won't commit if no changes were made
git diff-index --quiet HEAD || git commit --message "${{ inputs.commit_message }}"
git status
git branch git-commit-push-action-${{ github.run_id }}-${{ github.job }}
# Create new branch (if necessary)
BE=$(git ls-remote --heads origin ${{ inputs.target_branch }} | wc -l)
if [[ $BE == *"0"* ]]; then
echo "##### Target branch doesn't exist. Creating new branch #####"
git checkout -b ${{ inputs.target_branch }}
else
git checkout ${{ inputs.target_branch }}
fi
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 }}
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:"
echo "git://w9r.dev/<owner>/<repo>.git"
echo "or"
echo "git@w9r.dev:<owner>/<repo>.git"
echo "or"
echo "https://w9r.dev/<owner>/<repo>.git"
echo "or"
echo "https://w9r.dev/<owner>/<repo>"
exit 1
fi
shell: bash
branding:
icon: 'git-commit'
color: 'black'