github Action to push files into another Github repository
Go to file
Carles Pina i Estany 47ce62aef3 Updates README.md
2022-06-09 08:58:06 +02:00
action.yml Rename so it can be published (hopefully) 2022-01-20 23:47:19 +01:00
Dockerfile Use repository stable 2022-05-27 09:24:11 +02:00
entrypoint.sh Use the correct variable on git clone of the destination repository 2022-05-27 09:19:02 +02:00
LICENSE Initial commit 2020-03-29 16:37:03 +02:00
README.md Updates README.md 2022-06-09 08:58:06 +02:00

github-action-push-to-another-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.

Flow:

The example repository has a MarkDown file main.md), during the GitHub Actions flow it executes build.sh and the output/ directory (configurable via source-directory appears in the output repository.

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:

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.

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.

user-email (argument)

The email that will be used for the commit in the destination-repository-name.

user-name (argument) [optional]

The name that will be used for the commit in the destination-repository-name. If not specified, the destination-github-username will be used instead.

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.

target-branch (argument) [optional]

The branch name for the destination repository. It defaults to main.

commit-message (argument) [optional]

The commit message to be used in the output repository. Optional and defaults to "Update from $REPOSITORY_URL@commit".

The string ORIGIN_COMMIT is replaced by $REPOSITORY_URL@commit.

target-directory (argument) [optional]

The directory to wipe and replace in the target repository. Defaults to wiping the entire repository

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.

⚠️ : The "Personal Access Token" that you just generated gives access to any repository to which you have access (it's not possible to restrict it to one repository). Technically anyone with write access to a repository where the token is made available via "Add a new secret" (next step), might manage to access it. The action also uses the token; you can verify how it is used in entrypoint.sh . Possible work arounds if you don't want to use the personal access token:

**News: ** new branch https://github.com/cpina/github-action-push-to-another-repository/tree/ssh-deploy-key allowing to use SSH_DEPLOY_KEYS. I will do some more testing and write documentation soon.

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

      - name: Pushes to another repository
        uses: cpina/github-action-push-to-another-repository@main
        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
          target-branch: main

Working example:

https://github.com/cpina/push-to-another-repository-example/blob/main/.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