diff --git a/README.md b/README.md index 6aaa628..adfbc45 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ _**Note**: This action is supported on **all runners** operating systems (`ubunt steps: - uses: actions/checkout@v2.3.4 # [...] --> 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 @@ -33,7 +33,7 @@ _**Note**: This action is supported on **all runners** operating systems (`ubunt steps: - uses: actions/checkout@v2.3.4 # [...] --> steps with actions or commands updating repository files - - uses: GuillaumeFalourd/git-commit-push@v1.1 + - uses: GuillaumeFalourd/git-commit-push@v1.2 with: email: ${{ github.actor }}@users.noreply.github.com name: ${{ github.actor }} diff --git a/action.yml b/action.yml index 47fda29..f81c62f 100644 --- a/action.yml +++ b/action.yml @@ -52,7 +52,8 @@ outputs: runs: using: "composite" steps: - - name: Git push and commit + - name: Git push and commit origin + if: ${{ inputs.remote_repository == 'origin' }} run: | CURRENT_BRANCH=${GITHUB_REF} case $CURRENT_BRANCH in "refs/heads/"*) @@ -83,7 +84,7 @@ runs: 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 -m "${{ inputs.commit_message }}" $EMPTY @@ -97,7 +98,83 @@ runs: 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 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//.git" + echo "or" + echo "git@github.com:/.git" + echo "or" + echo "https://github.com//.git" + echo "or" + echo "https://github.com//" + exit 1 + fi shell: bash branding: diff --git a/reports/macos_report_minimal.txt b/reports/macos_report_minimal.txt index 676c933..dd38453 100644 --- a/reports/macos_report_minimal.txt +++ b/reports/macos_report_minimal.txt @@ -1 +1 @@ -Thu Jan 6 04:17:26 UTC 2022 +Fri Jan 7 04:17:05 UTC 2022 diff --git a/reports/ubuntu_report_minimal.txt b/reports/ubuntu_report_minimal.txt index 79751bd..49af995 100644 --- a/reports/ubuntu_report_minimal.txt +++ b/reports/ubuntu_report_minimal.txt @@ -1 +1 @@ -Thu Jan 6 02:32:39 UTC 2022 +Fri Jan 7 02:32:59 UTC 2022 diff --git a/reports/windows_report_minimal.txt b/reports/windows_report_minimal.txt index 72d2d8d..6c17bba 100644 --- a/reports/windows_report_minimal.txt +++ b/reports/windows_report_minimal.txt @@ -1 +1 @@ -Thu Jan 6 06:23:22 CUT 2022 +Fri Jan 7 06:23:21 CUT 2022