Skip to content

Commit b14e6d3

Browse files
authored
Merge pull request #24 from andstor/develop
v3.0.0
2 parents beb01c3 + f8e71cd commit b14e6d3

File tree

5 files changed

+128
-31
lines changed

5 files changed

+128
-31
lines changed

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org).
6+
7+
## [Unreleased]
8+
9+
## [3.0.0] - 2020-03-10
10+
### Added
11+
- Input variable `clean` for optional removal of contents in the `dst_path` before copying.
12+
- Input variable `exclude` for path exclusion filter with glob patterns.
13+
- Input variable `filter` for path filtering with glob patterns.
14+
### Changed
15+
- Renamed input variable `src_filter` to `file_filter`.
16+
17+
## [2.0.0] - 2020-02-23
18+
19+
## [1.1.0] - 2019-08-29
20+
21+
## [1.0.1] - 2019-07-09
22+
23+
## 1.0.0 - 2019-07-09
24+
25+
[Unreleased]: https://github.com/andstor/copycat-action/compare/v3.0.0...HEAD
26+
[3.0.0]: https://github.com/andstor/copycat-action/compare/v2.0.0...v3.0.0
27+
[2.0.0]: https://github.com/andstor/copycat-action/compare/v1.1.0...v2.0.0
28+
[1.1.0]: https://github.com/andstor/copycat-action/compare/v1.1.0...v1.0.1
29+
[1.0.1]: https://github.com/andstor/copycat-action/compare/v1.0.1...v1.0.1
30+
[1.0.1]: https://github.com/andstor/copycat-action/compare/v1.0.0...v1.0.1

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM alpine:3.10
22

3+
RUN apk add --no-cache bash
34
RUN apk add --no-cache git
45

56
COPY entrypoint.sh /entrypoint.sh
6-
ENTRYPOINT ["/entrypoint.sh"]
7+
ENTRYPOINT ["/bin/bash", "-c", "/entrypoint.sh"]

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The following example [workflow step](https://help.github.com/en/actions/configu
1515

1616
```yml
1717
- name: Copy
18-
uses: andstor/copycat-action@v2
18+
uses: andstor/copycat-action@v3
1919
with:
2020
personal_token: ${{ secrets.PERSONAL_TOKEN }}
2121
src_path: /.
@@ -30,22 +30,38 @@ The following input variable options can/must be configured:
3030
3131
|Input variable|Necessity|Description|Default|
3232
|--------------------|--------|-----------|-------|
33-
|`src_path`|Required|The source path to the file(s) or folder(s) to copy from. For example, `/.` or `path/to/home.md`.||
34-
|`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`|
35-
|`dst_owner`|Required|The name of the owner of the repository to push to. For example, `andstor`.||
36-
|`dst_repo_name`|Required|The name of the repository to push to. For example, `copycat-action`.||
33+
|`src_path`|Required|The source path to the file(s) or folder(s) to copy from. For example `/.` or `path/to/home.md`.||
34+
|`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`|
35+
|`dst_owner`|Required|The name of the owner of the repository to push to. For example `andstor`.||
36+
|`dst_repo_name`|Required|The name of the repository to push to. For example `copycat-action`.||
3737
|`src_branch`|Optional|The branch name of the source repository.|`master`|
3838
|`dst_branch`|Optional|The branch name of the destination repository.|`master`|
39-
|`src_filter`|Optional|A pattern for filtering files to be copied. For example `*.sh`||
39+
|`clean`|Optional|Set to `true` if the `dst_path` should be emptied before copying.|`false`|
40+
|`file_filter`|Optional|A simple [pattern](https://www.gnu.org/software/findutils/manual/html_mono/find.html#Shell-Pattern-Matching) for filtering files to be copied. Acts on file basename. For example `*.sh`.||
41+
|`filter`|Optional|A glob pattern for filtering files to be copied. Acts on file paths. For example `**/!(*.*)`.||
42+
|`exclude`|Optional|A glob pattern for excluding paths. For example `*/tests/*`.||
4043
|`src_wiki`|Optional|Set to `true` if the source repository you want to copy from is the GitHub Wiki.| `false`|
4144
|`dst_wiki`|Optional|Set to `true` if the destination repository you want to copy from is the GitHub Wiki.|`false`|
4245
|`username`|Optional|The GitHub username to associate commits made by this GitHub action.|[`GITHUB_ACTOR`](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables)|
43-
|`email`|Optional|The email used for associating commits made by this GitHub action.|[`GITHUB_ACTOR`](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables)`@users.noreply.github.com`|
46+
|`email`|Optional|The email used for associating commits made by this GitHub action.|[`GITHUB_ACTOR`](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables) `@users.noreply.github.com`|
4447

4548
## Secrets
4649

4750
* `personal_token`: (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).
4851

52+
## Filtering
53+
Copycat provides several ways of filtering which files you want to copy.
54+
All three types of filtering can be applied simultaneously.
55+
56+
### Input variables
57+
#### `file_filter`
58+
The `file_filter`input variable allows you to filter file basenames. Hence, you can for example only copy all text files by setting `file_filter`to `*.txt`. The variable only accepts simple [patterns](https://www.gnu.org/software/findutils/manual/html_mono/find.html#Shell-Pattern-Matching).
59+
60+
#### `filter`
61+
The `filter` input variable provides extensive globbing support. It also supports extended globbing and globstar. The globbing applies to file paths. So, to for example match all files that doesn't have a file extention, the pattern could look like `**/!(*.*)`.
62+
63+
#### `exclude`
64+
The `exclude` input variable can be used to exclude certain paths. It will apply to the file paths. One are for example able to exclude certain deirectory names. Setting `exclude` to `*/tests/*` results in only copying files that don't lie inside a folder named `tests` (both directly and indirectly). Here, a file with for example the path `foo/tests/bar/baz.txt` would not be copied over.
4965

5066
## Examples
5167

@@ -63,7 +79,7 @@ jobs:
6379
runs-on: ubuntu-latest
6480
steps:
6581
- name: Copycat
66-
uses: andstor/copycat-action@v2
82+
uses: andstor/copycat-action@v3
6783
with:
6884
personal_token: ${{ secrets.PERSONAL_TOKEN }}
6985
src_path: /.

action.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ inputs:
2828
description: 'The branch name of the destination repository'
2929
required: false
3030
default: 'master'
31-
src_filter:
32-
description: 'A pattern for filtering files to be copied'
31+
clean:
32+
description: 'Set to true if the dst_path should be emptied before copying'
33+
default: false
34+
required: false
35+
file_filter:
36+
description: 'A glob pattern for filtering file names'
37+
required: false
38+
filter:
39+
description: 'A glob pattern for filtering file paths to be included for copying'
40+
required: false
41+
exclude:
42+
description: 'A glob pattern for excluding paths'
3343
required: false
3444
src_wiki:
3545
description: 'Set to true if the source repository you want to copy from is the GitHub Wiki'
@@ -56,7 +66,10 @@ runs:
5666
- ${{ inputs.dst_repo_name }}
5767
- ${{ inputs.src_branch }}
5868
- ${{ inputs.dst_branch }}
59-
- ${{ inputs.src_filter }}
69+
- ${{ inputs.clean }}
70+
- ${{ inputs.file_filter }}
71+
- ${{ inputs.filter }}
72+
- ${{ inputs.exclude }}
6073
- ${{ inputs.src_wiki }}
6174
- ${{ inputs.dst_wiki }}
6275
- ${{ inputs.username }}

entrypoint.sh

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
#
33
# @author André Storhaug <andr3.storhaug@gmail.com>
4-
# @date 2020-02-22
4+
# @date 2020-03-09
55
# @license MIT
6-
# @version 2.0.0
6+
# @version 3.0.0
77

88
set -o pipefail
99

10+
shopt -s extglob globstar nullglob dotglob
11+
1012
PERSONAL_TOKEN="$INPUT_PERSONAL_TOKEN"
1113
SRC_PATH="$INPUT_SRC_PATH"
1214
DST_PATH="$INPUT_DST_PATH"
1315
DST_OWNER="$INPUT_DST_OWNER"
1416
DST_REPO_NAME="$INPUT_DST_REPO_NAME"
1517
SRC_BRANCH="$INPUT_SRC_BRANCH"
1618
DST_BRANCH="$INPUT_DST_BRANCH"
19+
CLEAN="$INPUT_CLEAN"
20+
FILE_FILTER="$INPUT_FILE_FILTER"
21+
FILTER="$INPUT_FILTER"
22+
EXCLUDE="$INPUT_EXCLUDE"
1723
SRC_WIKI="$INPUT_SRC_WIKI"
1824
DST_WIKI="$INPUT_DST_WIKI"
1925
USERNAME="$INPUT_USERNAME"
2026
EMAIL="$INPUT_EMAIL"
2127

2228
if [[ -z "$SRC_PATH" ]]; then
23-
echo "SRC_PATH environment variable is missing. Cannot proceed."
24-
exit 1
29+
echo "SRC_PATH environment variable is missing. Cannot proceed."
30+
exit 1
2531
fi
2632

2733
if [[ -z "$DST_OWNER" ]]; then
28-
echo "DST_OWNER environment variable is missing. Cannot proceed."
29-
exit 1
34+
echo "DST_OWNER environment variable is missing. Cannot proceed."
35+
exit 1
3036
fi
3137

3238
if [[ -z "$DST_REPO_NAME" ]]; then
33-
echo "DST_REPO_NAME environment variable is missing. Cannot proceed."
34-
exit 1
39+
echo "DST_REPO_NAME environment variable is missing. Cannot proceed."
40+
exit 1
3541
fi
3642

3743
if [ "$SRC_WIKI" = "true" ]; then
@@ -46,6 +52,10 @@ else
4652
DST_WIKI=""
4753
fi
4854

55+
if [[ -n "$EXCLUDE" && -z "$FILTER" ]]; then
56+
FILTER="**"
57+
fi
58+
4959
BASE_PATH=$(pwd)
5060
DST_PATH="${DST_PATH:-${SRC_PATH}}"
5161

@@ -60,15 +70,15 @@ SRC_REPO_NAME="${GITHUB_REPOSITORY#*/}${SRC_WIKI}"
6070
DST_REPO="${DST_OWNER}/${DST_REPO_NAME}${DST_WIKI}"
6171
DST_REPO_NAME="${DST_REPO_NAME}${DST_WIKI}"
6272

63-
DIR="${DST_PATH%/*}"
73+
FINAL_SOURCE="${SRC_REPO_NAME}/${SRC_PATH}"
6474

6575
git config --global user.name "${USERNAME}"
6676
git config --global user.email "${EMAIL}"
6777

68-
if [[ -z "$SRC_FILTER" ]]; then
78+
if [[ -z "$FILE_FILTER" ]]; then
6979
echo "Copying \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}"
7080
else
71-
echo "Copying files matching \"${SRC_FILTER}\" from \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}"
81+
echo "Copying files matching \"${FILE_FILTER}\" from \"${SRC_REPO_NAME}/${SRC_PATH}\" and pushing it to ${GITHUB_REPOSITORY}"
7282
fi
7383

7484
git clone --branch ${SRC_BRANCH} --single-branch --depth 1 https://${PERSONAL_TOKEN}@github.com/${SRC_REPO}.git
@@ -78,8 +88,25 @@ if [ "$?" -ne 0 ]; then
7888
fi
7989
rm -rf ${SRC_REPO_NAME}/.git
8090

81-
if [[ -n "$SRC_FILTER" ]]; then
82-
find ${SRC_REPO_NAME}/ -type f -not -name "${SRC_FILTER}" -exec rm {} \;
91+
if [[ -n "$FILE_FILTER" ]]; then
92+
find ${SRC_REPO_NAME}/ -type f -not -name "${FILE_FILTER}" -exec rm {} \;
93+
fi
94+
95+
if [[ -n "$FILTER" ]]; then
96+
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
97+
mkdir ${temp_dir}/${SRC_REPO_NAME}
98+
cd ${SRC_REPO_NAME}
99+
FINAL_SOURCE="${tmp_dir}/${SRC_REPO_NAME}/${SRC_PATH}"
100+
for f in ${FILTER} ; do
101+
[ -e "$f" ] || continue
102+
[ -d "$f" ] && continue
103+
if [[ -n "$EXCLUDE" ]] ; then
104+
[[ $f == $EXCLUDE ]] && continue
105+
fi
106+
file_dir=$(dirname "${f}")
107+
mkdir -p ${tmp_dir}/${SRC_REPO_NAME}/${file_dir} && cp ${f} ${tmp_dir}/${SRC_REPO_NAME}/${file_dir}
108+
done
109+
cd ..
83110
fi
84111

85112
git clone --branch ${DST_BRANCH} --single-branch --depth 1 https://${PERSONAL_TOKEN}@github.com/${DST_REPO}.git
@@ -88,14 +115,24 @@ if [ "$?" -ne 0 ]; then
88115
exit 1
89116
fi
90117

91-
mkdir -p ${DST_REPO_NAME}/${DIR} || exit "$?"
92-
cp -rf ${SRC_REPO_NAME}/${SRC_PATH} ${DST_REPO_NAME}/${DST_PATH} || exit "$?"
118+
if [ "$CLEAN" = "true" ]; then
119+
if [ -f "${DST_REPO_NAME}/${DST_PATH}" ] ; then
120+
find ${DST_REPO_NAME}/${DST_PATH} -type f -not -path '*/\.git/*' -delete
121+
elif [ -d "${DST_REPO_NAME}/${DST_PATH}" ] ; then
122+
find ${DST_REPO_NAME}/${DST_PATH%/*}/* -type f -not -path '*/\.git/*' -delete
123+
else
124+
echo >&2 "Nothing to clean 🧽"
125+
fi
126+
fi
127+
128+
mkdir -p ${DST_REPO_NAME}/${DST_PATH%/*} || exit "$?"
129+
cp -rf ${FINAL_SOURCE} ${DST_REPO_NAME}/${DST_PATH} || exit "$?"
93130
cd ${DST_REPO_NAME} || exit "$?"
94131

95-
if [ -d "${BASE_PATH}/${SRC_REPO_NAME}/${SRC_PATH}" ]; then
96-
COMMIT_MESSAGE="Update file(s) in \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\""
132+
if [ -f "${BASE_PATH}/${FINAL_SOURCE}" ]; then
133+
COMMIT_MESSAGE="Update file in \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\""
97134
else
98-
COMMIT_MESSAGE="Update file \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\""
135+
COMMIT_MESSAGE="Update file(s) \"${SRC_PATH}\" from \"${GITHUB_REPOSITORY}\""
99136
fi
100137

101138
if [ -z "$(git status --porcelain)" ]; then

0 commit comments

Comments
 (0)