mirror of
https://github.com/cpina/github-action-push-to-another-repository.git
synced 2024-12-22 08:06:10 -07:00
Merge pull request #41 from DevOpenWRT-Router/main
Target directory on destination repo
This commit is contained in:
commit
d4122690a7
|
@ -1,4 +1,4 @@
|
||||||
FROM alpine:3.12
|
FROM alpine:latest
|
||||||
|
|
||||||
RUN apk add --no-cache git
|
RUN apk add --no-cache git
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# github-action-push-to-another-repository
|
# github-action-push-to-another-repository-directory
|
||||||
|
|
||||||
When to use this GitHub Action? It is useful in case that you have a GitHub repository with a a directory that you want to push to another GitHub repository using GitHub Actions (automated on push, for example). It is also useful if using GitHub Actions you generate certain files that you want to push to another GitHub repository.
|
When to use this GitHub Action? It is useful in case that you have a GitHub repository with a a directory that you want to push to another GitHub repository using GitHub Actions (automated on push, for example). It is also useful if using GitHub Actions you generate certain files that you want to push to another GitHub repository.
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ Flow:
|
||||||
|
|
||||||
The [example repository](https://github.com/cpina/push-to-another-repository-example) has a MarkDown file [main.md](https://github.com/cpina/push-to-another-repository-example/blob/main/main.md)), during the [GitHub Actions flow](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19) it executes [build.sh](https://github.com/cpina/push-to-another-repository-example/blob/main/build.sh) and the output/ directory (configurable via [source-directory](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L27) appears in the [output repository](https://github.com/cpina/push-to-another-repository-output).
|
The [example repository](https://github.com/cpina/push-to-another-repository-example) has a MarkDown file [main.md](https://github.com/cpina/push-to-another-repository-example/blob/main/main.md)), during the [GitHub Actions flow](https://github.com/cpina/push-to-another-repository-example/blob/main/.github/workflows/ci.yml#L19) it executes [build.sh](https://github.com/cpina/push-to-another-repository-example/blob/main/build.sh) and the output/ directory (configurable via [source-directory](https://github.com/cpina/push-to-another-repository-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 are deleted. This is to make sure that it contains only the generated files in the last run without previously generated files.
|
Please bear in mind: files in the target repository's specified directory are deleted. This is to make sure that it contains only the generated files in the last run without previously generated files.
|
||||||
|
|
||||||
There are different variables to setup the behaviour:
|
There are different variables to setup the behaviour:
|
||||||
|
|
||||||
|
@ -37,6 +37,9 @@ The branch name for the destination repository. It defaults to `main` for histor
|
||||||
### `commit-message` (argument) [optional]
|
### `commit-message` (argument) [optional]
|
||||||
The commit message to be used in the output repository. Optional and defaults to "Update from $REPOSITORY_URL@commit".
|
The commit message to be used in the output repository. Optional and defaults to "Update from $REPOSITORY_URL@commit".
|
||||||
|
|
||||||
|
### `target-directory` (argument) [optional]
|
||||||
|
The directory to wipe and replace in the target repository. Defaults to wiping the entire repository
|
||||||
|
|
||||||
The string `ORIGIN_COMMIT` is replaced by `$REPOSITORY_URL@commit`.
|
The string `ORIGIN_COMMIT` is replaced by `$REPOSITORY_URL@commit`.
|
||||||
|
|
||||||
### `API_TOKEN_GITHUB` (environment)
|
### `API_TOKEN_GITHUB` (environment)
|
||||||
|
|
63
action.yml
63
action.yml
|
@ -1,20 +1,27 @@
|
||||||
name: 'Push directory to another repository'
|
name: Push directory to another repository directory
|
||||||
description: 'Useful to push files to another repository to be used, for example, via github pages'
|
description: >-
|
||||||
|
Useful to push files to another repository to be used, for example, via github
|
||||||
|
pages
|
||||||
inputs:
|
inputs:
|
||||||
|
source-before-directory:
|
||||||
|
description: Source before directory from the origin directory
|
||||||
|
required: false
|
||||||
source-directory:
|
source-directory:
|
||||||
description: 'Source directory from the origin directory'
|
description: Source directory from the origin directory
|
||||||
required: true
|
required: true
|
||||||
destination-github-username:
|
destination-github-username:
|
||||||
description: 'Name of the destination username/organization'
|
description: Name of the destination username/organization
|
||||||
required: true
|
required: true
|
||||||
destination-repository-name:
|
destination-repository-name:
|
||||||
description: 'Destination repository'
|
description: Destination repository
|
||||||
required: true
|
required: true
|
||||||
user-email:
|
user-email:
|
||||||
description: 'Email for the git commit'
|
description: Email for the git commit
|
||||||
required: true
|
required: true
|
||||||
user-name:
|
user-name:
|
||||||
description: '[Optional] Name for the git commit. Defaults to the destination username/organization name'
|
description: >-
|
||||||
|
[Optional] Name for the git commit. Defaults to the destination
|
||||||
|
username/organization name
|
||||||
required: false
|
required: false
|
||||||
default: ''
|
default: ''
|
||||||
destination-repository-username:
|
destination-repository-username:
|
||||||
|
@ -22,25 +29,35 @@ inputs:
|
||||||
required: false
|
required: false
|
||||||
default: ''
|
default: ''
|
||||||
target-branch:
|
target-branch:
|
||||||
description: '[Optional] set target branch name for the destination repository. Defaults to "master" for historical reasons'
|
description: >-
|
||||||
default: 'master'
|
[Optional] set target branch name for the destination repository. Defaults
|
||||||
|
to "main" for historical reasons
|
||||||
|
default: main
|
||||||
required: false
|
required: false
|
||||||
commit-message:
|
commit-message:
|
||||||
description: '[Optional] commit message for the output repository. ORIGIN_COMMIT is replaced by the URL@commit in the origin repo'
|
description: >-
|
||||||
default: 'Update from ORIGIN_COMMIT'
|
[Optional] commit message for the output repository. ORIGIN_COMMIT is
|
||||||
|
replaced by the URL@commit in the origin repo
|
||||||
|
default: Update from ORIGIN_COMMIT
|
||||||
|
required: false
|
||||||
|
target-directory:
|
||||||
|
description: '[Optional] The directory to wipe and replace in the target repository'
|
||||||
|
default: .
|
||||||
required: false
|
required: false
|
||||||
runs:
|
runs:
|
||||||
using: 'docker'
|
using: docker
|
||||||
image: 'Dockerfile'
|
image: Dockerfile
|
||||||
args:
|
args:
|
||||||
- ${{ inputs.source-directory }}
|
- '${{ inputs.source-before-directory }}'
|
||||||
- ${{ inputs.destination-github-username }}
|
- '${{ inputs.source-directory }}'
|
||||||
- ${{ inputs.destination-repository-name }}
|
- '${{ inputs.destination-github-username }}'
|
||||||
- ${{ inputs.user-email }}
|
- '${{ inputs.destination-repository-name }}'
|
||||||
- ${{ inputs.user-name }}
|
- '${{ inputs.user-email }}'
|
||||||
- ${{ inputs.destination-repository-username }}
|
- '${{ inputs.user-name }}'
|
||||||
- ${{ inputs.target-branch }}
|
- '${{ inputs.destination-repository-username }}'
|
||||||
- ${{ inputs.commit-message }}
|
- '${{ inputs.target-branch }}'
|
||||||
|
- '${{ inputs.commit-message }}'
|
||||||
|
- '${{ inputs.target-directory }}'
|
||||||
branding:
|
branding:
|
||||||
icon: 'git-commit'
|
icon: git-commit
|
||||||
color: 'green'
|
color: green
|
||||||
|
|
|
@ -3,15 +3,17 @@
|
||||||
set -e # if a command fails it stops the execution
|
set -e # if a command fails it stops the execution
|
||||||
set -u # script fails if trying to access to an undefined variable
|
set -u # script fails if trying to access to an undefined variable
|
||||||
|
|
||||||
echo "Starts"
|
echo "[+] Action start"
|
||||||
SOURCE_DIRECTORY="$1"
|
SOURCE_BEFORE_DIRECTORY="$1"
|
||||||
DESTINATION_GITHUB_USERNAME="$2"
|
SOURCE_DIRECTORY="$2"
|
||||||
DESTINATION_REPOSITORY_NAME="$3"
|
DESTINATION_GITHUB_USERNAME="$3"
|
||||||
USER_EMAIL="$4"
|
DESTINATION_REPOSITORY_NAME="$4"
|
||||||
USER_NAME="$5"
|
USER_EMAIL="$5"
|
||||||
DESTINATION_REPOSITORY_USERNAME="$6"
|
USER_NAME="$6"
|
||||||
TARGET_BRANCH="$7"
|
DESTINATION_REPOSITORY_USERNAME="$7"
|
||||||
COMMIT_MESSAGE="$8"
|
TARGET_BRANCH="$8"
|
||||||
|
COMMIT_MESSAGE="$9"
|
||||||
|
TARGET_DIRECTORY="${10}"
|
||||||
|
|
||||||
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
|
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
|
||||||
then
|
then
|
||||||
|
@ -25,19 +27,48 @@ fi
|
||||||
|
|
||||||
CLONE_DIR=$(mktemp -d)
|
CLONE_DIR=$(mktemp -d)
|
||||||
|
|
||||||
echo "Cloning destination git repository"
|
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
|
||||||
# Setup git
|
# Setup git
|
||||||
git config --global user.email "$USER_EMAIL"
|
git config --global user.email "$USER_EMAIL"
|
||||||
git config --global user.name "$USER_NAME"
|
git config --global user.name "$USER_NAME"
|
||||||
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
|
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" "$CLONE_DIR"
|
||||||
ls -la "$CLONE_DIR"
|
ls -la "$CLONE_DIR"
|
||||||
|
|
||||||
TARGET_DIR=$(mktemp -d)
|
TEMP_DIR=$(mktemp -d)
|
||||||
# This mv has been the easier way to be able to remove files that were there
|
# This mv has been the easier way to be able to remove files that were there
|
||||||
# but not anymore. Otherwise we had to remove the files from "$CLONE_DIR",
|
# but not anymore. Otherwise we had to remove the files from "$CLONE_DIR",
|
||||||
# including "." and with the exception of ".git/"
|
# including "." and with the exception of ".git/"
|
||||||
mv "$CLONE_DIR/.git" "$TARGET_DIR"
|
mv "$CLONE_DIR/.git" "$TEMP_DIR/.git"
|
||||||
|
|
||||||
|
echo "[+] Checking if $TARGET_DIRECTORY exist in git repo $DESTINATION_REPOSITORY_NAME"
|
||||||
|
# Remove contents of target directory and create a new empty one
|
||||||
|
if [ -d "$CLONE_DIR/$TARGET_DIRECTORY/" ]
|
||||||
|
then
|
||||||
|
echo "[+] Deleting files from $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME"
|
||||||
|
rm -R "${CLONE_DIR:?}/$TARGET_DIRECTORY/"
|
||||||
|
fi
|
||||||
|
echo "[+] Creating $TARGET_DIRECTORY if doesnt already exist"
|
||||||
|
mkdir -p "$CLONE_DIR/$TARGET_DIRECTORY"
|
||||||
|
|
||||||
|
mv "$TEMP_DIR/.git" "$CLONE_DIR/.git"
|
||||||
|
|
||||||
|
echo "[+] Listing Current Directory Location"
|
||||||
|
ls -al
|
||||||
|
#echo "[+] Listing home+ Directory Location"
|
||||||
|
#ls -al /home/runner/work/Action_OpenWRT_AutoBuild_Linksys_Devices/Action_OpenWRT_AutoBuild_Linksys_Devices/openwrt
|
||||||
|
echo "[+] Listing root Location"
|
||||||
|
ls -al /
|
||||||
|
echo "[+] Listing /home Location"
|
||||||
|
ls -al /home
|
||||||
|
|
||||||
|
# echo "[+] Changing to $SOURCE_BEFORE_DIRECTORY"
|
||||||
|
# cd "$SOURCE_BEFORE_DIRECTORY"
|
||||||
|
|
||||||
|
|
||||||
|
echo "[+] List contents of $SOURCE_DIRECTORY"
|
||||||
|
ls "$SOURCE_DIRECTORY"
|
||||||
|
|
||||||
|
echo "[+] Checking if local $SOURCE_DIRECTORY exist"
|
||||||
if [ ! -d "$SOURCE_DIRECTORY" ]
|
if [ ! -d "$SOURCE_DIRECTORY" ]
|
||||||
then
|
then
|
||||||
echo "ERROR: $SOURCE_DIRECTORY does not exist"
|
echo "ERROR: $SOURCE_DIRECTORY does not exist"
|
||||||
|
@ -52,27 +83,27 @@ then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Copy contents to target git repository"
|
echo "[+] Copying contents of source repository folder $SOURCE_DIRECTORY to folder $TARGET_DIRECTORY in git repo $DESTINATION_REPOSITORY_NAME"
|
||||||
cp -ra "$SOURCE_DIRECTORY"/. "$TARGET_DIR"
|
cp -ra "$SOURCE_DIRECTORY"/. "$CLONE_DIR/$TARGET_DIRECTORY"
|
||||||
cd "$TARGET_DIR"
|
cd "$CLONE_DIR"
|
||||||
|
|
||||||
echo "Files that will be pushed:"
|
echo "[+] Files that will be pushed"
|
||||||
ls -la
|
ls -la
|
||||||
|
|
||||||
ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
|
ORIGIN_COMMIT="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
|
||||||
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
|
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
|
||||||
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
|
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
|
||||||
|
|
||||||
echo "git add:"
|
echo "[+] Adding git commit"
|
||||||
git add .
|
git add .
|
||||||
|
|
||||||
echo "git status:"
|
echo "[+] git status:"
|
||||||
git status
|
git status
|
||||||
|
|
||||||
echo "git diff-index:"
|
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 : 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"
|
git diff-index --quiet HEAD || git commit --message "$COMMIT_MESSAGE"
|
||||||
|
|
||||||
echo "git push origin:"
|
echo "[+] Pushing git commit"
|
||||||
# --set-upstream: sets de branch when pushing to a branch that does not exist
|
# --set-upstream: sets de branch when pushing to a branch that does not exist
|
||||||
git push "https://$USER_NAME:$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH"
|
git push "https://$USER_NAME:$API_TOKEN_GITHUB@github.com/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user