From bb3e4ea525493219f32767f56ab9f3a9188c667f Mon Sep 17 00:00:00 2001 From: Carles Pina i Estany Date: Sun, 25 Apr 2021 10:55:07 +0100 Subject: [PATCH] Add a check in entrypoint.sh to help users if the source directory does not exist Updated documentation --- README.md | 13 +++++++------ entrypoint.sh | 25 ++++++++++++++++++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 62a66bb..1966903 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ # github-action-push-to-another-repository -Used to push generated files from a directory from Git Action step into another repository on Github. By design it deletes the files from the destination directory as it is meant to "publish" a set generated files. +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. -E.g. -Repository pandoc-test contains Markdown and a Git Action to generate, using Pandoc, an output: HTML, PDF, odt, epub, etc. +Flow: -Repository pandoc-test-output: contains only the generated files from the first Git Action. Pushed here with github-action-push-to-another-repository +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/master/main.md)), during the [GitHub Actions flow](https://github.com/cpina/push-to-another-repository-example/blob/master/.github/workflows/ci.yml#L19) it executes [build.sh](https://github.com/cpina/push-to-another-repository-example/blob/master/build.sh) and the output/ directory (configurable via [source-directory](https://github.com/cpina/push-to-another-repository-example/blob/master/.github/workflows/ci.yml#L27) appears in the [output repository](https://github.com/cpina/push-to-another-repository-output). -And pandoc-test-output can have Git Pages to give access to the files (or just links to the raw version of the files) +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. + +There are different variables to setup the behaviour: ## Inputs ### `source-directory` (argument) @@ -28,7 +29,7 @@ The email that will be used for the commit in the destination-repository-name. The Username/Organization for the destination repository, if different from `destination-github-username`. For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`. ### `target-branch` (argument) [optional] -The branch name for the destination repository, if different from `master`. +The branch name for the destination repository. It defaults to `master` for historical reasons, feel free to change it to `main`. ### `commit-message` (argument) [optional] The commit message to be used in the output repository. Optional and defaults to "Update from $REPOSITORY_URL@commit". diff --git a/entrypoint.sh b/entrypoint.sh index 6832319..987733c 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,26 +27,41 @@ git clone --single-branch --branch "$TARGET_BRANCH" "https://$API_TOKEN_GITHUB@g ls -la "$CLONE_DIR" TARGET_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" -echo "Copying contents to git repo" +if [ ! -d "$SOURCE_DIRECTORY" ] +then + echo "$SOURCE_DIRECTORY does not exist" + echo "Reminder: github-action-push-to-another-repository does not do the checkout" + echo "from the source GitHub repository. The checkout needs to be done using" + echo "a different step. See the actions/checkout@v2 in the example file" + echo "https://github.com/cpina/push-to-another-repository-example/blob/master/.github/workflows/ci.yml#L16" + exit 1 +fi + +echo "Copy contents to target git repository" cp -ra "$SOURCE_DIRECTORY"/. "$TARGET_DIR" cd "$TARGET_DIR" -echo "Files that will be pushed" +echo "Files that will be pushed:" ls -la -echo "Adding git commit" - ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}" +echo "git add:" 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" +echo "git push origin:" # --set-upstream: sets de branch when pushing to a branch that does not exist git push origin --set-upstream "$TARGET_BRANCH"