Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
savi2w committed Apr 30, 2023
0 parents commit 80513de
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM google/cloud-sdk:alpine

LABEL "com.github.actions.name"="GCP Storage Sync"
LABEL "com.github.actions.description"="Sync a directory to an GCP Storage repository"
LABEL "com.github.actions.icon"="refresh-cw"
LABEL "com.github.actions.color"="blue"

LABEL version="1.0.0"
LABEL repository="https://github.com/savi2w/gcp-storage-sync"
LABEL homepage="https://savi2w.moe/"
LABEL maintainer="Weslen Guerreiro <savi2w@icloud.com>"

ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Weslen Guerreiro

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.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# GitHub Action to GCP Storage Bucket

This simple action uses the [gsutil tool](https://cloud.google.com/storage/docs/gsutil) to sync a directory (either from your repository or generated during your workflow) with a remote GCP Storage bucket

## Usage

### `workflow.yml`

Place in a `.yml` file such as this one in your `.github/workflows` folder. [Refer to the documentation on workflow YAML syntax here.](https://help.github.com/en/articles/workflow-syntax-for-github-actions)

Mostly [`gsutil rsync` flags](https://cloud.google.com/storage/docs/gsutil/commands/rsync) are optional to allow for maximum customizability and must be provided by you via `args:`

#### Example

- `-d` mirror source and destination. [See.](https://cloud.google.com/storage/docs/gsutil/commands/rsync#using--d-option-with-caution-to-mirror-source-and-destination)

```yaml
name: gcp-storage-sync

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: savi2w/gcp-storage-sync@main
with:
args: -d
env:
GCP_SERVICE_ACCOUNT_KEY_FILE: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY_FILE }}
GCP_STORAGE_BUCKET: ${{ secrets.GCP_STORAGE_BUCKET }}
SOURCE_DIR: "public"
```

### Configuration

The following settings must be passed as environment variables as shown in the example. Sensitive information, especially `GCP_SERVICE_ACCOUNT_KEY_FILE`, should be [set as encrypted secrets](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables) — otherwise, they'll be public to anyone browsing your repository's source code and CI logs

| Key | Value | Suggested Type | Required | Default |
| ------------- | ------------- | ------------- | ------------- | ------------- |
| `GCP_SERVICE_ACCOUNT_KEY_FILE` | Your JSON GCP service account key file. [More info here](https://cloud.google.com/storage/docs/authentication/managing-hmackeys) | `Secret ENV` | **Yes** | N/A |
| `GCP_STORAGE_BUCKET` | Your GCP Storage bucket name For example, `my-project` | `Secret ENV` | **Yes** | N/A |
| `SOURCE_DIR` | The local directory (or file) you wish to sync/upload to GCP Storage. For example, `public` | `ENV` | **Yes** | N/A |
| `DEST_DIR` | The directory inside of the GCP Storage bucket you wish to sync/upload to. For example, `my_project/assets`. Defaults to the root of the bucket | `ENV` | No | `/` (root of bucket) |

## License

This project is distributed under the [MIT license](LICENSE)
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: "GCP Storage Sync"
description: "Sync a directory to an GCP Storage repository"
author: savi2w
runs:
using: docker
image: Dockerfile
branding:
icon: refresh-cw
color: blue
21 changes: 21 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

set -e

if [ -z "$GCP_STORAGE_BUCKET" ]; then
echo "GCP_STORAGE_BUCKET is not set. Quitting."
exit 1
fi

if [ -z "$GCP_SERVICE_ACCOUNT_KEY_FILE" ]; then
echo "GCP_SERVICE_ACCOUNT_KEY_FILE is not set. Quitting."
exit 1
fi

echo $GCP_SERVICE_ACCOUNT_KEY_FILE > json_file.json
gcloud auth activate-service-account --key-file json_file.json

sh -c "gsutil -m rsync $* -r ${SOURCE_DIR} gs://${GCP_STORAGE_BUCKET}/${DEST_DIR}"

gcloud auth revoke --all
shred -zvu -n 5 json_file.json

0 comments on commit 80513de

Please sign in to comment.