Skip to content

Commit

Permalink
Merge pull request #8 from andstor/develop
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
andstor authored Jul 8, 2019
2 parents 59f5e7a + f0aa8c8 commit 5885585
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 32 deletions.
47 changes: 30 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
## Usage
```
action "Copycat" {
uses = "andstor/[email protected]"
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| 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|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) <[email protected]>

* `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).
## License
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details.
57 changes: 42 additions & 15 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
#!/bin/sh
set -eo pipefail
#
# @author André Storhaug <[email protected]>
# @date 2019-07-01
# @copyright MIT
# @version 0.1.2

if [[ -z "${SRC_PATH}" ]]; then
set -o pipefail

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=""
fi

BASE_PATH=$(pwd)
DST_PATH="${DST_PATH:-${SRC_PATH}}"

SRC_BRANCH="${SRC_BRANCH:-master}"
Expand All @@ -38,25 +45,45 @@ 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"

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://${GH_PAT}@github.com/${SRC_REPO}.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 (issue #1)

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
if [ "$?" -ne 0 ]; then
echo >&2 "Cloning '$DST_REPO' failed"
exit 1
fi

mkdir -p DIR
cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH}
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 "$?"

cd ${DST_REPO_NAME}
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

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 "${COMMIT_MESSAGE}"

git push -u origin ${DST_BRANCH}
git push -u origin ${DST_BRANCH}
fi

echo "Copying complete 👌"

0 comments on commit 5885585

Please sign in to comment.