Skip to content

Commit 74ef32e

Browse files
authored
Merge pull request #1 from meese-enterprises/multiple-source-directories
2 parents d613851 + e21d565 commit 74ef32e

File tree

3 files changed

+99
-94
lines changed

3 files changed

+99
-94
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ There are two example repositories:
88
* [Repository 1](https://github.com/cpina/push-to-another-repository-deploy-keys-example): using SSH deploy keys (recommended)
99
* [Repository 2](https://github.com/cpina/push-to-another-repository-example): using a personal access token setup
1010

11-
On a push of the repositories (thanks to the file [.github/workflows/ci.yml](https://github.com/cpina/push-to-another-repository-deploy-keys-example/tree/main/.github/workflows) it uses Pandoc to read the MarkDown file [main.md](https://github.com/cpina/push-to-another-repository-deploy-ssh-example/blob/main/main.md) (via [this step](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19) and the example [build.sh](https://github.com/cpina/push-to-another-repository-deploy-keys-example/blob/main/build.sh). build.sh generates the output/ directory configurable via [source-directory](https://github.com/cpina/push-to-another-repository-deploy-keys-example/blob/main/.github/workflows/ci.yml#L27) appears in the [output repository](https://github.com/cpina/push-to-another-repository-output).
11+
On a push of the repositories (thanks to the file [.github/workflows/ci.yml](https://github.com/cpina/push-to-another-repository-deploy-keys-example/tree/main/.github/workflows) it uses Pandoc to read the MarkDown file [main.md](https://github.com/cpina/push-to-another-repository-deploy-ssh-example/blob/main/main.md) (via [this step](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19) and the example [build.sh](https://github.com/cpina/push-to-another-repository-deploy-keys-example/blob/main/build.sh). build.sh generates the output/ directory configurable via [source-directories](https://github.com/meese-enterprises/push-to-another-repository-deploy-keys-example/blob/main/.github/workflows/ci.yml#L27) appears in the [output repository](https://github.com/cpina/push-to-another-repository-output).
1212

1313
Please bear in mind: files in the target repository's specified directory are deleted. This is to make sure that it contains only the files generated on the last run.
1414

1515
There are different variables to set up the behaviour:
1616

1717
## Inputs
18-
### `source-directory` (argument)
19-
From the repository that this Git Action is executed the directory that contains the files to be pushed into the repository.
18+
### `source-directories` (argument)
19+
From the repository that this Git Action is executed the directories that contains the files to be pushed into the repository.
2020

2121
### `destination-github-username` (argument)
2222
For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`.
@@ -112,7 +112,7 @@ Then make the token available to the Github Action following the steps:
112112
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
113113
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
114114
with:
115-
source-directory: 'output'
115+
source-directories: ['subrepo1/output', 'subrepo2/output']
116116
destination-github-username: 'cpina'
117117
destination-repository-name: 'pandoc-test-output'
118118
user-email: [email protected]

action.yml

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
name: Push directory to another repository
1+
name: Push directories to another repository
22
description: >-
3-
Useful to push files to another repository to be used, for example, via github
4-
pages
3+
Useful to push files to another repository to be used, for example, via GitHub
4+
Pages
55
inputs:
6-
source-before-directory:
7-
description: Source before directory from the origin directory
8-
required: false
9-
source-directory:
10-
description: Source directory from the origin directory
6+
source-directories:
7+
description: Source directories from the origin
118
required: true
129
destination-github-username:
1310
description: Name of the destination username/organization
@@ -19,19 +16,19 @@ inputs:
1916
description: Email for the git commit
2017
required: true
2118
github-server:
22-
description: 'Github server'
23-
default: 'github.com'
19+
description: "Github server"
20+
default: "github.com"
2421
required: false
2522
user-name:
2623
description: >-
2724
[Optional] Name for the git commit. Defaults to the destination
2825
username/organization name
2926
required: false
30-
default: ''
27+
default: ""
3128
destination-repository-username:
32-
description: '[Optional] Username/organization for the destination repository'
29+
description: "[Optional] Username/organization for the destination repository"
3330
required: false
34-
default: ''
31+
default: ""
3532
target-branch:
3633
description: >-
3734
[Optional] set target branch name for the destination repository. Defaults
@@ -45,30 +42,29 @@ inputs:
4542
default: Update from ORIGIN_COMMIT
4643
required: false
4744
target-directory:
48-
description: '[Optional] The directory to wipe and replace in the target repository'
49-
default: ''
45+
description: "[Optional] The directory to wipe and replace in the target repository"
46+
default: ""
5047
required: false
5148
force:
5249
description: "[Optional] If 'true', will force push and thus overwrite the target repo's history"
53-
default: 'false'
50+
default: "false"
5451
required: false
5552

5653
runs:
5754
using: docker
5855
image: Dockerfile
5956
args:
60-
- '${{ inputs.source-before-directory }}'
61-
- '${{ inputs.source-directory }}'
62-
- '${{ inputs.destination-github-username }}'
63-
- '${{ inputs.destination-repository-name }}'
64-
- '${{ inputs.github-server }}'
65-
- '${{ inputs.user-email }}'
66-
- '${{ inputs.user-name }}'
67-
- '${{ inputs.destination-repository-username }}'
68-
- '${{ inputs.target-branch }}'
69-
- '${{ inputs.commit-message }}'
70-
- '${{ inputs.target-directory }}'
71-
- '${{ inputs.force }}'
57+
- "${{ inputs.source-directories }}"
58+
- "${{ inputs.destination-github-username }}"
59+
- "${{ inputs.destination-repository-name }}"
60+
- "${{ inputs.github-server }}"
61+
- "${{ inputs.user-email }}"
62+
- "${{ inputs.user-name }}"
63+
- "${{ inputs.destination-repository-username }}"
64+
- "${{ inputs.target-branch }}"
65+
- "${{ inputs.commit-message }}"
66+
- "${{ inputs.target-directory }}"
67+
- "${{ inputs.force }}"
7268
branding:
7369
icon: git-commit
7470
color: green

entrypoint.sh

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
set -e # if a command fails it stops the execution
44
set -u # script fails if trying to access to an undefined variable
55

6+
echo ""
67
echo "[+] Action start"
7-
SOURCE_BEFORE_DIRECTORY="${1}"
8-
SOURCE_DIRECTORY="${2}"
9-
DESTINATION_GITHUB_USERNAME="${3}"
10-
DESTINATION_REPOSITORY_NAME="${4}"
11-
GITHUB_SERVER="${5}"
12-
USER_EMAIL="${6}"
13-
USER_NAME="${7}"
14-
DESTINATION_REPOSITORY_USERNAME="${8}"
15-
TARGET_BRANCH="${9}"
16-
COMMIT_MESSAGE="${10}"
17-
TARGET_DIRECTORY="${11}"
18-
FORCE="${12}"
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}"
1919

2020
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
2121
then
@@ -27,13 +27,14 @@ then
2727
USER_NAME="$DESTINATION_GITHUB_USERNAME"
2828
fi
2929

30+
DESTINATION_REPOSITORY="$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME"
3031
TARGET_BRANCH_EXISTS=true
3132

3233
# Verify that there (potentially) some access to the destination repository
3334
# and set up git (with GIT_CMD variable) and GIT_CMD_REPOSITORY
3435
if [ -n "${SSH_DEPLOY_KEY:=}" ]
3536
then
36-
# Inspired by https://github.com/leigholiver/commit-with-deploy-key/blob/main/entrypoint.sh , thanks!
37+
# Inspired by https://github.com/leigholiver/commit-with-deploy-key/blob/main/entrypoint.sh, thanks!
3738
mkdir --parents "$HOME/.ssh"
3839
DEPLOY_KEY_FILE="$HOME/.ssh/deploy_key"
3940
echo "${SSH_DEPLOY_KEY}" > "$DEPLOY_KEY_FILE"
@@ -44,13 +45,13 @@ then
4445

4546
export GIT_SSH_COMMAND="ssh -i "$DEPLOY_KEY_FILE" -o UserKnownHostsFile=$SSH_KNOWN_HOSTS_FILE"
4647

47-
GIT_CMD_REPOSITORY="git@$GITHUB_SERVER:$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git"
48+
GIT_CMD_REPOSITORY="git@$GITHUB_SERVER:$DESTINATION_REPOSITORY.git"
4849

4950
elif [ -n "${API_TOKEN_GITHUB:=}" ]
5051
then
51-
GIT_CMD_REPOSITORY="https://$DESTINATION_REPOSITORY_USERNAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git"
52+
GIT_CMD_REPOSITORY="https://$DESTINATION_REPOSITORY_USERNAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY.git"
5253
else
53-
echo "::error::API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one (recommended the SSH_DEPLOY_KEY"
54+
echo "[-] API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one in!"
5455
exit 1
5556
fi
5657

@@ -60,27 +61,31 @@ CLONE_DIR=$(mktemp -d)
6061
echo "[+] Git version"
6162
git --version
6263

63-
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
6464
# Setup git
6565
git config --global user.email "$USER_EMAIL"
6666
git config --global user.name "$USER_NAME"
6767

68+
echo ""
69+
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
6870
{
6971
git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR"
7072
} || {
7173
{
72-
echo "Target branch doesn't exist, fetching main branch"
73-
git clone --single-branch "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
74+
echo "Target branch doesn't exist, fetching the 'main' branch"
75+
git clone --single-branch "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY.git" "$CLONE_DIR"
7476
TARGET_BRANCH_EXISTS=false
7577
} || {
76-
echo "::error::Could not clone the destination repository. Command:"
77-
echo "::error::git clone --single-branch --depth 1 --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR"
78-
echo "::error::(Note that if they exist USER_NAME and API_TOKEN is redacted by GitHub)"
79-
echo "::error::Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB OR SSH_DEPLOY_KEY"
78+
echo ""
79+
echo "[-] Could not clone the destination repository. Command:"
80+
echo "[-] git clone --single-branch --depth 1 --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR"
81+
echo "[-] (Note that if they exist, USER_NAME and API_TOKEN are redacted by GitHub)"
82+
echo "[-] Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB OR SSH_DEPLOY_KEY"
8083
exit 1
8184
}
8285
}
8386

87+
echo ""
88+
echo "[+] Listing the contents of the clone directory:"
8489
ls -la "$CLONE_DIR"
8590

8691
TEMP_DIR=$(mktemp -d)
@@ -92,76 +97,80 @@ mv "$CLONE_DIR/.git" "$TEMP_DIR/.git"
9297
# $TARGET_DIRECTORY is '' by default
9398
ABSOLUTE_TARGET_DIRECTORY="$CLONE_DIR/$TARGET_DIRECTORY/"
9499

100+
echo ""
95101
echo "[+] Deleting $ABSOLUTE_TARGET_DIRECTORY"
96102
rm -rf "$ABSOLUTE_TARGET_DIRECTORY"
97103

104+
echo ""
98105
echo "[+] Creating (now empty) $ABSOLUTE_TARGET_DIRECTORY"
99106
mkdir -p "$ABSOLUTE_TARGET_DIRECTORY"
100107

101-
echo "[+] Listing Current Directory Location"
102-
ls -al
103-
104-
echo "[+] Listing root Location"
105-
ls -al /
106-
107108
mv "$TEMP_DIR/.git" "$CLONE_DIR/.git"
108109

109-
echo "[+] List contents of $SOURCE_DIRECTORY"
110-
ls "$SOURCE_DIRECTORY"
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+
120+
echo ""
121+
echo "[+] List contents of $SOURCE_DIRECTORY:"
122+
ls -la "$SOURCE_DIRECTORY"
123+
124+
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"
127+
done
111128

112-
echo "[+] Checking if local $SOURCE_DIRECTORY exist"
113-
if [ ! -d "$SOURCE_DIRECTORY" ]
114-
then
115-
echo "ERROR: $SOURCE_DIRECTORY does not exist"
116-
echo "This directory needs to exist when push-to-another-repository is executed"
117-
echo
118-
echo "In the example it is created by ./build.sh: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19"
119-
echo
120-
echo "If you want to copy a directory that exist in the source repository"
121-
echo "to the target repository: you need to clone the source repository"
122-
echo "in a previous step in the same build section. For example using"
123-
echo "actions/checkout@v2. See: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L16"
124-
exit 1
125-
fi
126-
127-
echo "[+] Copying contents of source repository folder $SOURCE_DIRECTORY to folder $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME"
128-
cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY"
129129
cd "$CLONE_DIR"
130-
131-
echo "[+] Files that will be pushed"
130+
echo ""
131+
echo "[+] List of files that will be pushed:"
132132
ls -la
133133

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+
134139
ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
135140
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
136141
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
137142

138-
if [ "$TARGET_BRANCH_EXISTS" = false ] ; then
143+
if [ "$TARGET_BRANCH_EXISTS" = false ]; then
144+
echo ""
139145
echo "Creating branch $TARGET_BRANCH"
140146
git checkout -b "$TARGET_BRANCH"
141147
fi
142148

143-
echo "[+] Set directory is safe ($CLONE_DIR)"
144-
# Related to https://github.com/cpina/github-action-push-to-another-repository/issues/64 and https://github.com/cpina/github-action-push-to-another-repository/issues/64
145-
# TODO: review before releasing it as a version
149+
# Related to https://github.com/cpina/github-action-push-to-another-repository/issues/64
150+
# and https://github.com/cpina/github-action-push-to-another-repository/issues/64
151+
echo ""
152+
echo "[+] Set directory as safe ($CLONE_DIR)"
146153
git config --global --add safe.directory "$CLONE_DIR"
147154

155+
echo ""
148156
echo "[+] Adding git commit"
149157
git add .
150158

159+
echo ""
151160
echo "[+] git status:"
152161
git status
153162

163+
# git diff-index: avoids the git commit failing if there are no changes
164+
echo ""
154165
echo "[+] git diff-index:"
155-
# git diff-index : to avoid doing the git commit failing if there are no changes to be commit
156166
git diff-index --quiet HEAD || git commit --message "$COMMIT_MESSAGE"
157167

158-
if $FORCE; then
159-
echo "[+] Force pushing git commit"
160-
FORCE_FLAG="-f"
168+
# --set-upstream: sets de branch when pushing to a branch that does not exist
169+
echo ""
170+
if [ "$FORCE" = true ]; then
171+
echo "[+] Forcefully pushing git commit"
172+
git push -f "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"
161173
else
162174
echo "[+] Pushing git commit"
163-
FORCE_FLAG=""
175+
git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"
164176
fi
165-
166-
# --set-upstream: sets de branch when pushing to a branch that does not exist
167-
git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH" "$FORCE_FLAG"

0 commit comments

Comments
 (0)