Skip to content

Commit

Permalink
modify to keep files in the destination repository
Browse files Browse the repository at this point in the history
  • Loading branch information
nkoppel committed Mar 15, 2021
1 parent f308953 commit 1960f66
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# github-action-push-to-another-repository

Used to push generated files from a directory from Git Action step into another repository on Github. By design it deletes the files from the destination directory as it is meant to "publish" a set generated files.
Used to push generated files from a directory from Git Action step into another repository on Github.

E.g.
Repository pandoc-test contains Markdown and a Git Action to generate, using Pandoc, an output: HTML, PDF, odt, epub, etc.
Expand All @@ -10,22 +10,23 @@ Repository pandoc-test-output: contains only the generated files from the first
And pandoc-test-output can have Git Pages to give access to the files (or just links to the raw version of the files)

## 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-files` (argument)
From the repository that this Git Action is executed the files to be pushed into the repository. This argument supports multiple space-separated filenames and globbing.

### `destination-directory` (argument) [optional]
The directory in the destination repository to copy the source files into.

### `destination-github-username` (argument)
For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`. It's also used for the `Author:` in the generated git messages.
For the repository `https://github.com/nkoppel/push-to-another-repository-output` is `nkoppel`. It's also used for the `Author:` in the generated git messages.

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

*Warning:* this Github Action currently deletes all the files and directories in the destination repository. The idea is to copy from an `output` directory into the `destination-repository-name` having a copy without any previous files there.
For the repository `https://github.com/nkoppel/push-to-another-repository-output` is `push-to-another-repository-output`

### `user-email` (argument)
The email that will be used for the commit in the destination-repository-name.

### `destination-repository-username` (argument) [optional]
The Username/Organization for the destination repository, if different from `destination-github-username`. For the repository `https://github.com/cpina/push-to-another-repository-output` is `cpina`.
The Username/Organization for the destination repository, if different from `destination-github-username`. For the repository `https://github.com/nkoppel/push-to-another-repository-output` is `nkoppel`.

### `target-branch` (argument) [optional]
The branch name for the destination repository, if different from `master`.
Expand Down Expand Up @@ -53,22 +54,12 @@ 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@master
uses: nkoppel/push-files-to-another-repository@master
env:
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
with:
source-directory: 'output'
destination-github-username: 'cpina'
source-files: 'output/*'
destination-github-username: 'nkoppel'
destination-repository-name: 'pandoc-test-output'
user-email: [email protected]
user-email: [email protected]
```
Working example:
https://github.com/cpina/push-to-another-repository-example/blob/master/.github/workflows/ci.yml
It generates files from:
https://github.com/cpina/push-to-another-repository-example
To:
https://github.com/cpina/push-to-another-repository-output
13 changes: 9 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: 'Push directory to another repository'
name: 'Copy files to another repository'
description: 'Useful to push files to another repository to be used, for example, via github pages'
inputs:
source-directory:
description: 'Source directory from the origin directory'
source-files:
description: 'Files to be pushed to the destination repository. Accepts multiple filenames and globbing.'
required: true
destination-github-username:
description: 'Name of the destination username/organization'
Expand All @@ -13,6 +13,10 @@ inputs:
user-email:
description: 'Email for the git commit'
required: true
destination-directory:
description: '[Opitonal] Directory to copy files into in the destination repository'
required: false
default: '.'
destination-repository-username:
description: '[Optional] Username/organization for the destination repository'
required: false
Expand All @@ -29,10 +33,11 @@ runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.source-directory }}
- ${{ inputs.source-files }}
- ${{ inputs.destination-github-username }}
- ${{ inputs.destination-repository-name }}
- ${{ inputs.user-email }}
- ${{ inputs.destination-directory }}
- ${{ inputs.destination-repository-username }}
- ${{ inputs.target-branch }}
- ${{ inputs.commit-message }}
Expand Down
17 changes: 8 additions & 9 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ set -e # if a command fails it stops the execution
set -u # script fails if trying to access to an undefined variable

echo "Starts"
SOURCE_DIRECTORY="$1"
SOURCE_FILES="$1"
DESTINATION_GITHUB_USERNAME="$2"
DESTINATION_REPOSITORY_NAME="$3"
USER_EMAIL="$4"
DESTINATION_REPOSITORY_USERNAME="$5"
TARGET_BRANCH="$6"
COMMIT_MESSAGE="$7"
DESTINATION_DIRECTORY="$4"
DESTINATION_REPOSITORY_USERNAME="$6"
TARGET_BRANCH="$7"
COMMIT_MESSAGE="$8"

if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
then
Expand All @@ -26,12 +27,10 @@ git config --global user.name "$DESTINATION_GITHUB_USERNAME"
git clone --single-branch --branch "$TARGET_BRANCH" "https://$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
ls -la "$CLONE_DIR"

TARGET_DIR=$(mktemp -d)
mv "$CLONE_DIR/.git" "$TARGET_DIR"

echo "Copying contents to git repo"
cp -ra "$SOURCE_DIRECTORY"/. "$TARGET_DIR"
cd "$TARGET_DIR"
mkdir -p "$CLONE_DIR/$DESTINATION_DIRECTORY"
cp -rf $SOURCE_FILES "$CLONE_DIR/$DESTINATION_DIRECTORY"
cd "$CLONE_DIR"

echo "Files that will be pushed"
ls -la
Expand Down

0 comments on commit 1960f66

Please sign in to comment.