This commit is contained in:
GuillaumeFalourd 2022-01-07 10:00:06 -03:00
commit d7246e25c4
5 changed files with 84 additions and 7 deletions

View file

@ -24,7 +24,7 @@ _**Note**: This action is supported on **all runners** operating systems (`ubunt
steps: steps:
- uses: actions/checkout@v2.3.4 - uses: actions/checkout@v2.3.4
# [...] --> steps with actions or commands updating repository files # [...] --> steps with actions or commands updating repository files
- uses: GuillaumeFalourd/git-commit-push@v1.1 - uses: GuillaumeFalourd/git-commit-push@v1.2
``` ```
### `2⃣ Full`: Commit and Push with `customized` parameters ### `2⃣ Full`: Commit and Push with `customized` parameters
@ -33,7 +33,7 @@ _**Note**: This action is supported on **all runners** operating systems (`ubunt
steps: steps:
- uses: actions/checkout@v2.3.4 - uses: actions/checkout@v2.3.4
# [...] --> steps with actions or commands updating repository files # [...] --> steps with actions or commands updating repository files
- uses: GuillaumeFalourd/git-commit-push@v1.1 - uses: GuillaumeFalourd/git-commit-push@v1.2
with: with:
email: ${{ github.actor }}@users.noreply.github.com email: ${{ github.actor }}@users.noreply.github.com
name: ${{ github.actor }} name: ${{ github.actor }}

View file

@ -52,7 +52,8 @@ outputs:
runs: runs:
using: "composite" using: "composite"
steps: steps:
- name: Git push and commit - name: Git push and commit origin
if: ${{ inputs.remote_repository == 'origin' }}
run: | run: |
CURRENT_BRANCH=${GITHUB_REF} CURRENT_BRANCH=${GITHUB_REF}
case $CURRENT_BRANCH in "refs/heads/"*) case $CURRENT_BRANCH in "refs/heads/"*)
@ -83,7 +84,7 @@ runs:
git config --local user.email "${{ inputs.email }}" git config --local user.email "${{ inputs.email }}"
git config --local user.name "${{ inputs.name }}" git config --local user.name "${{ inputs.name }}"
if [[ `git status --porcelain` ]]; then if [[ `git status --porcelain` ]]; then
git add ${{ inputs.files }} -v git add ${{ inputs.files }} -v
git commit -m "${{ inputs.commit_message }}" $EMPTY git commit -m "${{ inputs.commit_message }}" $EMPTY
@ -97,7 +98,83 @@ runs:
else else
echo "WARNING: No changes were detected. git commit push action aborted." echo "WARNING: No changes were detected. git commit push action aborted."
fi 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 Github 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"
ls -la "$CLONE_DIRECTORY"
echo
echo "##### Copying contents to destination Github repository #####"
cp -rvf "${{ inputs.files }}" "$CLONE_DIRECTORY"
cd "$CLONE_DIRECTORY"
REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY
git remote set-url origin $REMOTE_URL
git fetch origin
git add ${{ inputs.files }}
# Won't commit if no changes were made
git diff-index --quiet HEAD || git commit --message "${{ inputs.commit_message }}"
git status
git branch copy-push-files-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 copy-push-files-action-${{ github.run_id }}-${{ github.job }}
git branch -d copy-push-files-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://github.com/<owner>/<repo>.git"
echo "or"
echo "git@github.com:<owner>/<repo>.git"
echo "or"
echo "https://github.com/<owner>/<repo>.git"
echo "or"
echo "https://github.com/<owner>/<repo>"
exit 1
fi
shell: bash shell: bash
branding: branding:

View file

@ -1 +1 @@
Thu Jan 6 04:17:26 UTC 2022 Fri Jan 7 04:17:05 UTC 2022

View file

@ -1 +1 @@
Thu Jan 6 02:32:39 UTC 2022 Fri Jan 7 02:32:59 UTC 2022

View file

@ -1 +1 @@
Thu Jan 6 06:23:22 CUT 2022 Fri Jan 7 06:23:21 CUT 2022