/google-drive-upload-action

Github action that uploads files to Google Drive and returns the link to the uploaded file

Primary LanguageGoMIT LicenseMIT

build Go Report Card

google-drive-upload-action

Github action that uploads files to Google Drive and saves a link to the uploaded file in GITHUB_OUTPUT. This only works with a Google Service Account!

Thanks to Team Tumbleweed for developing the initial version of this actions package.

To make a GSA go to the Credentials Dashboard. 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:

base64 credentials.json -w0

On mac this the base64 by default opts for -w as 0, you can skip and just use base64 without any params.

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.

Inputs

filename

Required: YES.

The name of the file you want to upload. Wildcards can be used to upload more than one file.

name

Required: NO

The name you want the file to have in Google Drive. If this input is not provided, it will use only the filename of the source path. It will be ignored if there are more than one file to be uploaded.

overwrite

Required: NO

If you want to overwrite the filename with existing file, it will use the target filename.

mimeType

Required: NO

file MimeType. If absent, Google Drive will attempt to automatically detect an appropriate value.

useCompleteSourceFilenameAsName

Required: NO

If true, the target file name will be the complete source filename and name parameter will be ignored.

mirrorDirectoryStructure

Required: NO

If true, the directory structure of the source file will be recreated relative to folderId.

namePrefix

Required: NO

Prefix to be added to target filename.

folderId

Required: YES.

The ID of the folder you want to upload to.

credentials

Required: YES.

A base64 encoded string with the GSA credentials.

Outputs

UPLOADED_FILE_URL

Required: NO.

The url of the uploaded file.

Usage Example

Simple Workflow 1

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.

# .github/workflows/main.yml
name: Main
on: [push]

jobs:
  my_job:
    runs-on: ubuntu-latest

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

      - name: Archive files
        run: |
          sudo apt-get update
          sudo apt-get install zip
          zip -r archive.zip *

      - name: Upload to gdrive
        uses: kingahmedino/google-drive-upload-git-action@main
        with:
          credentials: ${{ secrets.credentials }}
          filename: "archive.zip"
          folderId: ${{ secrets.folderId }}
          name: "documentation.zip" # optional string
          overwrite: "true" # optional boolean
      - name: Make Directory Structure
        run: |
          mkdir -p w/x/y
          date +%s > w/x/y/z
      - name: Mirror Directory Structure
        uses: kingahmedino/google-drive-upload-git-action@main
        with:
          credentials: ${{ secrets.DRIVE_CREDENTIALS }}
          filename: w/x/y/z
          folderId: ${{ secrets.folderId }}
          overwrite: "true"
          mirrorDirectoryStructure: "true"

Simple Workflow 2

In this example, we do the same thing as above but we get the url to the uploaded file from GITHUB_OUTPUT for use in subsequent steps in our workflow.

# .github/workflows/main.yml
name: Main
on: [push]

jobs:
  my_job:
    runs-on: ubuntu-latest

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

      - name: Archive files
        run: |
          sudo apt-get update
          sudo apt-get install zip
          zip -r archive.zip *

      - name: Upload to gdrive
        uses: kingahmedino/google-drive-upload-git-action@main
        with:
          credentials: ${{ secrets.credentials }}
          filename: "archive.zip"
          folderId: ${{ secrets.folderId }}
          name: "documentation.zip" # optional string
          overwrite: "true" # optional boolean
        id: upload_build
        
      - name: Comment upload URL
        uses: thollander/actions-comment-pull-request@v2
        with:
          message: |
            You can access artifacts from this build here: ${{ steps.upload_build.outputs.UPLOADED_FILE_URL }}