Skip to content

Commit

Permalink
Merge pull request #2 from meese-enterprises/add-bash-to-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 authored Jul 9, 2022
2 parents 74ef32e + 271806f commit 6bb17e6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:latest

RUN apk add --no-cache git openssh-client
RUN apk add --no-cache bash git openssh-client

COPY entrypoint.sh /entrypoint.sh

Expand Down
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ There are different variables to set up the behaviour:
### `source-directories` (argument)
From the repository that this Git Action is executed the directories that contains the files to be pushed into the repository.

### `destination-directory-prefixes` (argument) [optional]
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.

### `destination-github-username` (argument)
For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`.

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

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.

This action supports both methods to keep backwards compatibility, because in the beginning it only supported the GitHub Personal Authentication token.

## Setup with SSH deploy key
### Generate the key files

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

## Example usage
```yaml
- name: Pushes to another repository
uses: cpina/github-action-push-to-another-repository@main
env:
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
with:
source-directories: ['subrepo1/output', 'subrepo2/output']
destination-github-username: 'cpina'
destination-repository-name: 'pandoc-test-output'
user-email: [email protected]
target-branch: main
- name: Pushes to another repository
uses: meese-enterprises/github-action-push-to-another-repository@main
env:
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
with:
source-directories: |
frontend/client/doc
frontend/dialogs/doc
destination-directory-prefixes: |
client
dialogs
target-directory: src/api
destination-github-username: fake-username
destination-repository-name: fake-repository
user-email: [email protected]
target-branch: main
```
(you only need `SSH_DEPLOY_KEY` or `API_TOKEN_GITHUB` depending on the method that you used)

Working example:

https://github.com/cpina/push-to-another-repository-deploy-keys-example/blob/main/.github/workflows/ci.yml

It generates files from:
https://github.com/cpina/push-to-another-repository-deploy-keys-example
To:
https://github.com/cpina/push-to-another-repository-output
(you only need `SSH_DEPLOY_KEY` or `API_TOKEN_GITHUB`, depending on the method that you used)
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ inputs:
source-directories:
description: Source directories from the origin
required: true
destination-directory-prefixes:
description: >-
[Optional] Destination directory prefixes, paired in order with the source directories
default: ""
required: false
destination-github-username:
description: Name of the destination username/organization
required: true
Expand Down Expand Up @@ -55,6 +60,7 @@ runs:
image: Dockerfile
args:
- "${{ inputs.source-directories }}"
- "${{ inputs.destination-directory-prefixes }}"
- "${{ inputs.destination-github-username }}"
- "${{ inputs.destination-repository-name }}"
- "${{ inputs.github-server }}"
Expand Down
75 changes: 44 additions & 31 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/bin/sh -l
#!/bin/bash -l

set -e # if a command fails it stops the execution
set -u # script fails if trying to access to an undefined variable

echo ""
echo "[+] Action start"
SOURCE_DIRECTORIES="${1}"
DESTINATION_GITHUB_USERNAME="${2}"
DESTINATION_REPOSITORY_NAME="${3}"
GITHUB_SERVER="${4}"
USER_EMAIL="${5}"
USER_NAME="${6}"
DESTINATION_REPOSITORY_USERNAME="${7}"
TARGET_BRANCH="${8}"
COMMIT_MESSAGE="${9}"
TARGET_DIRECTORY="${10}"
FORCE="${11}"
DESTINATION_DIRECTORY_PREFIXES="${2}"
DESTINATION_GITHUB_USERNAME="${3}"
DESTINATION_REPOSITORY_NAME="${4}"
GITHUB_SERVER="${5}"
USER_EMAIL="${6}"
USER_NAME="${7}"
DESTINATION_REPOSITORY_USERNAME="${8}"
TARGET_BRANCH="${9}"
COMMIT_MESSAGE="${10}"
TARGET_DIRECTORY="${11}"
FORCE="${12}"

if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
then
Expand Down Expand Up @@ -55,7 +56,6 @@ else
exit 1
fi


CLONE_DIR=$(mktemp -d)

echo "[+] Git version"
Expand Down Expand Up @@ -107,35 +107,48 @@ mkdir -p "$ABSOLUTE_TARGET_DIRECTORY"

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

# Loop over all the directories and copy them to the destination
for SOURCE_DIRECTORY in $SOURCE_DIRECTORIES
do
if [ ! -d "$SOURCE_DIRECTORY" ]
then
echo ""
echo "[+] Source directory $SOURCE_DIRECTORY does not exist, skipping"
continue
fi

# https://stackoverflow.com/a/42399738/6456163
set +e
NUM_SOURCE_DIRS=$(echo -n "$SOURCE_DIRECTORIES" | grep -c '^')
NUM_DEST_PREFIXES=$(echo -n "$DESTINATION_DIRECTORY_PREFIXES" | grep -c '^')
if [ "$NUM_DEST_PREFIXES" -ne 0 ] && [ "$NUM_SOURCE_DIRS" -ne "$NUM_DEST_PREFIXES" ]
then
echo ""
echo "[+] List contents of $SOURCE_DIRECTORY:"
ls -la "$SOURCE_DIRECTORY"
echo "[-] The number of source directories ($NUM_SOURCE_DIRS) is different from the number of destination directory prefixes ($NUM_DEST_PREFIXES)"
exit 1
fi
set -e

# Parses the passed strings to arrays
# https://stackoverflow.com/a/5257398/6456163
SOURCE_DIRECTORIES_ARRAY=(${SOURCE_DIRECTORIES//\\n/ })

if [ -n "${DESTINATION_DIRECTORY_PREFIXES:=}" ]
then
DESTINATION_DIRECTORY_PREFIXES_ARRAY=(${DESTINATION_DIRECTORY_PREFIXES//\\n/ })
else
# Populate an array of the correct length with empty strings
for i in $(seq "$NUM_SOURCE_DIRS"); do
DESTINATION_DIRECTORY_PREFIXES_ARRAY[$i]=""
done
fi

# Loop over all the directories and copy them to the right destination
for i in $(seq "$NUM_SOURCE_DIRS"); do
i=$((i-1))
SOURCE_DIRECTORY="${SOURCE_DIRECTORIES_ARRAY[$i]}"
DESTINATION_DIRECTORY_PREFIX="${DESTINATION_DIRECTORY_PREFIXES_ARRAY[$i]}"

echo ""
echo "[+] Copying contents of source repository folder '$SOURCE_DIRECTORY' to git repo '$DESTINATION_REPOSITORY_NAME'"
cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY"
echo "[+] Copying $SOURCE_DIRECTORY to $ABSOLUTE_TARGET_DIRECTORY$DESTINATION_DIRECTORY_PREFIX"
cp -r "$SOURCE_DIRECTORY" "$ABSOLUTE_TARGET_DIRECTORY$DESTINATION_DIRECTORY_PREFIX"
done

cd "$CLONE_DIR"
echo ""
echo "[+] List of files that will be pushed:"
ls -la

# Used for local testing, when ran via `./entrypoint.sh [...]`
# GITHUB_REPOSITORY="$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME"
# GITHUB_SHA="$(git rev-parse HEAD)"
# GITHUB_REF="refs/heads/$TARGET_BRANCH"

ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
Expand Down

0 comments on commit 6bb17e6

Please sign in to comment.