commit ff8b5f99e8774e8da27fde4f28a7fc72eaeafb72 Author: Zarif Rani Date: Mon Mar 11 05:09:13 2024 +0200 first commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..db90a79 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine:latest + +RUN apk add --no-cache git git-lfs openssh-client + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68a0b07 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Carles Pina Estany + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..752ff10 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# github-action-push-to-another-repository + +See the extensive documentation in https://cpina.github.io/push-to-another-repository-docs/ (includes examples, FAQ, troubleshooting, etc.). + +GitHub repository of the documentation: https://github.com/cpina/push-to-another-repository-docs + +THIS IS NOT ORIGINAL REPO, THIS IS MODEFIED REPO TO WORK WITH GITEA WORKSPACE. + +Things need to be change and added. +- github-server: # no https:// and port number, only hostname + +- ssh-server-port: \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..56f7715 --- /dev/null +++ b/action.yml @@ -0,0 +1,79 @@ +name: Push directory to another repository +description: >- + Useful to push files to another repository to be used, for example, via github + pages +inputs: + source-before-directory: + description: Source before directory from the origin directory + required: false + source-directory: + description: Source directory from the origin directory + required: true + destination-github-username: + description: Name of the destination username/organization + required: true + destination-repository-name: + description: Destination repository + required: true + user-email: + description: Email for the git commit + required: true + github-server: + description: 'Github server' + default: 'github.com' + required: true + user-name: + description: >- + [Optional] Name for the git commit. Defaults to the destination + username/organization name + required: false + default: '' + destination-repository-username: + description: '[Optional] Username/organization for the destination repository' + required: false + default: '' + target-branch: + description: >- + [Optional] set target branch name for the destination repository. Defaults + to "main" + default: main + required: false + commit-message: + description: >- + [Optional] commit message for the output repository. ORIGIN_COMMIT is + replaced by the URL@commit in the origin repo + default: Update from ORIGIN_COMMIT + required: false + target-directory: + description: '[Optional] The directory to wipe and replace in the target repository' + default: '' + required: false + create-target-branch-if-needed: + type: boolean + description: >- + [Optional] create target branch if not exist. Defaults to `false` + default: false + required: false + ssh-server-port: + description: Git ssh server port + required: true +runs: + using: docker + image: Dockerfile + args: + - '${{ inputs.source-before-directory }}' + - '${{ inputs.source-directory }}' + - '${{ inputs.destination-github-username }}' + - '${{ inputs.destination-repository-name }}' + - '${{ inputs.github-server }}' + - '${{ inputs.user-email }}' + - '${{ inputs.user-name }}' + - '${{ inputs.destination-repository-username }}' + - '${{ inputs.target-branch }}' + - '${{ inputs.commit-message }}' + - '${{ inputs.target-directory }}' + - '${{ inputs.create-target-branch-if-needed }}' + - '${{ inputs.ssh-server-port }}' +branding: + icon: git-commit + color: green diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..9959644 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,177 @@ +#!/bin/sh -l + +set -e # if a command fails it stops the execution +set -u # script fails if trying to access to an undefined variable + +echo "[+] Action start and roll" +SOURCE_BEFORE_DIRECTORY="${1}" +SOURCE_DIRECTORY="${2}" +DESTINATION_GITHUB_USERNAME="${3}" +DESTINATION_REPOSITORY_NAME="${4}" +GITHUB_SERVER="${5}" +USER_EMAIL="${6}" +USER_NAME="${7}" +DESTINATION_REPOSITORY_USERNAME="${8}" +TARGET_BRANCH="${9}" +COMMIT_MESSAGE="${10}" +TARGET_DIRECTORY="${11}" +CREATE_TARGET_BRANCH_IF_NEEDED="${12}" +SSH_SERVER_PORT="${13}" + +if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] +then + DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME" +fi + +if [ -z "$USER_NAME" ] +then + USER_NAME="$DESTINATION_GITHUB_USERNAME" +fi + +# Verify that there (potentially) some access to the destination repository +# and set up git (with GIT_CMD variable) and GIT_CMD_REPOSITORY +if [ -n "${SSH_DEPLOY_KEY:=}" ] +then + echo "[+] Using SSH_DEPLOY_KEY" + + # Inspired by https://github.com/leigholiver/commit-with-deploy-key/blob/main/entrypoint.sh , thanks! + mkdir --parents "$HOME/.ssh" + DEPLOY_KEY_FILE="$HOME/.ssh/id_ed25519" + echo "${SSH_DEPLOY_KEY}" > "$DEPLOY_KEY_FILE" + chmod 600 "$DEPLOY_KEY_FILE" + + SSH_KNOWN_HOSTS_FILE="$HOME/.ssh/known_hosts" + echo "[+] ssh-keyscan $GITHUB_SERVER:$SSH_SERVER_PORT" + ssh-keyscan -p "$SSH_SERVER_PORT" -H "$GITHUB_SERVER" > "$SSH_KNOWN_HOSTS_FILE" + + # export GIT_SSH_COMMAND="ssh -i "$DEPLOY_KEY_FILE" -o UserKnownHostsFile=$SSH_KNOWN_HOSTS_FILE" + + GIT_CMD_REPOSITORY="ssh://git@$GITHUB_SERVER:$SSH_SERVER_PORT/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" + +elif [ -n "${API_TOKEN_GITHUB:=}" ] +then + echo "[+] Using API_TOKEN_GITHUB" + GIT_CMD_REPOSITORY="https://$DESTINATION_REPOSITORY_USERNAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" +else + echo "::error::API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one (recommended the SSH_DEPLOY_KEY)" + exit 1 +fi + + +CLONE_DIR=$(mktemp -d) + +echo "[+] Git version" +git --version + +echo "[+] Enable git lfs" +git lfs install + +echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME" + +# Setup git +git config --global user.email "$USER_EMAIL" +git config --global user.name "$USER_NAME" + +# workaround for https://github.com/cpina/github-action-push-to-another-repository/issues/103 +git config --global http.version HTTP/1.1 + +{ + git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR" +} || { + if [ "$CREATE_TARGET_BRANCH_IF_NEEDED" = "true" ] + then + # Default branch of the repository is cloned. Later on the required branch + # will be created + git clone --single-branch --depth 1 "$GIT_CMD_REPOSITORY" "$CLONE_DIR" + else + false + fi +} || { + echo "::error::Could not clone the destination repository. Command:" + echo "::error::git clone --single-branch --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR" + echo "::error::(Note that if they exist USER_NAME and API_TOKEN is redacted by GitHub)" + echo "::error::Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB OR SSH_DEPLOY_KEY" + exit 1 + +} +ls -la "$CLONE_DIR" + +TEMP_DIR=$(mktemp -d) +# This mv has been the easier way to be able to remove files that were there +# but not anymore. Otherwise we had to remove the files from "$CLONE_DIR", +# including "." and with the exception of ".git/" +mv "$CLONE_DIR/.git" "$TEMP_DIR/.git" + +# $TARGET_DIRECTORY is '' by default +ABSOLUTE_TARGET_DIRECTORY="$CLONE_DIR/$TARGET_DIRECTORY/" + +echo "[+] Deleting $ABSOLUTE_TARGET_DIRECTORY" +rm -rf "$ABSOLUTE_TARGET_DIRECTORY" + +echo "[+] Creating (now empty) $ABSOLUTE_TARGET_DIRECTORY" +mkdir -p "$ABSOLUTE_TARGET_DIRECTORY" + +echo "[+] Listing Current Directory Location" +ls -al + +echo "[+] Listing root Location" +ls -al / + +mv "$TEMP_DIR/.git" "$CLONE_DIR/.git" + +echo "[+] List contents of $SOURCE_DIRECTORY" +ls "$SOURCE_DIRECTORY" + +echo "[+] Checking if local $SOURCE_DIRECTORY exist" +if [ ! -d "$SOURCE_DIRECTORY" ] +then + echo "ERROR: $SOURCE_DIRECTORY does not exist" + echo "This directory needs to exist when push-to-another-repository is executed" + echo + echo "In the example it is created by ./build.sh: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19" + echo + echo "If you want to copy a directory that exist in the source repository" + echo "to the target repository: you need to clone the source repository" + echo "in a previous step in the same build section. For example using" + echo "actions/checkout@v2. See: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L16" + exit 1 +fi + +echo "[+] Copying contents of source repository folder $SOURCE_DIRECTORY to folder $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME" +cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY" +cd "$CLONE_DIR" + +echo "[+] Files that will be pushed" +ls -la + +ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" +COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}" +COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}" + +echo "[+] Set directory is safe ($CLONE_DIR)" +# Related to https://github.com/cpina/github-action-push-to-another-repository/issues/64 +git config --global --add safe.directory "$CLONE_DIR" + +if [ "$CREATE_TARGET_BRANCH_IF_NEEDED" = "true" ] +then + echo "[+] Switch to the TARGET_BRANCH" + # || true: if the $TARGET_BRANCH already existed in the destination repo: + # it is already the current branch and it cannot be switched to + # (it's not needed) + # If the branch did not exist: it switches (creating) the branch + git switch -c "$TARGET_BRANCH" || true +fi + +echo "[+] Adding git commit" +git add . + +echo "[+] git status:" +git status + +echo "[+] git diff-index:" +# git diff-index : to avoid doing the git commit failing if there are no changes to be commit +git diff-index --quiet HEAD || git commit --message "$COMMIT_MESSAGE" + +echo "[+] Pushing git commit" +# --set-upstream: sets de branch when pushing to a branch that does not exist +git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"