├── Dockerfile ├── LICENSE ├── entrypoint.sh └── README.md /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.10 2 | 3 | LABEL repository="https://github.com/andstor/copycat-action" 4 | LABEL homepage="https://github.com/andstor/copycat-action" 5 | LABEL "maintainer"="André Storhaug " 6 | 7 | LABEL "com.github.actions.name"="Copycat Action" 8 | LABEL "com.github.actions.description"="Copy files to other repositories" 9 | LABEL "com.github.actions.icon"="copy" 10 | LABEL "com.github.actions.color"="red" 11 | 12 | RUN apk add --no-cache git 13 | 14 | COPY entrypoint.sh /entrypoint.sh 15 | ENTRYPOINT ["/entrypoint.sh"] 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 André Storhaug 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # @author André Storhaug 4 | # @date 2019-07-01 5 | # @copyright MIT 6 | # @version 1.2.0 7 | 8 | set -o pipefail 9 | 10 | if [[ -z "$SRC_PATH" ]]; then 11 | echo "SRC_PATH environment variable is missing. Cannot proceed." 12 | exit 1 13 | fi 14 | 15 | if [[ -z "$DST_OWNER" ]]; then 16 | echo "DST_OWNER environment variable is missing. Cannot proceed." 17 | exit 1 18 | fi 19 | 20 | if [[ -z "$DST_REPO_NAME" ]]; then 21 | echo "DST_REPO_NAME environment variable is missing. Cannot proceed." 22 | exit 1 23 | fi 24 | 25 | if [ "$SRC_WIKI" = "true" ]; then 26 | SRC_WIKI=".wiki" 27 | else 28 | SRC_WIKI="" 29 | fi 30 | 31 | if [ "$DST_WIKI" = "true" ]; then 32 | DST_WIKI=".wiki" 33 | else 34 | DST_WIKI="" 35 | fi 36 | 37 | BASE_PATH=$(pwd) 38 | DST_PATH="${DST_PATH:-${SRC_PATH}}" 39 | 40 | USERNAME="${USERNAME:-${GITHUB_ACTOR}}" 41 | EMAIL="${EMAIL:-${GITHUB_ACTOR}@users.noreply.github.com}" 42 | 43 | SRC_BRANCH="${SRC_BRANCH:-master}" 44 | DST_BRANCH="${DST_BRANCH:-master}" 45 | 46 | SRC_REPO="${GITHUB_REPOSITORY}${SRC_WIKI}" 47 | SRC_REPO_NAME="${GITHUB_REPOSITORY#*/}${SRC_WIKI}" 48 | DST_REPO="${DST_OWNER}/${DST_REPO_NAME}${DST_WIKI}" 49 | DST_REPO_NAME="${DST_REPO_NAME}${DST_WIKI}" 50 | 51 | DIR="${DST_PATH%/*}" 52 | 53 | git config --global user.name "${USERNAME}" 54 | git config --global user.email "${EMAIL}" 55 | 56 | echo "Copying \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}" 57 | 58 | git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${SRC_REPO}.git 59 | if [ "$?" -ne 0 ]; then 60 | echo >&2 "Cloning '$SRC_REPO' failed" 61 | exit 1 62 | fi 63 | rm -rf ${SRC_REPO_NAME}/.git # TODO: Remove every file that matches a filter (issue #1) 64 | 65 | git clone --branch ${DST_BRANCH} --single-branch --depth 1 https://${GH_PAT}@github.com/${DST_REPO}.git 66 | if [ "$?" -ne 0 ]; then 67 | echo >&2 "Cloning '$DST_REPO' failed" 68 | exit 1 69 | fi 70 | 71 | mkdir -p ${DST_REPO_NAME}/${DIR} || exit "$?" 72 | cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} || exit "$?" 73 | cd ${DST_REPO_NAME} || exit "$?" 74 | 75 | if [ -d "${BASE_PATH}/${SRC_REPO_NAME}/${SRC_PATH}" ]; then 76 | COMMIT_MESSAGE="Update file(s) in \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" 77 | else 78 | COMMIT_MESSAGE="Update file \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\"" 79 | fi 80 | 81 | if [ -z "$(git status --porcelain)" ]; then 82 | # Working directory is clean 83 | echo "No changes detected " 84 | else 85 | # Uncommitted changes 86 | git add -A 87 | git commit --message "${COMMIT_MESSAGE}" 88 | git push origin ${DST_BRANCH} 89 | fi 90 | 91 | echo "Copying complete 👌" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Copycat 2 | [GitHub action](https://developer.github.com/actions/) for copying files from one repository to another. 3 | 4 | ## Usage 5 | ``` 6 | name: Copy 7 | on: gollum 8 | jobs: 9 | copycat: 10 | name: Copycat 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@master 14 | - name: Copycat 15 | uses: andstor/copycat-action@v1.1.0 16 | env: 17 | DST_BRANCH: master 18 | DST_OWNER: andstor 19 | DST_REPO_NAME: copycat-action 20 | DST_PATH: /wiki/ 21 | GH_PAT: ${{ secrets.GH_PAT }} 22 | SRC_BRANCH: master 23 | SRC_PATH: /. 24 | SRC_WIKI: "true" 25 | USERNAME: nutsbot 26 | EMAIL: andr3.storhaug+bot@gmail.com 27 | 28 | ``` 29 | 30 | ## Environment variables 31 | The following environment variable options can/must be configured: 32 | 33 | |Environment variable|Required|Description|Default| 34 | |--------------------|--------|-----------|-------| 35 | |`SRC_PATH`|Required|The source path to the file(s) or folder(s) to copy from. For example, `/.` or `path/to/home.md`.|| 36 | |`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`| 37 | |`DST_OWNER`|Required|The name of the owner of the repository to push to. For example, `andstor`.|| 38 | |`DST_REPO_NAME`|Required|The name of the repository to push to. For example, `copycat-action`.|| 39 | |`SRC_BRANCH`|Optional|The branch name of the source repository. Optional.|`master`| 40 | |`DST_BRANCH`|Optional|The branch name of the destination repository. Optional.|`master`| 41 | |`SRC_WIKI`|Optional|Set to `true` if the source repository you want to copy from is the GitHub Wiki.| `false`| 42 | |`DST_WIKI`|Optional|Set to `true` if the destination repository you want to copy from is the GitHub Wiki.|`false`| 43 | |`USERNAME`|Optional|The GitHub username to associate commits made by this GitHub action.|[`GITHUB_ACTOR`](https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables)| 44 | |`EMAIL`|Optional|The email used for associating commits made by this GitHub action.|[`GITHUB_ACTOR`](https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables)`@users.noreply.github.com`| 45 | 46 | ## Secrets 47 | * `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). 48 | 49 | ## Author 50 | The Copycat GitHub action is written by [André Storhaug](https://github.com/andstor) 51 | 52 | ## License 53 | This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details. 54 | --------------------------------------------------------------------------------