Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@ on: [push, pull_request]
name: CI
jobs:
go-build:
strategy:
matrix:
go-version: [1.16]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- name: Install Go
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
go-version: 1.17
-
name: Checkout code
uses: actions/checkout@v2
- name: Build
-
name: Build
run: go build .

docker-build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Build Container
run: docker build -f Dockerfile .
-
name: Checkout
uses: actions/checkout@v2
-
name: Setup QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Build and push
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t gdrive-upload-action \
.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@

# End of https://www.gitignore.io/api/go

.DS_Store
.idea/
creds.json
gdrive-upload-action

# Env files
.envrc
.env
/.gitignore
51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Contribution user guide

You'll need to [download the Go binary][go-binary] to build and test this application.

You may also need to follow the user guide in the README.md to obtain the
Google service account to interact with the API with your locally built app.

## Check out the repo

```bash
git clone mathisve/gdrive-upload-action
```

## Run the tests

You may want to run the tests before you start development, and make sure all tests are passing.

```bash
go test -v
```

## Build the app locally

You can build the app locally with either `go run main.go` or `go build .`.

## Running the app locally

The `go run` method is very useful during development, as you can make sure
new or altered functionality works end-to-end.

In order for the githubactions.GitInput functions to work, you'll need to
export the needed env variables. E.g if using [a direnv `.envrc` file][direnv]:

```bash
export INPUT_FILENAME=README.md
export INPUT_ENCODED=true
export INPUT_CREDENTIALS=...some-base64-service-account-json...
export INPUT_FOLDERID=...some-gdrive-folder-id...
```

Then, run the app, and examine the output

```bash
go run main.go
::add-mask::...
::add-mask::...
::debug::Creating file README.md in folder 1cPZfHTv4Btz-wazqowfEPurP4Ede_zyv
```

[go-binary]: https://go.dev/dl/
[direnv]: https://direnv.net/
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM golang:1.16
FROM golang:1.18-alpine as BUILD

WORKDIR /src
COPY . .
WORKDIR /src/
COPY . /src/

ENV GO111MODULE=on
RUN go mod tidy
RUN CGO_ENABLED=0 go build -o /bin/app .

RUN go build -o /bin/action

ENTRYPOINT ["/bin/action"]
FROM alpine
COPY --from=BUILD /bin/app /bin/app
ENTRYPOINT [ "/bin/app" ]
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2021-Present TTW Software Team
Copyright 2021-Present Mathis Van Eetvelde

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:

Expand Down
103 changes: 72 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
[![build](https://github.com/team-tumbleweed/gdrive-upload-action/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/team-tumbleweed/gdrive-upload-action/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/team-tumbleweed/gdrive-upload-action)](https://goreportcard.com/report/github.com/team-tumbleweed/gdrive-upload-action)
[![build](https://github.com/mathisve/gdrive-upload-action/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/mathisve/gdrive-upload-action/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/mathisve/gdrive-upload-action)](https://goreportcard.com/report/github.com/mathisve/gdrive-upload-action)

# gdrive-upload-action
Github action that uploads files to Google Drive.
**This only works with a Google Service Account!**
# Github action that uploads files to Google Drive

To make a GSA go to the [Credentials Dashboard](https://console.cloud.google.com/apis/credentials). You will need to download the **.json key** and base64 encode it. You will use this string as the `credentials` input. To convert the *json* file to base64 without having to use an online tool (which is insecure), use this command:
## User guide

`echo -n $(cat credentials.json)| base64 -w 0`
### Service account required

You will also need to **share the drive with the servie account.** To do this, just share the folder like you would normally with a friend, except you share it with the service account email address. Additionally you will need to give the service account acccess to the google drive API.
Go to `https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project={PROJECT_ID}`. Where `{PROJECT_ID}` is the id of your GCP project. Find more info about that [here.](https://support.google.com/googleapi/answer/7014113?hl=en)
**Only works with a Google Service Account!**

# Inputs
To make a Google Service Account, go to the [Credentials Dashboard](https://console.cloud.google.com/apis/credentials) and press `CREATE CREDENTIALS`, then click on `Service Account`. After creating the SA, go back to the Credentials Dashboard, click on the SA you just created, click on the `KEYS` tabs. Then click on `ADD KEY`, `Create New Key` and select `json`.

## ``filename``
Required: **YES**.
### Encoded credentials

The name of the file you want to upload.
1. Encode the credentials.

## ``name``
Required: **NO**
`cat credentials.json | base64`

The name you want the file to have in Google Drive. If this input is not provided, it will use the `filename`.
2. Create a new Github secret called `credentials` and copy the output of the previous command into this secret.

## ``folderId``
Required: **YES**.
3. Use this secret as your credentials input in your workflow.

The [ID of the folder](https://ploi.io/documentation/database/where-do-i-get-google-drive-folder-id) you want to upload to.
### Plaintext credentials

## ``credentials``
Required: **YES**.
1. Create a new Github secret called `credentials` paste the contents of the credentials file you just downloaded in it.

A base64 encoded string with the [GSA credentials](https://stackoverflow.com/questions/46287267/how-can-i-get-the-file-service-account-json-for-google-translate-api/46290808).
2. Use this secret as your credentials input in your workflow.

3. Set the `encoded` input to `false` in your workflow.

# Usage Example

## Simple Workflow
You will also need to **share the drive with the service account.** To do this, just share the folder like you would normally with a friend, except you share it with the Service Account email address.

Additionally you need to enable the Google Drive API for your GCP project. Do this by going [here](https://console.cloud.google.com/marketplace/product/google/drive.googleapis.com) and pressing `ENABLE`.

## Example: Simple Workflow

In this example we stored the folderId and credentials as action secrets. This is highly recommended as leaking your credentials key will allow anyone to use your service account.

```yaml
# .github/workflows/main.yml
name: Main
on: [push]
on:
push:
branches:
- 'master'
- 'main'
- '!test'

jobs:
my_job:
gdrive-upload:
runs-on: ubuntu-latest

steps:

- name: Checkout code
uses: actions/checkout@v2

Expand All @@ -60,10 +62,49 @@ jobs:
zip -r archive.zip *

- name: Upload to gdrive
uses: team-tumbleweed/gdrive-upload-action@main
uses: mathisve/gdrive-upload-action@main
with:
filename: "archive.zip"
name: "documentation.zip"
filename: archive.zip
name: documentation.zip
folderId: ${{ secrets.folderId }}
credentials: ${{ secrets.credentials }}
encoded: false
```

## Inputs

### ``filename``
Required: **YES**

The name of the file you want to upload.

### ``name``

Required: **NO**

The name you want the file to have in Google Drive. If this input is not provided, it will use the `filename`.

### ``folderId``
Required: **YES**

The [ID of the folder](https://ploi.io/documentation/database/where-do-i-get-google-drive-folder-id) you want to upload to.

### ``credentials``
Required: **YES**

A string with the [GSA credentials](https://stackoverflow.com/questions/46287267/how-can-i-get-the-file-service-account-json-for-google-translate-api/46290808).
This string should be base64 encoded. If it is not, set the `encoded` input to `false`.

### ``encoded``
Required: **NO**

Whether or not the credentials string is base64 encoded. Defaults to `true`.

### ``overwrite``
Required: **NO**

If you want to overwrite all existing files in the drive folder that match the given `name`, with the current file content. Defaults to `false`

## Contributing

For contribution user guide see CONTRIBUTING.md
10 changes: 8 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ inputs:
description: 'the Id of the parent folder you want to upload the file in'
required: true
credentials:
description: 'the service account credentials encoded in base64'
description: 'the service account credentials. Can be base64 encoded.'
required: true
encoded:
description: 'true if the credentials are base64 encoded. false if they are not.'
required: false
overwrite:
description: 'set to true to update an existing file with the same name, or false to create a new file.'
required: false

runs:
using: docker
image: Dockerfile
image: Dockerfile
17 changes: 14 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
module gdrive-upload-action

go 1.16
go 1.18

require (
cloud.google.com/go v0.75.0 // indirect
github.com/sethvargo/go-githubactions v0.3.0
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 // indirect
google.golang.org/api v0.40.0
)

require (
cloud.google.com/go v0.75.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
go.opencensus.io v0.22.5 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 // indirect
golang.org/x/text v0.3.4 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210226172003-ab064af71705 // indirect
google.golang.org/grpc v1.35.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)
Loading