google-github-actions / upload-cloud-storage

A GitHub Action for uploading files to a Google Cloud Storage (GCS) bucket.
https://cloud.google.com/storage
Apache License 2.0
206 stars 49 forks source link
actions gcp gcs github-actions google-cloud google-cloud-platform google-cloud-storage

upload-cloud-storage

The upload-cloud-storage GitHub Action uploads files to a Google Cloud Storage (GCS) bucket.

Paths to files that are successfully uploaded are set as output variables and can be used in subsequent steps.

This is not an officially supported Google product, and it is not covered by a Google Cloud support contract. To report bugs or request features in a Google Cloud product, please contact Google Cloud support.

Prerequisites

Usage

⚠️ WARNING! The Node.js runtime has known issues with unicode characters in filepaths on Windows. There is nothing we can do to fix this issue in our GitHub Action. If you use unicode or special characters in your filenames, please use gsutil or gcloud to upload instead.

For uploading a file

jobs:
  job_id:
    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
    - id: 'checkout'
      uses: 'actions/checkout@v4'

    - id: 'auth'
      uses: 'google-github-actions/auth@v2'
      with:
        workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
        service_account: 'my-service-account@my-project.iam.gserviceaccount.com'

    - id: 'upload-file'
      uses: 'google-github-actions/upload-cloud-storage@v2'
      with:
        path: '/path/to/file'
        destination: 'bucket-name'

    # Example of using the output
    - id: 'uploaded-files'
      uses: 'foo/bar@v1'
      env:
        file: '${{ steps.upload-file.outputs.uploaded }}'

The file will be uploaded to gs://bucket-name/file

For uploading a folder

jobs:
  job_id:
    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
    - id: 'checkout'
      uses: 'actions/checkout@v4'

    - id: 'auth'
      uses: 'google-github-actions/auth@v2'
      with:
        workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
        service_account: 'my-service-account@my-project.iam.gserviceaccount.com'

    - id: 'upload-folder'
      uses: 'google-github-actions/upload-cloud-storage@v2'
      with:
        path: '/path/to/folder'
        destination: 'bucket-name'

    # Example of using the output
    - id: 'uploaded-files'
      uses: 'foo/bar@v1'
      env:
        files: '${{ steps.upload-folder.outputs.uploaded }}'

Destination Filenames

If the folder has the following structure:

.
└── myfolder
    ├── file1
    └── folder2
        └── file2.txt

Default Configuration

With default configuration

- id: 'upload-files'
  uses: 'google-github-actions/upload-cloud-storage@v2'
  with:
    path: 'myfolder'
    destination: 'bucket-name'

The files will be uploaded to gs://bucket-name/myfolder/file1,gs://bucket-name/myfolder/folder2/file2.txt

Optionally, you can also specify a prefix in destination.

- id: 'upload-files'
  uses: 'google-github-actions/upload-cloud-storage@v2'
  with:
    path: 'myfolder'
    destination: 'bucket-name/myprefix'

The files will be uploaded to gs://bucket-name/myprefix/myfolder/file1,gs://bucket-name/myprefix/myfolder/folder2/file2.txt

Upload to bucket root

To upload myfolder to the root of the bucket, you can set parent to false. Setting parent to false will omit path when uploading to bucket.

- id: 'upload-files'
  uses: 'google-github-actions/upload-cloud-storage@v2'
  with:
    path: 'myfolder'
    destination: 'bucket-name'
    parent: false

The files will be uploaded to gs://bucket-name/file1,gs://bucket-name/folder2/file2.txt

If path was set to myfolder/folder2, the file will be uploaded to gs://bucket-name/file2.txt

Optionally, you can also specify a prefix in destination.

- id: 'upload-files'
  uses: 'google-github-actions/upload-cloud-storage@v2'
  with:
    path: 'myfolder'
    destination: 'bucket-name/myprefix'
    parent: false

The files will be uploaded to gs://bucket-name/myprefix/file1,gs://bucket-name/myprefix/folder2/file2.txt

Glob Pattern

You can specify a glob pattern like

- id: 'upload-files'
  uses: 'google-github-actions/upload-cloud-storage@v2'
  with:
    path: 'myfolder'
    destination: 'bucket-name'
    glob: '**/*.txt'

This will particular pattern will match all text files within myfolder.

In this case, myfolder/folder2/file2.txt is the only matched file and will be uploaded to gs://bucket-name/myfolder/folder2/file2.txt.

If parent is set to false, it wil be uploaded to gs://bucket-name/folder2/file2.txt.

Inputs

Outputs

List of successfully uploaded file(s).

For example:

- id: 'upload-file'
  uses: 'google-github-actions/upload-cloud-storage@v2'
  with:
    path: '/path/to/file'
    destination: 'bucket-name/file'

will be available in future steps as the output "uploaded":

- id: 'publish'
  uses: 'foo/bar@v1'
  env:
    file: '${{ steps.upload-file.outputs.uploaded }}'

Authorization

There are a few ways to authenticate this action. The caller must have permissions to access the secrets being requested.

Via google-github-actions/auth

Use google-github-actions/auth to authenticate the action. You can use Workload Identity Federation or traditional Service Account Key JSON authentication.

jobs:
  job_id:
    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
    - id: 'auth'
      uses: 'google-github-actions/auth@v2'
      with:
        workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
        service_account: 'my-service-account@my-project.iam.gserviceaccount.com'

    - uses: 'google-github-actions/upload-cloud-storage@v2'

Via Application Default Credentials

If you are hosting your own runners, and those runners are on Google Cloud, you can leverage the Application Default Credentials of the instance. This will authenticate requests as the service account attached to the instance. This only works using a custom runner hosted on GCP.

jobs:
  job_id:
    steps:
    - id: 'upload-file'
      uses: 'google-github-actions/upload-cloud-storage@v2'

The action will automatically detect and use the Application Default Credentials.