Compare commits

...

117 Commits
v1.1 ... main

Author SHA1 Message Date
Carles Pina i Estany
7c1bd869f3 Not being able to develop: add warning 2024-09-15 20:25:58 +01:00
Carles Pina i Estany
07c4d7b3de Add workaround for https://github.com/cpina/github-action-push-to-another-repository/issues/103 2023-05-19 23:31:53 +02:00
Carles Pina i Estany
b2e2c48ca5 Fix error if create-target-branch-if-needed is used and the target branch already existed 2023-05-19 16:47:16 +02:00
Carles Pina i Estany
d63a102c8f Update comment and output 2023-05-19 16:28:33 +02:00
Carles Pina i Estany
cbe757f8d8 Change indentation, add comment, use "git switch -c" instead of "git checkout -b" 2023-05-01 00:03:52 +02:00
Damien Cornu
33585ea95a Create branch if needed option 2023-04-25 15:16:26 +02:00
Carles Pina Estany
0a14457bb2
Enable git lfs (#98) 2023-02-25 21:58:06 +00:00
Carles Pina Estany
9e487f2958
Update README.md: add links to the documentation (#89) 2022-09-26 23:44:58 +01:00
Carles Pina Estany
f4b9c90516
Add issue template (#88) 2022-09-21 17:33:41 +01:00
Carles Pina i Estany
6cb505d32f Since we need to download only the last snapshot, it is not necessary to download the full commits history.
Thanks @wcota
PR: https://github.com/cpina/github-action-push-to-another-repository/pull/60
2022-09-06 00:18:35 +01:00
Carles Pina i Estany
74596b72fa Link to the formatted documentation 2022-09-05 23:33:35 +01:00
Carles Pina i Estany
9391058a7d Use the specified server to get the ssh fingerprints 2022-09-05 23:20:06 +01:00
Carles Pina Estany
e775fb8544
Update issue templates 2022-07-28 12:03:10 +01:00
Carles Pina i Estany
cda33b879b Action is more verbose (in order to help debugging) 2022-07-14 22:04:39 +01:00
g
f8f86f8d8f Fix if / then / else condition 2022-06-10 09:45:34 +02:00
Carles Pina i Estany
e9f59d2faa Fix command line being executed 2022-06-10 09:42:52 +02:00
Carles Pina i Estany
ab68811c3a Merge branch 'ssh-deploy-key' 2022-06-09 21:20:39 +02:00
Carles Pina i Estany
b5ac509f7b Update README.md with deploy ssh keys documentation 2022-06-09 21:19:29 +02:00
Carles Pina i Estany
47ce62aef3 Updates README.md 2022-06-09 08:58:06 +02:00
Carles Pina Estany
1cdcd11404
Update README with a reference to the new branch deploying with SSH keys 2022-06-09 00:48:53 +02:00
Carles Pina i Estany
1ff90e4a65 Add support for SSH_DEPLOY_KEY variable (instead of only the personal access token) 2022-06-09 00:46:30 +02:00
Carles Pina i Estany
f973f49c62 Add link to a related action 2022-06-08 21:26:31 +02:00
Carles Pina i Estany
3f9ba1ff0a Improve README.md with details of the PAT 2022-06-08 21:23:17 +02:00
Carles Pina i Estany
9543f7e15d Use repository stable
Alpine's Edge was used when a newer git version was needed. Now the
stable one is new enough
2022-05-27 09:24:11 +02:00
Carles Pina i Estany
aa2fdffb28 Use the correct variable on git clone of the destination repository
fixes #68
2022-05-27 09:19:02 +02:00
Carles Pina Estany
ac0bb2c8f9
Fix unsafe repository error (#65)
Mark the repository as safe.

It should be ok to do so in the action context because the vulnerability affects "multi-user shared machines" and this is a single-user Docker image. Details of why this is needed CVE-2022-24765: https://github.blog/2022-04-12-git-security-vulnerability-announced/

I will keep monitoring if there could be any problems and get in touch in you have more information.
2022-04-13 10:17:12 +02:00
Carles Pina i Estany
483689a71c Use {} in the variable expansion 2022-01-21 17:42:11 +01:00
Carles Pina i Estany
084156bc22 Rename so it can be published (hopefully) 2022-01-20 23:47:19 +01:00
Carles Pina i Estany
418e6e7a36 Merge branch 'main' of github.com:cpina/github-action-push-to-another-repository into main 2022-01-20 23:31:50 +01:00
Carles Pina i Estany
f5b6d14ea8 Simplify variable expansion 2022-01-20 23:30:55 +01:00
Carles Pina Estany
164a872083
Merge pull request #51 from nedbat/nedbat/fix-readme
README: two fixes from target-directory work
2021-12-28 20:39:26 +00:00
Ned Batchelder
2e729bfae7 README: two fixes from target-directory work
1. The sentence about `ORIGIN_COMMIT` should be part of the description
   of `commit-message`.

2. The title of the repo is still `github-action-push-to-another-repository`.
2021-12-28 06:25:56 -05:00
Carles Pina i Estany
9077ab5399 Improve error message 2021-12-15 00:13:36 +00:00
Carles Pina i Estany
23869efe3d Better handling of the git clone error 2021-12-15 00:09:49 +00:00
Carles Pina i Estany
331033bc0f Try a better way to report errors 2021-12-15 00:01:48 +00:00
Carles Pina i Estany
b8ba82a17f Fix typo 2021-12-14 23:48:39 +00:00
Carles Pina i Estany
7096925caa Report the error if the destination branch does not exist 2021-12-14 23:46:24 +00:00
Carles Pina i Estany
e6c065d351 Fix text 2021-12-14 23:41:43 +00:00
Carles Pina i Estany
e7314057a0 Fix deleting of files when TARGET_DIRECTORY is not specified 2021-12-08 23:55:47 +01:00
Carles Pina i Estany
24c550e8a7 Merge branch 'main' of github.com:cpina/github-action-push-to-another-repository into main 2021-12-08 23:34:05 +01:00
Carles Pina i Estany
c200780764 Improve message 2021-12-08 23:33:53 +01:00
Carles Pina Estany
6e36e20d0e
Merge pull request #44 from rottencandy/patch-2
Update documentation for target-branch
2021-11-10 08:58:48 +01:00
Mohammed Saud
cdae6bf3fc
Update documentation for target-branch
Target branch was defaulted to main in b97d556c
But the readme documentation was not updated.
2021-11-10 13:11:24 +05:30
Carles Pina i Estany
460cd93e14 Restore the .git even in the code path without target_directory 2021-10-26 00:04:38 +01:00
Carles Pina i Estany
66ead10846 Delete unnedded code 2021-10-26 00:02:51 +01:00
Carles Pina i Estany
5ca36a784d If no directory to be deleted avoids... deleting the directory 2021-10-25 23:55:36 +01:00
Carles Pina i Estany
371fdd6652 Avoid unneeded check 2021-10-25 23:47:44 +01:00
Carles Pina i Estany
df5ac1b335 Merge branch 'patch-1' into main 2021-10-25 23:41:08 +01:00
Carles Pina i Estany
f7a5c6e388 Change the order (all the required and then all the optionals) 2021-10-25 23:32:18 +01:00
Carles Pina i Estany
9ed2368a01 server is not required (default was already set) 2021-10-25 23:31:41 +01:00
Carles Pina Estany
eb81a2a185
Merge pull request #35 from oshdev/patch-1
fix(target-branch): historical branch name
2021-10-25 23:06:42 +01:00
Carles Pina i Estany
af82da7f2b Use indentation consistenly 2021-10-25 23:03:05 +01:00
Carles Pina Estany
d4122690a7
Merge pull request #41 from DevOpenWRT-Router/main
Target directory on destination repo
2021-10-25 22:55:10 +01:00
Osh Slomczynski
aa69941031
fix(target-branch): historical branch name
Historical value for the default `target-branch` option was and still is `master`: 2ebe0cc15f/action.yml (L26)
2021-09-02 13:10:12 +01:00
Eliminater74
44d7a5c0f9
Update Fix 2021-08-31 06:04:57 -04:00
Eliminater74
2708e223bc
Update entrypoint.sh Cleanup 2021-08-29 18:15:51 -04:00
Eliminater74
38d751da52
Update action.yml Script: Cleanup 2021-08-29 18:07:37 -04:00
Eliminater74
ed4074dd02
Another fix 2021-08-29 09:30:34 -04:00
Eliminater74
52acf3c68c
fixme 2021-08-29 09:26:38 -04:00
Eliminater74
7a8c60c186
Add some fixes 2021-08-29 08:24:21 -04:00
Eliminater74
b4480ee4f3
Dockerfile: FROM alpine:latest 2021-08-21 08:28:35 -04:00
Eliminater74
b646e274db
Update: entrypoint.sh 2021-08-20 20:54:32 -04:00
Eliminater74
b97d556c1d
Update: action.yml 2021-08-20 19:30:49 -04:00
Eliminater74
67a3a057c8
Update: enterypoint.sh 2021-08-20 19:23:03 -04:00
Michael Ryan
12ff072138
Fixed mistake in readme
Signed-off-by: Eliminater74 <eliminater74@gmail.com>
2021-08-20 18:57:40 -04:00
Michael Ryan
f87dd89716
Modified so this could be deployed to github marketplace
Signed-off-by: Eliminater74 <eliminater74@gmail.com>
2021-08-20 18:57:09 -04:00
Michael Ryan
520a9d8c28
Allows pushing to target directory in target repo
Signed-off-by: Eliminater74 <eliminater74@gmail.com>
2021-08-20 18:56:37 -04:00
Eliminater74
448b7dad95
Test FIX 2021-08-20 18:40:02 -04:00
Piotr Joński
84dc6cc070
Update entrypoint.sh 2021-06-24 09:00:58 +02:00
Piotr Joński
69e1f423c8
Update action.yml 2021-06-24 09:00:26 +02:00
Piotr Joński
280163351f
Update entrypoint.sh 2021-06-24 08:59:20 +02:00
Piotr Joński
c3f0aa4db9
Add github-server input parameter 2021-06-24 08:57:24 +02:00
Carles Pina Estany
2ebe0cc15f
Merge pull request #29 from HonQii/patch-1
Fix could not read Username for 'https://github.com' error
2021-05-08 12:34:02 +02:00
HonQi
6b89adc168
Fix could not read Username for 'https://github.com' error
Push to the repo of another account (such as an organization) may trigger this error. Clearly indicate the account name to avoid
2021-05-06 16:28:27 +08:00
Carles Pina Estany
940a2857e5
Merge pull request #27 from JamesMCo/main
Add argument to specify name used in commits
2021-04-29 23:45:48 +02:00
James C
637a257cca
Add argument to specify name used in commits 2021-04-29 06:06:40 +01:00
Carles Pina i Estany
5c472514b3 Better use "$" to make it clear that this is going to be escaped 2021-04-25 17:59:53 +01:00
Carles Pina i Estany
cb0e53ecbe Try to let users use GITHU_REF variable in the commit message 2021-04-25 17:53:31 +01:00
Carles Pina i Estany
8e9bfb00e6 Improve error message 2021-04-25 11:26:51 +01:00
Carles Pina i Estany
9b1e7ac6a5 Fix indentation (spaces) 2021-04-25 11:22:41 +01:00
Carles Pina i Estany
3a9dc3773c Uses "main" branch instead of "master" for the examples, instructions, etc. 2021-04-25 11:09:13 +01:00
Carles Pina i Estany
88287f5737 Fix markdown 2021-04-25 10:59:12 +01:00
Carles Pina i Estany
85a0ac138a Improve the error message 2021-04-25 10:57:58 +01:00
Carles Pina i Estany
bb3e4ea525 Add a check in entrypoint.sh to help users if the source directory does not exist
Updated documentation
2021-04-25 10:55:07 +01:00
Carles Pina i Estany
f30895310c Fixed issue deleting files that need to be deleted from the target repo
See #16
2020-12-02 20:04:04 +00:00
Carles Pina i Estany
f5a3797d5b Fix typo 2020-12-02 16:21:05 +00:00
Carles Pina i Estany
ec672cbc0a Fixed deleting files from $CLONE_DIR (keep $CLONE_DIR/.git and deletes files like $CLONE_DIR/.gitattributes)
Thanks @ruuk for the report
2020-12-02 16:16:04 +00:00
Carles Pina i Estany
e7bc77c21d Added verbose 2020-12-02 15:51:35 +00:00
Carles Pina i Estany
3d4da26ca5 Fixed problem deleting files starting with .git (e.g. .gitattributes) 2020-12-02 15:43:05 +00:00
Carles Pina i Estany
976916018a Improve comment 2020-11-10 09:01:07 +00:00
Carles Pina Estany
ea29b5b83f
Fix pushing the generated files into a branch that does not exist
allow to push into remote, even if brach does not exist
2020-11-10 09:00:30 +00:00
Carles Pina Estany
edbe7a0efc
Use long command option instead of short and adds comment
Thanks @TomasVotruba
2020-11-10 08:59:28 +00:00
Tomas Votruba
9852c86807
allow to push into remote, even if brach does not exist 2020-11-10 02:36:41 +01:00
Carles Pina i Estany
eae1cf2f70 Avoid failing if no changes are to be pushed 2020-11-05 17:43:46 +00:00
Carles Pina i Estany
61d842d233 Fix typo in a message 2020-11-05 17:39:27 +00:00
Carles Pina i Estany
c26a144ee1 Fix substitution. Works with any strings (not as sed)
Less portable but tested with Busybox shell used in this image
2020-11-04 16:45:08 +00:00
Carles Pina i Estany
79e1b1b20b Fix sed (had the missing slash but now replaces it every time) 2020-11-04 16:28:43 +00:00
Carles Pina i Estany
c6421df38d Fix "typo" in yml 2020-11-04 16:26:43 +00:00
Carles Pina i Estany
cfb5cb5949 Allow to pass the commit message 2020-11-04 16:23:02 +00:00
Carles Pina i Estany
5a00473a58 Align names of the variables in entrypoint.sh with the names of the variables in action.yml 2020-11-04 15:27:20 +00:00
Carles Pina i Estany
61d0646c22 Improve documentation 2020-11-04 15:20:40 +00:00
Carles Pina i Estany
da21c6b08d set options to make it easier to debug if a command fails
Make use of optional arguments on action.yml in order to avoid testing
against an undefined variable
2020-11-04 15:02:44 +00:00
Carles Pina i Estany
884b652fa7 Update alpine to latest released 2020-11-04 14:41:02 +00:00
Carles Pina i Estany
5d8dc0fe1a Add double quotes
TARGET_BRANCH cannot have spaces currently but adding double shell for
consistent style.

GITHUB_USERNAME probably cannot have spaces but just for consistency
(and in case that it can have spaces)
2020-09-23 09:53:20 +01:00
Carles Pina Estany
160c443d67
Merge pull request #6 from smellman/master
supports github branch renaming (push to another branch that is not master)
2020-09-23 09:50:31 +01:00
Taro Matsuzawa
80a683b9ad fixes missing ] 2020-09-23 16:53:28 +09:00
Taro Matsuzawa
26fe0168e2 supports target branch for github-renaming 2020-09-23 16:49:12 +09:00
Carles Pina Estany
db81c54e24
Merge pull request #4 from dmnemec/master
allow separate author and destination repository
2020-08-12 15:04:22 +02:00
Devin Nemec
4b313f1b76 unset upstream (not needed) 2020-08-10 09:14:18 -05:00
Devin Nemec
a76c6fd84a explicitly clone and use master branch 2020-08-10 09:13:52 -05:00
Devin Nemec
dc3ca8455d fix typo 2020-08-10 09:03:53 -05:00
Devin Nemec
b511d6418a adding context and setting upstream 2020-08-10 09:02:28 -05:00
Devin Nemec
b174d4c1a3 fix if statement 2020-08-10 08:31:08 -05:00
Devin Nemec
a69edf2ee5 remove space 2020-08-10 08:06:59 -05:00
Devin Nemec
f32c463e11 allow separate author and destination repository 2020-08-07 15:49:54 -05:00
Carles Pina Estany
ece5884654
Merge pull request #2 from eibanez/master
Delete trailing parenthesis
2020-07-29 10:02:31 +02:00
Eduardo Ibanez
807f8fa4f7
Delete trailing parenthesis 2020-07-28 21:32:30 -05:00
6 changed files with 288 additions and 86 deletions

38
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@ -0,0 +1,38 @@
---
name: Bug report
about: Bug report with a problem
title: ''
labels: bug
assignees: ''
---
> [!WARNING]
> TL;DR: this repository is not maintained for any new development. I plan to
> fix issues if they arise due to changes on GitHub, to minimise disruption of
> existing usage.
>
> My circumstances changed since I started the GitHub Action in 2020. I am not
> able to add functionality to it or fix issues.
>
> The action is 175 lines of shell script. Feel free to fork it and modify it
> for your own use case.
>
> If you create a fork that might replace this one, I will add a note in the
> documentation and the README.md. Please, open an issue and I will do it.
Suggestion: read carefully the GitHub Action output. It might contain information that might help fixing the problem. If not please keep opening the bug report.
The [documentation](https://github.com/cpina/push-to-another-repository-docs)
might have information on what you could do to fix the problem. See the [FAQ](https://cpina.github.io/push-to-another-repository-docs/faq.html)
<!--
**Copy-paste the output of the GitHub Action**
Full output might contain useful information including the git command, git output and settings used. It also helps to search for the problem later on.
**URL to the execution**
Paste the URL of the execution where it's possible to see the problem. This will help me to see the configuration and the output in more context.
-->

View File

@ -0,0 +1,10 @@
---
name: Windows/Mac/No Docker
about: Questions about Windows/Mac/No Docker runners
title: ''
labels: Windows-Mac-NoDocker
assignees: ''
---
Suggestion: see the relevant FAQ entry: https://cpina.github.io/push-to-another-repository-docs/faq.html#could-it-work-on-mac-os-windows-or-no-docker-environments

View File

@ -1,6 +1,6 @@
FROM alpine:3.10
FROM alpine:latest
RUN apk add --no-cache git
RUN apk add --no-cache git git-lfs openssh-client
COPY entrypoint.sh /entrypoint.sh

View File

@ -1,61 +1,19 @@
# github-action-push-to-another-repository
Used to push generated files from a directory from Git Action step into another repository on Github.
> [!WARNING]
> TL;DR: this repository is not maintained for any new development. I plan to
> fix issues if they arise due to changes on GitHub, to minimise disruption of
> existing usage.
>
> My circumstances changed since I started the GitHub Action in 2020. I am not
> able to add functionality to it or fix issues.
>
> The action is 175 lines of shell script. Feel free to fork it and modify it
> for your own use case.
>
> If you create a fork that might replace this one, I will add a note in the
> documentation and the README.md. Please, open an issue and I will do it.
E.g.
Repository pandoc-test contains Markdown and a Git Action to generate, using Pandoc, an output: HTML, PDF, odt, epub, etc.
See the extensive documentation in https://cpina.github.io/push-to-another-repository-docs/ (includes examples, FAQ, troubleshooting, etc.).
Repository pandoc-test-output: contains only the generated files from the first Git Action. Pushed here with github-action-push-to-another-repository
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.
### `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.
### `destination-repository-name` (argument)
For the repository `https://github.com/cpina/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.
### `API_TOKEN_GITHUB` (environment)
E.g.:
`API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}`
Generate your personal token following the steps:
* Go to the Github Settings (on the right hand side on the profile picture)
* On the left hand side pane click on "Developer Settings"
* Click on "Personal Access Tokens" (also available at https://github.com/settings/tokens)
* Generate a new token, choose "Repo". Copy the token.
Then make the token available to the Github Action following the steps:
* Go to the Github page for the repository that you push from, click on "Settings"
* On the left hand side pane click on "Secrets"
* Click on "Add a new secret" and name it "API_TOKEN_GITHUB"
## Example usage
```yaml
- name: Pushes to another repository
uses: cpina/github-action-push-to-another-repository@master
env:
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
with:
source-directory: 'output'
destination-github-username: 'cpina'
destination-repository-name: 'pandoc-test-output'
user-email: carles3@pina.cat
```
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
GitHub repository of the documentation: https://github.com/cpina/push-to-another-repository-docs

View File

@ -1,26 +1,76 @@
name: 'Push directory to another repository'
description: 'Useful to push files to another repository to be used, for example, via github pages'
name: Push directory to another repository
description: >-
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'
description: Source directory from the origin directory
required: true
destination-github-username:
description: 'Name of the destination username/organization'
description: Name of the destination username/organization
required: true
destination-repository-name:
description: 'Destination repository'
description: Destination repository
required: true
user-email:
description: 'Email for the git commit'
description: Email for the git commit
required: true
github-server:
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: ''
destination-repository-username:
description: '[Optional] Username/organization for the destination repository'
required: false
default: ''
target-branch:
description: >-
[Optional] set target branch name for the destination repository. Defaults
to "main"
default: main
required: false
commit-message:
description: >-
[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
create-target-branch-if-needed:
type: boolean
description: >-
[Optional] create target branch if not exist. Defaults to `false`
default: false
required: false
runs:
using: 'docker'
image: 'Dockerfile'
using: docker
image: Dockerfile
args:
- ${{ inputs.source-directory }}
- ${{ inputs.destination-github-username }}
- ${{ inputs.destination-repository-name }}
- ${{ inputs.user-email }}
- '${{ 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.create-target-branch-if-needed }}'
branding:
icon: 'git-commit'
color: 'green'
icon: git-commit
color: green

View File

@ -1,29 +1,175 @@
#!/bin/sh -l
echo "Starts"
FOLDER="$1"
GITHUB_USERNAME="$2"
GITHUB_REPO="$3"
USER_EMAIL="$4"
set -e # if a command fails it stops the execution
set -u # script fails if trying to access to an undefined variable
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}"
CREATE_TARGET_BRANCH_IF_NEEDED="${12}"
if [ -z "$DESTINATION_REPOSITORY_USERNAME" ]
then
DESTINATION_REPOSITORY_USERNAME="$DESTINATION_GITHUB_USERNAME"
fi
if [ -z "$USER_NAME" ]
then
USER_NAME="$DESTINATION_GITHUB_USERNAME"
fi
# 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
echo "[+] Using SSH_DEPLOY_KEY"
# 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"
chmod 600 "$DEPLOY_KEY_FILE"
SSH_KNOWN_HOSTS_FILE="$HOME/.ssh/known_hosts"
ssh-keyscan -H "$GITHUB_SERVER" > "$SSH_KNOWN_HOSTS_FILE"
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"
elif [ -n "${API_TOKEN_GITHUB:=}" ]
then
echo "[+] Using API_TOKEN_GITHUB"
GIT_CMD_REPOSITORY="https://$DESTINATION_REPOSITORY_USERNAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$DESTINATION_REPOSITORY_USERNAME/$DESTINATION_REPOSITORY_NAME.git"
else
echo "::error::API_TOKEN_GITHUB and SSH_DEPLOY_KEY are empty. Please fill one (recommended the SSH_DEPLOY_KEY)"
exit 1
fi
CLONE_DIR=$(mktemp -d)
echo "[+] Git version"
git --version
echo "[+] Enable git lfs"
git lfs install
echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME"
# Setup git
git config --global user.email "$USER_EMAIL"
git config --global user.name "$GITHUB_USERNAME"
git clone "https://$API_TOKEN_GITHUB@github.com/$GITHUB_USERNAME/$GITHUB_REPO.git" "$CLONE_DIR"
git config --global user.name "$USER_NAME"
# workaround for https://github.com/cpina/github-action-push-to-another-repository/issues/103
git config --global http.version HTTP/1.1
{
git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR"
} || {
if [ "$CREATE_TARGET_BRANCH_IF_NEEDED" = "true" ]
then
# Default branch of the repository is cloned. Later on the required branch
# will be created
git clone --single-branch --depth 1 "$GIT_CMD_REPOSITORY" "$CLONE_DIR"
else
false
fi
} || {
echo "::error::Could not clone the destination repository. Command:"
echo "::error::git clone --single-branch --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"
exit 1
}
ls -la "$CLONE_DIR"
# Copy files into the git and deletes all git
find "$CLONE_DIR" | grep -v "^$CLONE_DIR/\.git" | grep -v "^$CLONE_DIR$" | xargs rm -rf # delete all files (to handle deletions)
TEMP_DIR=$(mktemp -d)
# 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",
# including "." and with the exception of ".git/"
mv "$CLONE_DIR/.git" "$TEMP_DIR/.git"
ls -la "$CLONE_DIR"
# $TARGET_DIRECTORY is '' by default
ABSOLUTE_TARGET_DIRECTORY="$CLONE_DIR/$TARGET_DIRECTORY/"
cp -r "$FOLDER"/* "$CLONE_DIR"
echo "[+] Deleting $ABSOLUTE_TARGET_DIRECTORY"
rm -rf "$ABSOLUTE_TARGET_DIRECTORY"
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"
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"
ls -la
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}"
echo "[+] Set directory is safe ($CLONE_DIR)"
# Related to https://github.com/cpina/github-action-push-to-another-repository/issues/64
git config --global --add safe.directory "$CLONE_DIR"
if [ "$CREATE_TARGET_BRANCH_IF_NEEDED" = "true" ]
then
echo "[+] Switch to the TARGET_BRANCH"
# || true: if the $TARGET_BRANCH already existed in the destination repo:
# it is already the current branch and it cannot be switched to
# (it's not needed)
# If the branch did not exist: it switches (creating) the branch
git switch -c "$TARGET_BRANCH" || true
fi
echo "[+] Adding git commit"
git add .
git commit --message "Update from https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA)"
git push origin master
echo "[+] git status:"
git status
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"
echo "[+] Pushing git commit"
# --set-upstream: sets de branch when pushing to a branch that does not exist
git push "$GIT_CMD_REPOSITORY" --set-upstream "$TARGET_BRANCH"