diff --git a/Dockerfile b/Dockerfile index f93756d..7f8af44 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 924cea4..70a0f97 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -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 @@ -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: carles3@pina.cat - 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: fake@email.com + 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) diff --git a/action.yml b/action.yml index 8490516..b2804fe 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 }}" diff --git a/entrypoint.sh b/entrypoint.sh index 2bfebf0..b5af35e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,4 @@ -#!/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 @@ -6,16 +6,17 @@ 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 @@ -55,7 +56,6 @@ else exit 1 fi - CLONE_DIR=$(mktemp -d) echo "[+] Git version" @@ -107,23 +107,41 @@ 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" @@ -131,11 +149,6 @@ 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}"