From 94f2d4deb1bd27b995e813ac0b3721636305555e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Mon, 1 Jul 2019 00:09:22 +0200 Subject: [PATCH 01/11] Correct mkdir command --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 681adf1..b152817 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,7 +49,7 @@ echo "Copying \"${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}" git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${GITHUB_TOKEN}@github.com/${SRC_REPO}.git git clone --branch ${DST_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${DST_REPO}.git -mkdir -p DIR +mkdir -p ${DST_REPO_NAME}/${DIR} cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} cd ${DST_REPO_NAME} From 63e464593246ae77b7d6d33bda3755d1c48c703c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Mon, 1 Jul 2019 00:25:01 +0200 Subject: [PATCH 02/11] Minor correction --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index b152817..2d04033 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -38,7 +38,7 @@ SRC_REPO_NAME="${GITHUB_REPOSITORY#*/}${SRC_WIKI}" DST_REPO="${DST_OWNER}/${DST_REPO_NAME}${DST_WIKI}" DST_REPO_NAME="${DST_REPO_NAME}${DST_WIKI}" -DIR="${SRC_PATH%/*}" +DIR="${DST_PATH%/*}" git config --global user.name "${GITHUB_ACTOR}" git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com" From 7aebbe94343605299f12857db94678e6682baff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Mon, 1 Jul 2019 03:19:02 +0200 Subject: [PATCH 03/11] Remove .git folder --- entrypoint.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 2d04033..c88ffe9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -43,19 +43,22 @@ DIR="${DST_PATH%/*}" git config --global user.name "${GITHUB_ACTOR}" git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com" -echo "Copying \"${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}" +echo "Copying \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}" git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${GITHUB_TOKEN}@github.com/${SRC_REPO}.git +rm -rf ${SRC_REPO_NAME}/.git + git clone --branch ${DST_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${DST_REPO}.git + mkdir -p ${DST_REPO_NAME}/${DIR} cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} cd ${DST_REPO_NAME} git add -A -git commit --message "Update \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +git commit --message "Update \"${SRC_PATH##*/}\" from \"${GITHUB_REPOSITORY}\"" git push -u origin ${DST_BRANCH} From 628ce7e72f8d2e624b84e46a0faa509a70d4391c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Mon, 1 Jul 2019 03:26:01 +0200 Subject: [PATCH 04/11] Smallfix commit messages --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index c88ffe9..4c20d4b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -58,7 +58,7 @@ cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} cd ${DST_REPO_NAME} git add -A -git commit --message "Update \"${SRC_PATH##*/}\" from \"${GITHUB_REPOSITORY}\"" +git commit --message "Update \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" git push -u origin ${DST_BRANCH} From 43fdd3fd3bb67a1aa8316e526f9ca7f3ef39ca0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Mon, 1 Jul 2019 23:06:33 +0200 Subject: [PATCH 05/11] Add error handling --- entrypoint.sh | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 4c20d4b..cc90e6d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,10 @@ #!/bin/sh -set -eo pipefail +# Purpose: Detecting Hardware Errors +# Author: Vivek Gite +# Last updated on : 28-Aug-2007 +# ----------------------------------------------- + +set -o pipefail if [[ -z "${SRC_PATH}" ]]; then echo "SRC_PATH environment variable is missing. Cannot proceed." @@ -45,21 +50,33 @@ git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com" echo "Copying \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}" - git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${GITHUB_TOKEN}@github.com/${SRC_REPO}.git -rm -rf ${SRC_REPO_NAME}/.git +if [ "$?" -ne 0 ]; then + echo >&2 "Cloning '$SRC_REPO' failed" + exit 1 +fi +rm -rf ${SRC_REPO_NAME}/.git # TODO remove every file that matches a filter. See issue #1. git clone --branch ${DST_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${DST_REPO}.git +if [ "$?" -ne 0 ]; then + echo >&2 "Cloning '$DST_REPO' failed" + exit 1 +fi +mkdir -p ${DST_REPO_NAME}/${DIR} || exit "$?" +cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} || exit "$?" -mkdir -p ${DST_REPO_NAME}/${DIR} -cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} - -cd ${DST_REPO_NAME} +cd ${DST_REPO_NAME} || exit "$?" -git add -A -git commit --message "Update \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +if [ -z "$(git status --porcelain)" ]; then + # Working directory is clean + echo "No changes detected " +else + # Uncommitted changes + git add -A + git commit --message "Update \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" -git push -u origin ${DST_BRANCH} + git push -u origin ${DST_BRANCH} +fi echo "Copying complete đŸ‘Œ" \ No newline at end of file From 8a4600fd1de572b3e04e6332b9d305c440cc048b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Tue, 2 Jul 2019 02:26:55 +0200 Subject: [PATCH 06/11] Fix boolean checks --- entrypoint.sh | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index cc90e6d..7852bad 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,33 +1,34 @@ #!/bin/sh -# Purpose: Detecting Hardware Errors -# Author: Vivek Gite -# Last updated on : 28-Aug-2007 -# ----------------------------------------------- +# +# @author AndrĂ© Storhaug +# @date 2019-07-01 +# @copyright MIT +# @version 0.1.2 set -o pipefail -if [[ -z "${SRC_PATH}" ]]; then +if [[ -z "$SRC_PATH" ]]; then echo "SRC_PATH environment variable is missing. Cannot proceed." exit 1 fi -if [[ -z "${DST_OWNER}" ]]; then +if [[ -z "$DST_OWNER" ]]; then echo "DST_OWNER environment variable is missing. Cannot proceed." exit 1 fi -if [[ -z "${DST_REPO_NAME}" ]]; then +if [[ -z "$DST_REPO_NAME" ]]; then echo "DST_REPO_NAME environment variable is missing. Cannot proceed." exit 1 fi -if [ -n "${SRC_WIKI}" = true ]; then +if [ "$SRC_WIKI" = "true" ]; then SRC_WIKI=".wiki" else SRC_WIKI="" fi -if [ -n "${DST_WIKI}" = true ]; then +if [ "$DST_WIKI" = "true" ]; then DST_WIKI=".wiki" else DST_WIKI="" @@ -55,7 +56,7 @@ if [ "$?" -ne 0 ]; then echo >&2 "Cloning '$SRC_REPO' failed" exit 1 fi -rm -rf ${SRC_REPO_NAME}/.git # TODO remove every file that matches a filter. See issue #1. +rm -rf ${SRC_REPO_NAME}/.git # TODO: Remove every file that matches a filter (issue #1) git clone --branch ${DST_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${DST_REPO}.git if [ "$?" -ne 0 ]; then From 67c017388f0ebcc69e7c44c60593c4ece604b819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Tue, 2 Jul 2019 02:36:01 +0200 Subject: [PATCH 07/11] Update readme --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 49cf5d0..0e39c32 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Copycat GitHub action for copying files from one repository to another. -##Usage +## Usage ``` ``` @@ -9,16 +9,16 @@ GitHub action for copying files from one repository to another. The following environment variable options can be configured: -|Environment variable|Description| Type |Default| -|-------|-----------| ---- |-------| -|`SRC_PATH`|The source path to the file(s) or folder(s) to copy from. For example,`home.md`|`String`|| -|`DST_PATH`|The destination path to copy the file(s) or folder(s) to. For example, `pages/.`. |`String`|`SRC_PATH`| -|`DST_OWNER`|The name of the owner of the repository to push to. For example, `andstor`.|`String`|| -|`DST_REPO_NAME`|The name of the repository to push to. For example, `copycat-action`.|`String`|| -|`SRC_BRANCH`|The branch name of the source repository. Optional.|`String`|`master`| -|`DST_BRANCH`|The branch name of the destination repository. Optional.|`String`|`master`| -|`SRC_WIKI`|If the source repository you want to copy from is the GitHub Wiki.|`Boolean`| `false`| -|`DST_WIKI`|If the destination repository you want to copy from is the GitHub Wiki.|`Boolean`| `false`| +|Environment variable|Description|Default| +|-------|-----------| ---- | +|`SRC_PATH`|The source path to the file(s) or folder(s) to copy from. For example,`home.md`|| +|`DST_PATH`|The destination path to copy the file(s) or folder(s) to. For example, `pages/.`. |`SRC_PATH`| +|`DST_OWNER`|The name of the owner of the repository to push to. For example, `andstor`.|| +|`DST_REPO_NAME`|The name of the repository to push to. For example, `copycat-action`.|| +|`SRC_BRANCH`|The branch name of the source repository. Optional.|`master`| +|`DST_BRANCH`|The branch name of the destination repository. Optional.|`master`| +|`SRC_WIKI`|Set to `true` if the source repository you want to copy from is the GitHub Wiki.| `false`| +|`DST_WIKI`|Set to `true` if the destination repository you want to copy from is the GitHub Wiki.|`false`| ## Secrets From 00b51296a32ce87725b0642fca8a44d105ce1e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Mon, 8 Jul 2019 23:04:54 +0200 Subject: [PATCH 08/11] Replace GITHUB_TOKEN with GH_PAT to work with Wiki --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 7852bad..9b3fa34 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,7 +51,7 @@ git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com" echo "Copying \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}" -git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${GITHUB_TOKEN}@github.com/${SRC_REPO}.git +git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${SRC_REPO}.git if [ "$?" -ne 0 ]; then echo >&2 "Cloning '$SRC_REPO' failed" exit 1 From 6a9c0dc435152d91268521e6694dda72cd63ae4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Tue, 9 Jul 2019 00:23:11 +0200 Subject: [PATCH 09/11] Differentiate between file or dir in commit message --- entrypoint.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9b3fa34..c65cc80 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -75,7 +75,12 @@ if [ -z "$(git status --porcelain)" ]; then else # Uncommitted changes git add -A - git commit --message "Update \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +if [ -d /bin ]; then + COMMIT_MESSAGE="Update file(s) in \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +else + COMMIT_MESSAGE="Update file \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +fi + git commit --message "${COMMIT_MESSAGE}" git push -u origin ${DST_BRANCH} fi From c983888fad66a907f91fbd2ed3c4030fcf9c136c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Tue, 9 Jul 2019 00:31:25 +0200 Subject: [PATCH 10/11] Actually fix better commit message --- entrypoint.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index c65cc80..983c278 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -34,6 +34,7 @@ else DST_WIKI="" fi +BASE_PATH=$(pwd) DST_PATH="${DST_PATH:-${SRC_PATH}}" SRC_BRANCH="${SRC_BRANCH:-master}" @@ -66,20 +67,20 @@ fi mkdir -p ${DST_REPO_NAME}/${DIR} || exit "$?" cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} || exit "$?" - cd ${DST_REPO_NAME} || exit "$?" +if [ -d "${BASE_PATH}/${SRC_REPO_NAME}/${SRC_PATH}" ]; then + COMMIT_MESSAGE="Update file(s) in \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +else + COMMIT_MESSAGE="Update file \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" +fi + if [ -z "$(git status --porcelain)" ]; then # Working directory is clean echo "No changes detected " else # Uncommitted changes git add -A -if [ -d /bin ]; then - COMMIT_MESSAGE="Update file(s) in \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" -else - COMMIT_MESSAGE="Update file \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" -fi git commit --message "${COMMIT_MESSAGE}" git push -u origin ${DST_BRANCH} From f0aa8c8bfda981fe0bede837d542e0f712357344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Storhaug?= Date: Tue, 9 Jul 2019 01:32:55 +0200 Subject: [PATCH 11/11] Update readme file --- README.md | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 0e39c32..0a0ba8d 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,40 @@ # Copycat -GitHub action for copying files from one repository to another. +[GitHub action](https://developer.github.com/actions/) for copying files from one repository to another. ## Usage ``` +action "Copycat" { + uses = "andstor/copycat-action@v1.0.0" + secrets = ["GH_PAT"] + env = { + DST_OWNER = "andstor" + DST_REPO_NAME = "copycat-action" + SRC_WIKI = "true" + DST_PATH = "/wiki/" + SRC_PATH = "/." + } +} ``` ## Environment variables +The following environment variable options can/must be configured: -The following environment variable options can be configured: - -|Environment variable|Description|Default| -|-------|-----------| ---- | -|`SRC_PATH`|The source path to the file(s) or folder(s) to copy from. For example,`home.md`|| -|`DST_PATH`|The destination path to copy the file(s) or folder(s) to. For example, `pages/.`. |`SRC_PATH`| -|`DST_OWNER`|The name of the owner of the repository to push to. For example, `andstor`.|| -|`DST_REPO_NAME`|The name of the repository to push to. For example, `copycat-action`.|| -|`SRC_BRANCH`|The branch name of the source repository. Optional.|`master`| -|`DST_BRANCH`|The branch name of the destination repository. Optional.|`master`| -|`SRC_WIKI`|Set to `true` if the source repository you want to copy from is the GitHub Wiki.| `false`| -|`DST_WIKI`|Set to `true` if the destination repository you want to copy from is the GitHub Wiki.|`false`| - +|Environment variable|Required|Description|Default| +|--------------------|--------|-----------|-------| +|`SRC_PATH`|Required|The source path to the file(s) or folder(s) to copy from. For example, `/.` or `path/to/home.md`.|| +|`DST_PATH`|Optional|The destination path to copy the file(s) or folder(s) to. For example, `/wiki/` or `path/to/index.md`. |`SRC_PATH`| +|`DST_OWNER`|Required|The name of the owner of the repository to push to. For example, `andstor`.|| +|`DST_REPO_NAME`|Required|The name of the repository to push to. For example, `copycat-action`.|| +|`SRC_BRANCH`|Optional|The branch name of the source repository. Optional.|`master`| +|`DST_BRANCH`|Optional|The branch name of the destination repository. Optional.|`master`| +|`SRC_WIKI`|Optional|Set to `true` if the source repository you want to copy from is the GitHub Wiki.| `false`| +|`DST_WIKI`|Optional|Set to `true` if the destination repository you want to copy from is the GitHub Wiki.|`false`| ## Secrets -* `GITHUB_TOKEN`: (required) Include the [GitHub token secret](https://developer.github.com/actions/creating-workflows/storing-secrets/#github-token-secret) to make authenticated calls to the GitHub API for the **source repository**. +* `GH_PAT`: (required) GitHub Private Access Token used for the clone/push operations. To create it follow the [GitHub Documentation](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line). + +## Author +The Copycat GitHub action is written by [AndrĂ© Storhaug](https://github.com/andstor) -* `GH_PAT`: (required) GitHub Private Access Token used for the clone/push operations for the **destination repository**. To create it follow the [GitHub Documentation](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line). \ No newline at end of file +## License +This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details.