Skip to content

A cross-platform (JS-based) Github Action to upload files and/or directories to S3-compatible storage providers.

License

Notifications You must be signed in to change notification settings

wscourge/gha-s3-upload

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

S3 Upload GitHub Action

A cross-platform (Node.js-based) GitHub Action to upload files and/or directories to S3-compatible storage providers.

Usage

All of the following are to be added to your .github/workflows/workflow.yml.

Single file upload

The following will upload a single file.txt file from /tmp/path/to/dir/file.txt absolute path and save it as a path/on/s3/file.txt:

name: Test Run
on: push
jobs:
  lambda:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: prepare a single file
        run: |
          mkdir -p /tmp/path/to/dir
          echo "Inside /tmp/path/to/dir/file.txt" >> /tmp/path/to/dir/file.txt
      - uses: wscourge/gha-s3-upload@main
        with:
          source: '/tmp/path/to/dir/file.txt'
          destination: 'path/on/s3'
          region: ${{ vars.S3_REGION }}
          bucket: 'my-bucket'
          endpoint: ${{ vars.S3_ENDPOINT }}
          access_key_id: ${{ secrets.S3_ACCESS_KEY_ID }}
          secret_access_key: ${{ secrets.S3_SECRET_ACCESS_KEY }}

Recursive directory upload

The following will upload all 4 files from /tmp/path absolute path and save them with a path/on/s3/ prefix:

name: Test Run
on: push
jobs:
  lambda:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: prepare directiories
        run: |
          mkdir -p /tmp/path/to/nested/dir
          echo "Inside /tmp/path/file.txt" >> /tmp/path/file.txt
          echo "Inside /tmp/path/to/file.txt" >> /tmp/path/to/file.txt
          echo "Inside /tmp/path/to/nested/file.txt" >> /tmp/path/to/nested/file.txt
          echo "Inside /tmp/path/to/nested/dir/file.txt" >> /tmp/path/to/nested/dir/file.txt
      - uses: wscourge/gha-s3-upload@main
        with:
          source: '/tmp/path'
          destination: 'path/on/s3'
          region: ${{ vars.S3_REGION }}
          bucket: 'my-bucket'
          endpoint: ${{ vars.S3_ENDPOINT }}
          access_key_id: ${{ secrets.S3_ACCESS_KEY_ID }}
          secret_access_key: ${{ secrets.S3_SECRET_ACCESS_KEY }}

Action inputs

Sensitive information, especially key_id and secret_access_key, should be set as encrypted secrets — otherwise, they'll be public to anyone browsing your repository's source code.

variable required description default
access_key_id yes Your S3 Access Key. See this.
secret_access_key yes Your S3 Secret Access Key. See this.
bucket yes The name of the S3 bucket.
region yes The name of the S3 region. "us-east-1"
source yes The local directory or file you wish to upload to S3.
destination yes The destination directory in S3.
endpoint no The endpoint URI to send requests to. See this.

Notes:

  • Set destination to an empty string "" to upload to S3's root directory.
  • Set destination to a filename, e.g.: "me.json" (or "path/to/me.json") to rename a single file that is being uploaded.
  • Do not set the following backslash at the end of source and destination directory names.

Development

Initial Setup

After you've cloned the repository to your local machine or codespace, you'll need to perform some initial setup steps before you can develop your action.

Note

You'll need to have a reasonably modern version of Node.js handy (20.x or later should work!). If you are using a version manager like nodenv or nvm, this template has a .node-version file at the root of the repository that will be used to automatically switch to the correct version when you cd into the repository. Additionally, this .node-version file is used by GitHub Actions in any actions/setup-node actions.

  1. 🛠️ Install the dependencies

    npm install
  2. 🏗️ Package the TypeScript for distribution

    npm run bundle
  3. ✅ Run the tests

    $ npm test
    
    PASS  ./index.test.js
      ✓ throws invalid number (3ms)
      ✓ wait 500 ms (504ms)
      ✓ test runs (95ms)
    
    ...

About

A cross-platform (JS-based) Github Action to upload files and/or directories to S3-compatible storage providers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published