Skip to content

Commit 6bb17e6

Browse files
authored
Merge pull request #2 from meese-enterprises/add-bash-to-docker
2 parents 74ef32e + 271806f commit 6bb17e6

File tree

4 files changed

+72
-55
lines changed

4 files changed

+72
-55
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM alpine:latest
22

3-
RUN apk add --no-cache git openssh-client
3+
RUN apk add --no-cache bash git openssh-client
44

55
COPY entrypoint.sh /entrypoint.sh
66

README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ There are different variables to set up the behaviour:
1818
### `source-directories` (argument)
1919
From the repository that this Git Action is executed the directories that contains the files to be pushed into the repository.
2020

21+
### `destination-directory-prefixes` (argument) [optional]
22+
If you have multiple directories in the source repository, you can specify the prefixes **in order** that each folder will be copied into in the destination folder.
23+
2124
### `destination-github-username` (argument)
2225
For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`.
2326

@@ -58,8 +61,6 @@ There are two options to do this:
5861

5962
Someone with write access to your repository or this action, could technically add code to leak the key. Thus, *it is recommended to use the SSH deploy key method to minimise repercusions* if this was the case.
6063

61-
This action supports both methods to keep backwards compatibility, because in the beginning it only supported the GitHub Personal Authentication token.
62-
6364
## Setup with SSH deploy key
6465
### Generate the key files
6566

@@ -106,26 +107,23 @@ Then make the token available to the Github Action following the steps:
106107

107108
## Example usage
108109
```yaml
109-
- name: Pushes to another repository
110-
uses: cpina/github-action-push-to-another-repository@main
111-
env:
112-
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
113-
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
114-
with:
115-
source-directories: ['subrepo1/output', 'subrepo2/output']
116-
destination-github-username: 'cpina'
117-
destination-repository-name: 'pandoc-test-output'
118-
user-email: [email protected]
119-
target-branch: main
110+
- name: Pushes to another repository
111+
uses: meese-enterprises/github-action-push-to-another-repository@main
112+
env:
113+
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
114+
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
115+
with:
116+
source-directories: |
117+
frontend/client/doc
118+
frontend/dialogs/doc
119+
destination-directory-prefixes: |
120+
client
121+
dialogs
122+
target-directory: src/api
123+
destination-github-username: fake-username
124+
destination-repository-name: fake-repository
125+
user-email: [email protected]
126+
target-branch: main
120127
```
121-
(you only need `SSH_DEPLOY_KEY` or `API_TOKEN_GITHUB` depending on the method that you used)
122-
123-
Working example:
124-
125-
https://github.com/cpina/push-to-another-repository-deploy-keys-example/blob/main/.github/workflows/ci.yml
126-
127-
It generates files from:
128-
https://github.com/cpina/push-to-another-repository-deploy-keys-example
129128
130-
To:
131-
https://github.com/cpina/push-to-another-repository-output
129+
(you only need `SSH_DEPLOY_KEY` or `API_TOKEN_GITHUB`, depending on the method that you used)

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ inputs:
66
source-directories:
77
description: Source directories from the origin
88
required: true
9+
destination-directory-prefixes:
10+
description: >-
11+
[Optional] Destination directory prefixes, paired in order with the source directories
12+
default: ""
13+
required: false
914
destination-github-username:
1015
description: Name of the destination username/organization
1116
required: true
@@ -55,6 +60,7 @@ runs:
5560
image: Dockerfile
5661
args:
5762
- "${{ inputs.source-directories }}"
63+
- "${{ inputs.destination-directory-prefixes }}"
5864
- "${{ inputs.destination-github-username }}"
5965
- "${{ inputs.destination-repository-name }}"
6066
- "${{ inputs.github-server }}"

entrypoint.sh

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
#!/bin/sh -l
1+
#!/bin/bash -l
22

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

66
echo ""
77
echo "[+] Action start"
88
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}"
1920

2021
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
2122
then
@@ -55,7 +56,6 @@ else
5556
exit 1
5657
fi
5758

58-
5959
CLONE_DIR=$(mktemp -d)
6060

6161
echo "[+] Git version"
@@ -107,35 +107,48 @@ mkdir -p "$ABSOLUTE_TARGET_DIRECTORY"
107107

108108
mv "$TEMP_DIR/.git" "$CLONE_DIR/.git"
109109

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
120116
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]}"
123141

124142
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"
127145
done
128146

129147
cd "$CLONE_DIR"
130148
echo ""
131149
echo "[+] List of files that will be pushed:"
132150
ls -la
133151

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-
139152
ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
140153
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
141154
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"

0 commit comments

Comments
 (0)