Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactoring s3 cache action #59

Merged
merged 2 commits into from
Apr 18, 2024
Merged

feat: refactoring s3 cache action #59

merged 2 commits into from
Apr 18, 2024

Conversation

lotyp
Copy link
Member

@lotyp lotyp commented Apr 18, 2024

Summary by CodeRabbit

  • Documentation

    • Enhanced the README to include new caching options using S3.
  • New Features

    • Introduced a new GitHub Action for caching artifacts in an S3 bucket, allowing for key customization and efficient storage.
  • Chores

    • Updated GitHub Actions workflows to improve reliability and context handling in automation processes.

Copy link

coderabbitai bot commented Apr 18, 2024

Walkthrough

The recent updates encompass improvements to GitHub Actions workflows and the introduction of a new S3 caching mechanism. These changes refine existing workflows by correcting versions and syntax while also introducing a new action for efficient artifact management through S3. Updates include version adjustments, script refinements, and documentation enhancements to ensure streamlined operations and optimized resource utilization.

Changes

File Path Change Summary
.github/workflows/build-image.yml,
.github/workflows/shellcheck.yml
Updated actions/checkout versions and syntax for workflow files.
.github/workflows/triage.yml Updated apply-labels.yml version from master to v2.0.0.
README.md Added a new action actions/s3/cache for caching artifacts with S3.
actions/s3/cache/README.md,
actions/s3/cache/action.yml,
actions/s3/cache/cache.sh
Introduced S3 cache action with customization options, environment setup, and script enhancements.

Poem

🐇 A Coding Tale by CodeRabbit 🌟

In the realm of code and automated art,
A rabbit danced through lines so smart.
With keystrokes light and scripts so clean,
S3 caches now, a digital dream.
Workflows flow, like a gentle stream,
Under the watchful eye of the coding team!
🌟🐾


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 75eedec and e3de369.
Files selected for processing (1)
  • actions/s3/cache/README.md (1 hunks)
Additional Context Used
LanguageTool (8)
actions/s3/cache/README.md (8)

Near line 27: Possible spelling mistake found.
Context: ...ys - Compress cache files using tar and gzip for efficient storage and transfer <br...


Near line 47: These punctuation marks differ from each other. Use only one if you are ending a sentence.
Context: ... - Type: string - Required - Default: . * s3-bucket-name AWS S3 bucket na...


Near line 51: Possible missing comma found.
Context: ... . * s3-bucket-name AWS S3 bucket name which will be used to save cache to and...


Near line 84: Loose punctuation mark.
Context: ...iables to be set: - AWS_ACCESS_KEY_ID: The AWS access key ID with permissions ...


Near line 85: Loose punctuation mark.
Context: ...the S3 bucket. - AWS_SECRET_ACCESS_KEY: The AWS secret access key associated wi...


Near line 86: Loose punctuation mark.
Context: ...d with the access key ID. - AWS_REGION: The AWS region where the S3 bucket is l...


Near line 166: Possible spelling mistake found.
Context: ...GitHub Actions. For example, using the hashFiles function allows you to create a new ca...


Near line 166: Possible spelling mistake found.
Context: ...new cache when dependencies change. The hashFiles function is specific to GitHub Actions...

Additional comments not posted (1)
actions/s3/cache/README.md (1)

208-212: The limitations section is clear and concise.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added type: enhancement For features and enhancements (feat) type: maintenance For maintenance, refactor and testing (perf, chore, style, revert, refactor, test, build, ci) labels Apr 18, 2024
@lotyp lotyp merged commit 706a892 into master Apr 18, 2024
2 checks passed
@lotyp lotyp deleted the feat/refactor branch April 18, 2024 21:14
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Comment on lines +16 to +28
# S3 / Cache

This GitHub Action allows you to save and restore cache artifacts from an S3 bucket. It provides a convenient way to cache dependencies, build artifacts, or any other files that can be reused across multiple workflow runs, helping to speed up your CI/CD pipeline.

<br>

## 📋 Features

- Save cache to an S3 bucket
- Restore cache from an S3 bucket
- Specify custom cache keys and restore keys
- Compress cache files using tar and gzip for efficient storage and transfer

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure consistent list formatting in the features section.

- * `cache-action`
+ - `cache-action`
- * `cache-path`
+ - `cache-path`
- * `s3-bucket-name`
+ - `s3-bucket-name`
- * `cache-key`
+ - `cache-key`
- * `restore-keys`
+ - `restore-keys`

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
# S3 / Cache
This GitHub Action allows you to save and restore cache artifacts from an S3 bucket. It provides a convenient way to cache dependencies, build artifacts, or any other files that can be reused across multiple workflow runs, helping to speed up your CI/CD pipeline.
<br>
## 📋 Features
- Save cache to an S3 bucket
- Restore cache from an S3 bucket
- Specify custom cache keys and restore keys
- Compress cache files using tar and gzip for efficient storage and transfer
# S3 / Cache
This GitHub Action allows you to save and restore cache artifacts from an S3 bucket. It provides a convenient way to cache dependencies, build artifacts, or any other files that can be reused across multiple workflow runs, helping to speed up your CI/CD pipeline.
<br>
## 📋 Features
- Save cache to an S3 bucket
- Restore cache from an S3 bucket
- Specify custom cache keys and restore keys
- Compress cache files using tar and gzip for efficient storage and transfer

Comment on lines +33 to +86
* `cache-action`

Specify what to do with the cache: save to a s3 bucket or restore from the s3 bucket into `cache_path`.

- Type: string
- Required
- Possible values: save, restore

* `cache-path`

Absolute or relative path to a folder with cache. When `cache-action` is `save` the path itself will not be saved, only the contents of the directory (including all subdirectories). When `cache-action` is `restore` all folders in `cache_path` will be created first and cache will be restored from the S3 bucket into this folder.

- Type: string
- Required
- Default: .

* `s3-bucket-name`

AWS S3 bucket name which will be used to save cache to and restore it from.

- Type: string
- Required

* `cache-key`

A cache key which is used only to save cache to S3 bucket

- Type: string
- Required only when `cache-action` is `save`

* `restore-keys`

An ordered list of keys to use for restoring cache from the s3 bucket

- Type: list of strings
- Required only when `cache-action` is `restore`

You can specify multiple keys by putting each key on its own line:
```yaml
restore-keys: |-
${{ runner.os }}-cache-${{ hashfiles('**/.package-lock.json') }}
${{ runner.os }}-cache
```
The first matching key will be restored.

<br>

## 🌎 Environment Variables

The action requires the following environment variables to be set:

- `AWS_ACCESS_KEY_ID`: The AWS access key ID with permissions to access the S3 bucket.
- `AWS_SECRET_ACCESS_KEY`: The AWS secret access key associated with the access key ID.
- `AWS_REGION`: The AWS region where the S3 bucket is located.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct punctuation and ensure consistent list formatting in the inputs section.

- * `cache-action`
+ - `cache-action`
- * `cache-path`
+ - `cache-path`
- * `s3-bucket-name`
+ - `s3-bucket-name`
- * `cache-key`
+ - `cache-key`
- * `restore-keys`
+ - `restore-keys`
- `AWS_ACCESS_KEY_ID`: The AWS access key ID with permissions to access the S3 bucket.
+ `AWS_ACCESS_KEY_ID`: The AWS access key ID with permissions to access the S3 bucket.
- `AWS_SECRET_ACCESS_KEY`: The AWS secret access key associated with the access key ID.
+ `AWS_SECRET_ACCESS_KEY`: The AWS secret access key associated with the access key ID.
- `AWS_REGION`: The AWS region where the S3 bucket is located.
+ `AWS_REGION`: The AWS region where the S3 bucket is located.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
* `cache-action`
Specify what to do with the cache: save to a s3 bucket or restore from the s3 bucket into `cache_path`.
- Type: string
- Required
- Possible values: save, restore
* `cache-path`
Absolute or relative path to a folder with cache. When `cache-action` is `save` the path itself will not be saved, only the contents of the directory (including all subdirectories). When `cache-action` is `restore` all folders in `cache_path` will be created first and cache will be restored from the S3 bucket into this folder.
- Type: string
- Required
- Default: .
* `s3-bucket-name`
AWS S3 bucket name which will be used to save cache to and restore it from.
- Type: string
- Required
* `cache-key`
A cache key which is used only to save cache to S3 bucket
- Type: string
- Required only when `cache-action` is `save`
* `restore-keys`
An ordered list of keys to use for restoring cache from the s3 bucket
- Type: list of strings
- Required only when `cache-action` is `restore`
You can specify multiple keys by putting each key on its own line:
```yaml
restore-keys: |-
${{ runner.os }}-cache-${{ hashfiles('**/.package-lock.json') }}
${{ runner.os }}-cache
```
The first matching key will be restored.
<br>
## 🌎 Environment Variables
The action requires the following environment variables to be set:
- `AWS_ACCESS_KEY_ID`: The AWS access key ID with permissions to access the S3 bucket.
- `AWS_SECRET_ACCESS_KEY`: The AWS secret access key associated with the access key ID.
- `AWS_REGION`: The AWS region where the S3 bucket is located.
- `cache-action`
Specify what to do with the cache: save to a s3 bucket or restore from the s3 bucket into `cache_path`.
- Type: string
- Required
- Possible values: save, restore
- `cache-path`
Absolute or relative path to a folder with cache. When `cache-action` is `save` the path itself will not be saved, only the contents of the directory (including all subdirectories). When `cache-action` is `restore` all folders in `cache_path` will be created first and cache will be restored from the S3 bucket into this folder.
- Type: string
- Required
- Default: .
- `s3-bucket-name`
AWS S3 bucket name which will be used to save cache to and restore it from.
- Type: string
- Required
- `cache-key`
A cache key which is used only to save cache to S3 bucket
- Type: string
- Required only when `cache-action` is `save`
- `restore-keys`
An ordered list of keys to use for restoring cache from the s3 bucket
- Type: list of strings
- Required only when `cache-action` is `restore`
You can specify multiple keys by putting each key on its own line:
```yaml
restore-keys: |-
${{ runner.os }}-cache-${{ hashfiles('**/.package-lock.json') }}
${{ runner.os }}-cache
```
The first matching key will be restored.
<br>
## 🌎 Environment Variables
The action requires the following environment variables to be set:
`AWS_ACCESS_KEY_ID`: The AWS access key ID with permissions to access the S3 bucket.
`AWS_SECRET_ACCESS_KEY`: The AWS secret access key associated with the access key ID.
`AWS_REGION`: The AWS region where the S3 bucket is located.

Comment on lines +92 to +202
build:
runs-on: ubuntu-latest
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4

- name: 📤 Save cache
uses: wayofdev/gh-actions/actions/s3/cache@v3.0.0
with:
cache-action: save
cache-path: ./node_modules
s3-bucket-name: my-cache-bucket
cache-key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}

...
```

### → Restoring Cache

```yaml
---

name: 🔍 Continuous integration

on: # yamllint disable-line rule:truthy
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4

- name: ♻️ Restore cache
uses: wayofdev/gh-actions/actions/s3/cache@v3.0.0
with:
cache-action: restore
cache-path: ${GITHUB_WORKSPACE}/.cache
s3-bucket-name: my_s3_bucket
restore-keys: |
${{ runner.os }}-cache-${{ hashfiles('**/.package-lock.json') }}
${{ runner.os }}-cache
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}

...
```

In the restore example, the action will attempt to restore the cache using the provided `restore-keys` in the specified order. If a cache hit occurs for a key, the restoration process will stop, and the cached files will be extracted to the `cache-path`.
### → Error Handling

The action will fail if any of the required inputs or environment variables are missing or if an invalid `cache-action` is provided. Appropriate error messages will be logged to help troubleshoot the issue.

### → Creating a Cache Key
A cache key can include any of the contexts, functions, literals, and operators supported by GitHub Actions.

For example, using the `hashFiles` function allows you to create a new cache when dependencies change. The `hashFiles` function is specific to GitHub Actions.

```yaml
cache-key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
```

Additionally, you can use arbitrary command output in a cache key, such as a date or software version:


```yaml
---

name: 🔍 Continuous integration

on: # yamllint disable-line rule:truthy
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get current timestamp
id: get-date
run: |
echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
shell: bash

- name: 📤 Save cache
uses: wayofdev/gh-actions/actions/s3-cache@v1
with:
cache-action: save
cache-path: ${GITHUB_WORKSPACE}/.cache
s3-bucket-name: my_s3_bucket
cache-key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}

...
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce consecutive blank lines in the YAML examples.

- 
- 
+ 

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
### → Saving Cache
```yaml
---
name: 🔍 Continuous integration
on: # yamllint disable-line rule:truthy
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4
- name: 📤 Save cache
uses: wayofdev/gh-actions/actions/s3/cache@v3.0.0
with:
cache-action: save
cache-path: ./node_modules
s3-bucket-name: my-cache-bucket
cache-key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
...
```
### → Restoring Cache
```yaml
---
name: 🔍 Continuous integration
on: # yamllint disable-line rule:truthy
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4
- name: ♻️ Restore cache
uses: wayofdev/gh-actions/actions/s3/cache@v3.0.0
with:
cache-action: restore
cache-path: ${GITHUB_WORKSPACE}/.cache
s3-bucket-name: my_s3_bucket
restore-keys: |
${{ runner.os }}-cache-${{ hashfiles('**/.package-lock.json') }}
${{ runner.os }}-cache
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
...
```
In the restore example, the action will attempt to restore the cache using the provided `restore-keys` in the specified order. If a cache hit occurs for a key, the restoration process will stop, and the cached files will be extracted to the `cache-path`.
### → Error Handling
The action will fail if any of the required inputs or environment variables are missing or if an invalid `cache-action` is provided. Appropriate error messages will be logged to help troubleshoot the issue.
### → Creating a Cache Key
A cache key can include any of the contexts, functions, literals, and operators supported by GitHub Actions.
For example, using the `hashFiles` function allows you to create a new cache when dependencies change. The `hashFiles` function is specific to GitHub Actions.
```yaml
cache-key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
```
Additionally, you can use arbitrary command output in a cache key, such as a date or software version:
```yaml
---
name: 🔍 Continuous integration
on: # yamllint disable-line rule:truthy
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get current timestamp
id: get-date
run: |
echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
shell: bash
- name: 📤 Save cache
uses: wayofdev/gh-actions/actions/s3-cache@v1
with:
cache-action: save
cache-path: ${GITHUB_WORKSPACE}/.cache
s3-bucket-name: my_s3_bucket
cache-key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}
...
```
### → Saving Cache
```yaml
---
name: 🔍 Continuous integration
on: # yamllint disable-line rule:truthy
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4
- name: 📤 Save cache
uses: wayofdev/gh-actions/actions/s3/cache@v3.0.0
with:
cache-action: save
cache-path: ./node_modules
s3-bucket-name: my-cache-bucket
cache-key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
...

→ Restoring Cache

---

name: 🔍 Continuous integration

on:  # yamllint disable-line rule:truthy
  push:

jobs:
  build:
    runs-on: ubuntu-latest
  steps:
    - name: 📦 Check out the codebase
      uses: actions/checkout@v4

    - name: ♻️ Restore cache
      uses: wayofdev/gh-actions/actions/s3/cache@v3.0.0
      with:
        cache-action: restore
        cache-path: ${GITHUB_WORKSPACE}/.cache
        s3-bucket-name: my_s3_bucket
        restore-keys: |
          ${{ runner.os }}-cache-${{ hashfiles('**/.package-lock.json') }}
          ${{ runner.os }}-cache
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: ${{ secrets.AWS_REGION }}

...

In the restore example, the action will attempt to restore the cache using the provided restore-keys in the specified order. If a cache hit occurs for a key, the restoration process will stop, and the cached files will be extracted to the cache-path.

→ Error Handling

The action will fail if any of the required inputs or environment variables are missing or if an invalid cache-action is provided. Appropriate error messages will be logged to help troubleshoot the issue.

→ Creating a Cache Key

A cache key can include any of the contexts, functions, literals, and operators supported by GitHub Actions.

For example, using the hashFiles function allows you to create a new cache when dependencies change. The hashFiles function is specific to GitHub Actions.

cache-key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

Additionally, you can use arbitrary command output in a cache key, such as a date or software version:

---

name: 🔍 Continuous integration

on:  # yamllint disable-line rule:truthy
  push:

jobs:
  build:
    runs-on: ubuntu-latest
  steps:
    - name: Get current timestamp
      id: get-date
      run: |
        echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
      shell: bash

    - name: 📤 Save cache
      uses: wayofdev/gh-actions/actions/s3-cache@v1
      with:
        cache-action: save
        cache-path: ${GITHUB_WORKSPACE}/.cache
        s3-bucket-name: my_s3_bucket
        cache-key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement For features and enhancements (feat) type: maintenance For maintenance, refactor and testing (perf, chore, style, revert, refactor, test, build, ci)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant