diff --git a/Dockerfile b/Dockerfile index d27f30b..4baf5a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.12 +FROM alpine:latest RUN apk add --no-cache git diff --git a/README.md b/README.md index a6839fd..1f5ff35 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# github-action-push-to-another-repository +# github-action-push-to-another-repository-directory When to use this GitHub Action? It is useful in case that you have a GitHub repository with a a directory that you want to push to another GitHub repository using GitHub Actions (automated on push, for example). It is also useful if using GitHub Actions you generate certain files that you want to push to another GitHub repository. @@ -6,7 +6,7 @@ Flow: The [example repository](https://github.com/cpina/push-to-another-repository-example) has a MarkDown file [main.md](https://github.com/cpina/push-to-another-repository-example/blob/main/main.md)), during the [GitHub Actions flow](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19) it executes [build.sh](https://github.com/cpina/push-to-another-repository-example/blob/main/build.sh) and the output/ directory (configurable via [source-directory](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L27) appears in the [output repository](https://github.com/cpina/push-to-another-repository-output). -Please bear in mind: files in the target repository are deleted. This is to make sure that it contains only the generated files in the last run without previously generated files. +Please bear in mind: files in the target repository's specified directory are deleted. This is to make sure that it contains only the generated files in the last run without previously generated files. There are different variables to setup the behaviour: @@ -37,6 +37,9 @@ The branch name for the destination repository. It defaults to `main` for histor ### `commit-message` (argument) [optional] The commit message to be used in the output repository. Optional and defaults to "Update from $REPOSITORY_URL@commit". +### `target-directory` (argument) [optional] +The directory to wipe and replace in the target repository. Defaults to wiping the entire repository + The string `ORIGIN_COMMIT` is replaced by `$REPOSITORY_URL@commit`. ### `API_TOKEN_GITHUB` (environment) diff --git a/action.yml b/action.yml index 7501040..bb093bc 100644 --- a/action.yml +++ b/action.yml @@ -1,20 +1,27 @@ -name: 'Push directory to another repository' -description: 'Useful to push files to another repository to be used, for example, via github pages' +name: Push directory to another repository directory +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' + description: Source directory from the origin directory required: true destination-github-username: - description: 'Name of the destination username/organization' + description: Name of the destination username/organization required: true destination-repository-name: - description: 'Destination repository' + description: Destination repository required: true user-email: - description: 'Email for the git commit' + description: Email for the git commit required: true user-name: - description: '[Optional] Name for the git commit. Defaults to the destination username/organization name' + description: >- + [Optional] Name for the git commit. Defaults to the destination + username/organization name required: false default: '' destination-repository-username: @@ -22,25 +29,35 @@ inputs: required: false default: '' target-branch: - description: '[Optional] set target branch name for the destination repository. Defaults to "master" for historical reasons' - default: 'master' + description: >- + [Optional] set target branch name for the destination repository. Defaults + to "main" for historical reasons + 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' + 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 runs: - using: 'docker' - image: 'Dockerfile' + using: docker + image: Dockerfile args: - - ${{ inputs.source-directory }} - - ${{ inputs.destination-github-username }} - - ${{ inputs.destination-repository-name }} - - ${{ inputs.user-email }} - - ${{ inputs.user-name }} - - ${{ inputs.destination-repository-username }} - - ${{ inputs.target-branch }} - - ${{ inputs.commit-message }} + - '${{ inputs.source-before-directory }}' + - '${{ inputs.source-directory }}' + - '${{ inputs.destination-github-username }}' + - '${{ inputs.destination-repository-name }}' + - '${{ inputs.user-email }}' + - '${{ inputs.user-name }}' + - '${{ inputs.destination-repository-username }}' + - '${{ inputs.target-branch }}' + - '${{ inputs.commit-message }}' + - '${{ inputs.target-directory }}' branding: - icon: 'git-commit' - color: 'green' + icon: git-commit + color: green diff --git a/entrypoint.sh b/entrypoint.sh index 4e46c3e..7ec1178 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,15 +3,17 @@ set -e # if a command fails it stops the execution set -u # script fails if trying to access to an undefined variable -echo "Starts" -SOURCE_DIRECTORY="$1" -DESTINATION_GITHUB_USERNAME="$2" -DESTINATION_REPOSITORY_NAME="$3" -USER_EMAIL="$4" -USER_NAME="$5" -DESTINATION_REPOSITORY_USERNAME="$6" -TARGET_BRANCH="$7" -COMMIT_MESSAGE="$8" +echo "[+] Action start" +SOURCE_BEFORE_DIRECTORY="$1" +SOURCE_DIRECTORY="$2" +DESTINATION_GITHUB_USERNAME="$3" +DESTINATION_REPOSITORY_NAME="$4" +USER_EMAIL="$5" +USER_NAME="$6" +DESTINATION_REPOSITORY_USERNAME="$7" +TARGET_BRANCH="$8" +COMMIT_MESSAGE="$9" +TARGET_DIRECTORY="${10}" if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] then @@ -25,19 +27,48 @@ fi CLONE_DIR=$(mktemp -d) -echo "Cloning destination git repository" +echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME" # Setup git git config --global user.email "$USER_EMAIL" git config --global user.name "$USER_NAME" git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR" ls -la "$CLONE_DIR" -TARGET_DIR=$(mktemp -d) +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" "$TARGET_DIR" +mv "$CLONE_DIR/.git" "$TEMP_DIR/.git" +echo "[+] Checking if $TARGET_DIRECTORY exist in git repo $DESTINATION_REPOSITORY_NAME" +# Remove contents of target directory and create a new empty one +if [ -d "$CLONE_DIR/$TARGET_DIRECTORY/" ] +then +echo "[+] Deleting files from $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME" +rm -R "${CLONE_DIR:?}/$TARGET_DIRECTORY/" +fi +echo "[+] Creating $TARGET_DIRECTORY if doesnt already exist" +mkdir -p "$CLONE_DIR/$TARGET_DIRECTORY" + +mv "$TEMP_DIR/.git" "$CLONE_DIR/.git" + +echo "[+] Listing Current Directory Location" +ls -al +#echo "[+] Listing home+ Directory Location" +#ls -al /home/runner/work/Action_OpenWRT_AutoBuild_Linksys_Devices/Action_OpenWRT_AutoBuild_Linksys_Devices/openwrt +echo "[+] Listing root Location" +ls -al / +echo "[+] Listing /home Location" +ls -al /home + +# echo "[+] Changing to $SOURCE_BEFORE_DIRECTORY" +# cd "$SOURCE_BEFORE_DIRECTORY" + + +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" @@ -52,27 +83,27 @@ then exit 1 fi -echo "Copy contents to target git repository" -cp -ra "$SOURCE_DIRECTORY"/. "$TARGET_DIR" -cd "$TARGET_DIR" +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:" +echo "[+] Files that will be pushed" ls -la ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}" COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}" -echo "git add:" +echo "[+] Adding git commit" git add . -echo "git status:" +echo "[+] git status:" git status -echo "git diff-index:" +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 "git push origin:" +echo "[+] Pushing git commit" # --set-upstream: sets de branch when pushing to a branch that does not exist git push "https://$USER_NAME:$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH"