Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Gherzan <[email protected]>
  • Loading branch information
agherzan committed Feb 22, 2022
0 parents commit 7d13428
Show file tree
Hide file tree
Showing 8 changed files with 666 additions and 0 deletions.
1 change: 1 addition & 0 deletions COPYING.MIT
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: Andrei Gherzan <[email protected]>
#
# SPDX-License-Identifier: MIT

FROM alpine:3.15.0

RUN apk add --no-cache \
bash \
bats \
git \
openssh-client

RUN mkdir -p /root/.ssh
RUN chmod 700 /root/.ssh

# Inclure bats support libraries
RUN git clone https://github.com/bats-core/bats-support.git \
--branch v0.3.0 --depth 1 \
/tests/test_helper/bats-support
RUN git clone https://github.com/bats-core/bats-assert.git \
--branch v2.0.0 --depth 1 \
/tests/test_helper/bats-assert

ADD tests/* tests/

ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
1 change: 1 addition & 0 deletions FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [agherzan]
9 changes: 9 additions & 0 deletions LICENSES/MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
143 changes: 143 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!--
SPDX-FileCopyrightText: Andrei Gherzan <[email protected]>
SPDX-License-Identifier: MIT
-->

# Git Mirror-Me Action

This GitHub action provides the ability to mirror a repository to any other git
repository over SSH. It can be used with repositories on GitHub, GitLab,
Bitbucket, etc.

Why "Me"? The name derives from the action's ability to default the source
repository to the one in which the action is triggered.

## Inputs

### `source-repository`

* Sets the URL of the source repository for the mirror (git push) operation.
* Required: no.
* Defaults: value based on the GitHub environment - `GITHUB_SERVER_URL/GITHUB_REPOSITORY`.

### `destination-repository`

* Sets the URL of the destination repository for the mirror (git push) operation.
* Required: yes.

### `dry-run`

* Run the git push operation as a dry run (not affecting the destination). It
is useful for testing purposes.
* Required: no.
* Default: `false`.
* Valid values: `true`, `false`.

## Environment variables

### `SRC_REPO`

* Same as `source-repository` input.
* Overridden by the input value.

### `DEST_REPO`

* Same as `destination-repository` input.
* Overridden by the input value.

### `DRY_RUN`

* Same as `dry-run` input.
* Overridden by the input value.

### `SSH_PRIVATE_KEY`

* The SSH private key used for SSH authentication during git push operation.
Make sure it is not protected by a password. Store the key as
[an encrypted secret](https://docs.github.com/en/actions/reference/encrypted-secrets)
and reference it as part of the workflow configuration file.
* Required: no.

### `SSH_KNOWN_HOSTS`

* Sets the SSH `known_hosts` file. When defined, `StrictHostKeyChecking` is
enabled. Otherwise, `StrictHostKeyChecking` is disabled. Store this as
[an encrypted secret](https://docs.github.com/en/actions/reference/encrypted-secrets)
and reference it as part of the workflow configuration file.
* Required: no.

## Sample configurations

### Workflow for mirroring this repository on push/delete/create

```
Name: Mirror Repository
on: [ push, delete, create ]
# We want only one mirror workflow at a time.
concurrency:
group: git-mirror-me
jobs:
git-mirror:
runs-on: ubuntu-latest
steps:
- uses: agherzan/git-mirror-me-action@v1
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }}
with:
destination-repository: "[email protected]:foo/bar.git"
```

### Workflow for mirroring an explicit repository on push

```
Name: Mirror Repository
on: [ push ]
# We want only one mirror workflow at a time.
concurrency:
group: git-mirror-me
jobs:
git-mirror:
runs-on: ubuntu-latest
steps:
- uses: agherzan/git-mirror-me-action@v1
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }}
with:
source-repository: "[email protected]:foo/bar.git"
destination-repository: "[email protected]:foo/bar.git"
```

## Tests

The implementation is augmented with tests written using the
[bats](https://github.com/bats-core/bats-core) testing framework. To run the
tests, build a local docker image from the included [Dockerfile](Dockerfile)
and run the tests in a container:

```
docker build .. -t git-mirror-me-action
docker run --rm -ti --entrypoint bats git-mirror-me-action --verbose-run /tests/entrypoint.bats
```

## Contributing

You can send patches using
[GitHub pull requests](https://github.com/agherzan/git-mirror-me-action/pulls).

## Maintainers

* Andrei Gherzan `<andrei at gherzan.com>`

## LICENSE

The repository is [reuse](https://reuse.software/) compliant. This project is
released under the [MIT](COPYING.MIT) license.
34 changes: 34 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: Andrei Gherzan <[email protected]>
#
# SPDX-License-Identifier: MIT

name: 'Mirror a git repository over SSH'
description: >
This GitHub action provides the ability to mirror a repository to any other git
repository over SSH.
branding:
icon: 'refresh-cw'
color: 'green'
inputs:
source-repository:
description: 'SSH URL of the destination repository.'
required: false
default: "auto"
destination-repository:
description: 'SSH URL of the destination repository.'
required: true
default: ''
dry-run:
description: 'Set to "true" to execute a dry run git push.'
required: false
default: 'false'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- --source-repository
- ${{ inputs.source-repository }}
- --destination-repository
- ${{ inputs.destination-repository }}
- --dry-run
- ${{ inputs.dry-run }}
Loading

0 comments on commit 7d13428

Please sign in to comment.