|
1 | | -#!/bin/sh -l |
| 1 | +#!/bin/bash -l |
2 | 2 |
|
3 | 3 | set -e # if a command fails it stops the execution |
4 | 4 | set -u # script fails if trying to access to an undefined variable |
5 | 5 |
|
6 | 6 | echo "" |
7 | 7 | echo "[+] Action start" |
8 | 8 | SOURCE_DIRECTORIES="${1}" |
9 | | -DESTINATION_GITHUB_USERNAME="${2}" |
10 | | -DESTINATION_REPOSITORY_NAME="${3}" |
11 | | -GITHUB_SERVER="${4}" |
12 | | -USER_EMAIL="${5}" |
13 | | -USER_NAME="${6}" |
14 | | -DESTINATION_REPOSITORY_USERNAME="${7}" |
15 | | -TARGET_BRANCH="${8}" |
16 | | -COMMIT_MESSAGE="${9}" |
17 | | -TARGET_DIRECTORY="${10}" |
18 | | -FORCE="${11}" |
| 9 | +DESTINATION_DIRECTORY_PREFIXES="${2}" |
| 10 | +DESTINATION_GITHUB_USERNAME="${3}" |
| 11 | +DESTINATION_REPOSITORY_NAME="${4}" |
| 12 | +GITHUB_SERVER="${5}" |
| 13 | +USER_EMAIL="${6}" |
| 14 | +USER_NAME="${7}" |
| 15 | +DESTINATION_REPOSITORY_USERNAME="${8}" |
| 16 | +TARGET_BRANCH="${9}" |
| 17 | +COMMIT_MESSAGE="${10}" |
| 18 | +TARGET_DIRECTORY="${11}" |
| 19 | +FORCE="${12}" |
19 | 20 |
|
20 | 21 | if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] |
21 | 22 | then |
|
55 | 56 | exit 1 |
56 | 57 | fi |
57 | 58 |
|
58 | | - |
59 | 59 | CLONE_DIR=$(mktemp -d) |
60 | 60 |
|
61 | 61 | echo "[+] Git version" |
@@ -107,35 +107,48 @@ mkdir -p "$ABSOLUTE_TARGET_DIRECTORY" |
107 | 107 |
|
108 | 108 | mv "$TEMP_DIR/.git" "$CLONE_DIR/.git" |
109 | 109 |
|
110 | | -# Loop over all the directories and copy them to the destination |
111 | | -for SOURCE_DIRECTORY in $SOURCE_DIRECTORIES |
112 | | -do |
113 | | - if [ ! -d "$SOURCE_DIRECTORY" ] |
114 | | - then |
115 | | - echo "" |
116 | | - echo "[+] Source directory $SOURCE_DIRECTORY does not exist, skipping" |
117 | | - continue |
118 | | - fi |
119 | | - |
| 110 | +# https://stackoverflow.com/a/42399738/6456163 |
| 111 | +set +e |
| 112 | +NUM_SOURCE_DIRS=$(echo -n "$SOURCE_DIRECTORIES" | grep -c '^') |
| 113 | +NUM_DEST_PREFIXES=$(echo -n "$DESTINATION_DIRECTORY_PREFIXES" | grep -c '^') |
| 114 | +if [ "$NUM_DEST_PREFIXES" -ne 0 ] && [ "$NUM_SOURCE_DIRS" -ne "$NUM_DEST_PREFIXES" ] |
| 115 | +then |
120 | 116 | echo "" |
121 | | - echo "[+] List contents of $SOURCE_DIRECTORY:" |
122 | | - ls -la "$SOURCE_DIRECTORY" |
| 117 | + echo "[-] The number of source directories ($NUM_SOURCE_DIRS) is different from the number of destination directory prefixes ($NUM_DEST_PREFIXES)" |
| 118 | + exit 1 |
| 119 | +fi |
| 120 | +set -e |
| 121 | + |
| 122 | +# Parses the passed strings to arrays |
| 123 | +# https://stackoverflow.com/a/5257398/6456163 |
| 124 | +SOURCE_DIRECTORIES_ARRAY=(${SOURCE_DIRECTORIES//\\n/ }) |
| 125 | + |
| 126 | +if [ -n "${DESTINATION_DIRECTORY_PREFIXES:=}" ] |
| 127 | +then |
| 128 | + DESTINATION_DIRECTORY_PREFIXES_ARRAY=(${DESTINATION_DIRECTORY_PREFIXES//\\n/ }) |
| 129 | +else |
| 130 | + # Populate an array of the correct length with empty strings |
| 131 | + for i in $(seq "$NUM_SOURCE_DIRS"); do |
| 132 | + DESTINATION_DIRECTORY_PREFIXES_ARRAY[$i]="" |
| 133 | + done |
| 134 | +fi |
| 135 | + |
| 136 | +# Loop over all the directories and copy them to the right destination |
| 137 | +for i in $(seq "$NUM_SOURCE_DIRS"); do |
| 138 | + i=$((i-1)) |
| 139 | + SOURCE_DIRECTORY="${SOURCE_DIRECTORIES_ARRAY[$i]}" |
| 140 | + DESTINATION_DIRECTORY_PREFIX="${DESTINATION_DIRECTORY_PREFIXES_ARRAY[$i]}" |
123 | 141 |
|
124 | 142 | echo "" |
125 | | - echo "[+] Copying contents of source repository folder '$SOURCE_DIRECTORY' to git repo '$DESTINATION_REPOSITORY_NAME'" |
126 | | - cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY" |
| 143 | + echo "[+] Copying $SOURCE_DIRECTORY to $ABSOLUTE_TARGET_DIRECTORY$DESTINATION_DIRECTORY_PREFIX" |
| 144 | + cp -r "$SOURCE_DIRECTORY" "$ABSOLUTE_TARGET_DIRECTORY$DESTINATION_DIRECTORY_PREFIX" |
127 | 145 | done |
128 | 146 |
|
129 | 147 | cd "$CLONE_DIR" |
130 | 148 | echo "" |
131 | 149 | echo "[+] List of files that will be pushed:" |
132 | 150 | ls -la |
133 | 151 |
|
134 | | -# Used for local testing, when ran via `./entrypoint.sh [...]` |
135 | | -# GITHUB_REPOSITORY="$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME" |
136 | | -# GITHUB_SHA="$(git rev-parse HEAD)" |
137 | | -# GITHUB_REF="refs/heads/$TARGET_BRANCH" |
138 | | - |
139 | 152 | ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" |
140 | 153 | COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}" |
141 | 154 | COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}" |
|
0 commit comments