Skip to content

Commit

Permalink
Merge pull request #1 from meese-enterprises/multiple-source-directories
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 authored Jul 9, 2022
2 parents d613851 + e21d565 commit 74ef32e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 94 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ There are two example repositories:
* [Repository 1](https://github.com/cpina/push-to-another-repository-deploy-keys-example): using SSH deploy keys (recommended)
* [Repository 2](https://github.com/cpina/push-to-another-repository-example): using a personal access token setup

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).
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).

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.

There are different variables to set up the behaviour:

## Inputs
### `source-directory` (argument)
From the repository that this Git Action is executed the directory that contains the files to be pushed into the repository.
### `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-github-username` (argument)
For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`.
Expand Down Expand Up @@ -112,7 +112,7 @@ Then make the token available to the Github Action following the steps:
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
with:
source-directory: 'output'
source-directories: ['subrepo1/output', 'subrepo2/output']
destination-github-username: 'cpina'
destination-repository-name: 'pandoc-test-output'
user-email: [email protected]
Expand Down
52 changes: 24 additions & 28 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: Push directory to another repository
name: Push directories to another repository
description: >-
Useful to push files to another repository to be used, for example, via github
pages
Useful to push files to another repository to be used, for example, via GitHub
Pages
inputs:
source-before-directory:
description: Source before directory from the origin directory
required: false
source-directory:
description: Source directory from the origin directory
source-directories:
description: Source directories from the origin
required: true
destination-github-username:
description: Name of the destination username/organization
Expand All @@ -19,19 +16,19 @@ inputs:
description: Email for the git commit
required: true
github-server:
description: 'Github server'
default: 'github.com'
description: "Github server"
default: "github.com"
required: false
user-name:
description: >-
[Optional] Name for the git commit. Defaults to the destination
username/organization name
required: false
default: ''
default: ""
destination-repository-username:
description: '[Optional] Username/organization for the destination repository'
description: "[Optional] Username/organization for the destination repository"
required: false
default: ''
default: ""
target-branch:
description: >-
[Optional] set target branch name for the destination repository. Defaults
Expand All @@ -45,30 +42,29 @@ inputs:
default: Update from ORIGIN_COMMIT
required: false
target-directory:
description: '[Optional] The directory to wipe and replace in the target repository'
default: ''
description: "[Optional] The directory to wipe and replace in the target repository"
default: ""
required: false
force:
description: "[Optional] If 'true', will force push and thus overwrite the target repo's history"
default: 'false'
default: "false"
required: false

runs:
using: docker
image: Dockerfile
args:
- '${{ inputs.source-before-directory }}'
- '${{ inputs.source-directory }}'
- '${{ inputs.destination-github-username }}'
- '${{ inputs.destination-repository-name }}'
- '${{ inputs.github-server }}'
- '${{ inputs.user-email }}'
- '${{ inputs.user-name }}'
- '${{ inputs.destination-repository-username }}'
- '${{ inputs.target-branch }}'
- '${{ inputs.commit-message }}'
- '${{ inputs.target-directory }}'
- '${{ inputs.force }}'
- "${{ inputs.source-directories }}"
- "${{ inputs.destination-github-username }}"
- "${{ inputs.destination-repository-name }}"
- "${{ inputs.github-server }}"
- "${{ inputs.user-email }}"
- "${{ inputs.user-name }}"
- "${{ inputs.destination-repository-username }}"
- "${{ inputs.target-branch }}"
- "${{ inputs.commit-message }}"
- "${{ inputs.target-directory }}"
- "${{ inputs.force }}"
branding:
icon: git-commit
color: green
133 changes: 71 additions & 62 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
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_BEFORE_DIRECTORY="${1}"
SOURCE_DIRECTORY="${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}"
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}"

if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
then
Expand All @@ -27,13 +27,14 @@ then
USER_NAME="$DESTINATION_GITHUB_USERNAME"
fi

DESTINATION_REPOSITORY="$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME"
TARGET_BRANCH_EXISTS=true

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

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

GIT_CMD_REPOSITORY="git@$GITHUB_SERVER:$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git"
GIT_CMD_REPOSITORY="git@$GITHUB_SERVER:$DESTINATION_REPOSITORY.git"

elif [ -n "${API_TOKEN_GITHUB:=}" ]
then
GIT_CMD_REPOSITORY="https://$DESTINATION_REPOSITORY_USERNAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git"
GIT_CMD_REPOSITORY="https://$DESTINATION_REPOSITORY_USERNAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY.git"
else
echo "::error::API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one (recommended the SSH_DEPLOY_KEY"
echo "[-] API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one in!"
exit 1
fi

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

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

echo ""
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
{
git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR"
} || {
{
echo "Target branch doesn't exist, fetching main branch"
git clone --single-branch "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
echo "Target branch doesn't exist, fetching the 'main' branch"
git clone --single-branch "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY.git" "$CLONE_DIR"
TARGET_BRANCH_EXISTS=false
} || {
echo "::error::Could not clone the destination repository. Command:"
echo "::error::git clone --single-branch --depth 1 --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR"
echo "::error::(Note that if they exist USER_NAME and API_TOKEN is redacted by GitHub)"
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"
echo ""
echo "[-] Could not clone the destination repository. Command:"
echo "[-] git clone --single-branch --depth 1 --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR"
echo "[-] (Note that if they exist, USER_NAME and API_TOKEN are redacted by GitHub)"
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"
exit 1
}
}

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

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

echo ""
echo "[+] Deleting $ABSOLUTE_TARGET_DIRECTORY"
rm -rf "$ABSOLUTE_TARGET_DIRECTORY"

echo ""
echo "[+] Creating (now empty) $ABSOLUTE_TARGET_DIRECTORY"
mkdir -p "$ABSOLUTE_TARGET_DIRECTORY"

echo "[+] Listing Current Directory Location"
ls -al

echo "[+] Listing root Location"
ls -al /

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

echo "[+] List contents of $SOURCE_DIRECTORY"
ls "$SOURCE_DIRECTORY"
# 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

echo ""
echo "[+] List contents of $SOURCE_DIRECTORY:"
ls -la "$SOURCE_DIRECTORY"

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

echo "[+] Checking if local $SOURCE_DIRECTORY exist"
if [ ! -d "$SOURCE_DIRECTORY" ]
then
echo "ERROR: $SOURCE_DIRECTORY does not exist"
echo "This directory needs to exist when push-to-another-repository is executed"
echo
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"
echo
echo "If you want to copy a directory that exist in the source repository"
echo "to the target repository: you need to clone the source repository"
echo "in a previous step in the same build section. For example using"
echo "actions/checkout@v2. See: https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L16"
exit 1
fi

echo "[+] Copying contents of source repository folder $SOURCE_DIRECTORY to folder $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME"
cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY"
cd "$CLONE_DIR"

echo "[+] Files that will be pushed"
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}"

if [ "$TARGET_BRANCH_EXISTS" = false ] ; then
if [ "$TARGET_BRANCH_EXISTS" = false ]; then
echo ""
echo "Creating branch $TARGET_BRANCH"
git checkout -b "$TARGET_BRANCH"
fi

echo "[+] Set directory is safe ($CLONE_DIR)"
# 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
# TODO: review before releasing it as a version
# 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
echo ""
echo "[+] Set directory as safe ($CLONE_DIR)"
git config --global --add safe.directory "$CLONE_DIR"

echo ""
echo "[+] Adding git commit"
git add .

echo ""
echo "[+] git status:"
git status

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

if $FORCE; then
echo "[+] Force pushing git commit"
FORCE_FLAG="-f"
# --set-upstream: sets de branch when pushing to a branch that does not exist
echo ""
if [ "$FORCE" = true ]; then
echo "[+] Forcefully pushing git commit"
git push -f "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"
else
echo "[+] Pushing git commit"
FORCE_FLAG=""
git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"
fi

# --set-upstream: sets de branch when pushing to a branch that does not exist
git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH" "$FORCE_FLAG"

0 comments on commit 74ef32e

Please sign in to comment.