cache/examples.md

233 lines
6.7 KiB
Markdown
Raw Normal View History

2019-10-31 11:36:50 -04:00
# Examples
2025-04-11 13:10:01 +05:30
- [Google Cloud Storage Cache](#google-cloud-storage-cache)
- [Basic Example](#basic-example)
- [Separate Restore/Save Actions](#separate-restoresave-actions)
- [Advanced GCS Cache Examples](#advanced-gcs-cache-examples)
- [Cross-Repository Caching](#cross-repository-caching)
- [Using Workload Identity Federation](#using-workload-identity-federation-recommended-for-production)
- [Language-Specific Caching Examples](#language-specific-caching-examples)
2025-04-11 13:10:01 +05:30
## Google Cloud Storage Cache
2025-04-11 13:10:01 +05:30
Using Google Cloud Storage (GCS) as a cache backend provides several advantages:
2025-04-11 13:10:01 +05:30
- **Larger storage**: Store caches beyond GitHub's 10GB repository limit
- **Cross-repository access**: Share caches between different repositories
- **Custom retention**: Control cache lifecycle with GCS retention policies
- **Fallback mechanism**: Automatically falls back to GitHub cache if GCS is unavailable
2025-04-11 13:10:01 +05:30
The following examples show how to configure GCS caching in your workflows.
2025-04-11 13:10:01 +05:30
### Basic Example
2019-10-31 11:36:50 -04:00
```yaml
2025-04-11 13:10:01 +05:30
name: Build with GCS Cache
2025-04-11 13:10:01 +05:30
on: push
2025-04-11 13:10:01 +05:30
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write' # Required for GCP workload identity federation
2025-04-11 13:10:01 +05:30
steps:
- uses: actions/checkout@v4
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
# Set up Google Cloud authentication
- id: auth
uses: google-github-actions/auth@v2
with:
# Using Service Account Key JSON
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
# Alternatively, use Workload Identity Federation (more secure)
# workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
# service_account: ${{ secrets.WIF_SERVICE_ACCOUNT }}
- name: Cache Dependencies
id: cache-deps
uses: danySam/gcs-cache@v1
with:
path: |
~/.npm
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
gcs-bucket: my-github-cache-bucket
2025-04-11 13:10:01 +05:30
- name: Install Dependencies
if: steps.cache-deps.outputs.cache-hit != 'true'
run: npm ci
2025-04-11 13:10:01 +05:30
- name: Build
run: npm run build
2021-12-30 11:54:15 +01:00
```
2025-04-11 13:10:01 +05:30
### Separate Restore/Save Actions
2022-02-22 10:21:09 -08:00
2025-04-11 13:10:01 +05:30
For more flexible control, you can use the `restore` and `save` actions separately:
2022-02-22 10:21:09 -08:00
```yaml
2025-04-11 13:10:01 +05:30
name: Build with GCS Cache (Separate Restore/Save)
2025-04-11 13:10:01 +05:30
on: push
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write' # Required for GCP workload identity federation
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
steps:
- uses: actions/checkout@v4
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
# Set up Google Cloud authentication
- id: auth
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
# Or use workload identity federation
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
- name: Restore Dependencies from Cache
id: cache-deps-restore
uses: danySam/gcs-cache/restore@v1
with:
path: |
~/.npm
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
gcs-bucket: my-github-cache-bucket
2025-04-11 13:10:01 +05:30
- name: Install Dependencies
if: steps.cache-deps-restore.outputs.cache-hit != 'true'
run: npm ci
2025-04-11 13:10:01 +05:30
- name: Build
run: npm run build
2025-04-11 13:10:01 +05:30
- name: Save Dependencies to Cache
id: cache-deps-save
uses: danySam/gcs-cache/save@v1
with:
path: |
~/.npm
node_modules
key: ${{ steps.cache-deps-restore.outputs.cache-primary-key }}
gcs-bucket: my-github-cache-bucket
```
2025-04-11 13:10:01 +05:30
### Advanced GCS Cache Examples
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
#### Cross-Repository Caching
2025-04-11 13:10:01 +05:30
Share caches across multiple repositories using the same GCS bucket:
2019-10-31 11:36:50 -04:00
```yaml
2025-04-11 13:10:01 +05:30
name: Build with Shared GCS Cache
2020-07-02 09:43:52 -04:00
2025-04-11 13:10:01 +05:30
on: push
2025-04-11 13:10:01 +05:30
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
2020-07-02 09:43:52 -04:00
2025-04-11 13:10:01 +05:30
steps:
- uses: actions/checkout@v4
2019-10-31 11:36:50 -04:00
2025-04-11 13:10:01 +05:30
# Set up Google Cloud authentication
- uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
2025-04-11 13:10:01 +05:30
- name: Shared Cross-Repo Cache
id: shared-cache
uses: danySam/gcs-cache@v1
with:
2025-04-11 13:10:01 +05:30
path: |
~/.gradle/caches
~/.gradle/wrapper
key: shared-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
2025-04-11 13:10:01 +05:30
shared-gradle-
gcs-bucket: shared-company-cache-bucket
gcs-path-prefix: gradle-cache # Optional: organize caches in the bucket
2019-11-13 10:55:05 -05:00
2025-04-11 13:10:01 +05:30
# Rest of your workflow
2019-11-13 10:55:05 -05:00
```
2025-04-11 13:10:01 +05:30
#### Using Workload Identity Federation (Recommended for Production)
2019-11-13 10:55:05 -05:00
2025-04-11 13:10:01 +05:30
For production environments, Google recommends [Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation) over service account keys for more secure authentication. This approach eliminates the need to manage long-lived service account keys:
2019-11-13 10:55:05 -05:00
```yaml
2025-04-11 13:10:01 +05:30
name: Build with GCS Cache using Workload Identity
2019-11-13 10:55:05 -05:00
2025-04-11 13:10:01 +05:30
on: push
jobs:
build:
2025-04-11 13:10:01 +05:30
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write' # Required for Workload Identity Federation
2025-04-11 13:10:01 +05:30
steps:
- uses: actions/checkout@v4
2020-07-19 19:52:18 +09:00
2025-04-11 13:10:01 +05:30
# Set up Google Cloud authentication with Workload Identity Federation
- id: auth
uses: google-github-actions/auth@v2
with:
# The workload identity provider resource name
workload_identity_provider: projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
# The service account email address
service_account: my-service-account@my-project.iam.gserviceaccount.com
# Optional: Create credentials file for Google Cloud SDK
create_credentials_file: true
# Optional: Cleanup credentials after job completion
cleanup_credentials: true
- name: Cache Dependencies
id: cache-deps
uses: danySam/gcs-cache@v1
with:
path: path/to/dependencies
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
gcs-bucket: my-github-cache-bucket
2020-07-19 19:52:18 +09:00
```
2021-04-29 19:58:13 +02:00
2025-04-11 13:10:01 +05:30
For detailed setup instructions, see the [Google GitHub Actions Auth documentation](https://github.com/google-github-actions/auth). The basic steps include:
2021-04-29 19:58:13 +02:00
2025-04-11 13:10:01 +05:30
1. Create a Workload Identity Pool and Provider in Google Cloud
2. Configure IAM permissions for your service account
3. Store configuration values in GitHub Secrets
4. Add the auth action to your workflow with proper permissions
2025-04-11 13:10:01 +05:30
Using Workload Identity Federation provides enhanced security since:
- No long-lived credentials need to be stored as GitHub Secrets
- Access is temporary and scoped to just the running workflow
- All access is fully auditable in Google Cloud logs
2025-04-11 13:10:01 +05:30
## Language-Specific Caching Examples
2025-04-11 13:10:01 +05:30
- [Bun](#bun)
- [C# - NuGet](#c---nuget)
- [Clojure - Lein Deps](#clojure---lein-deps)
- [D - DUB](#d---dub)
- [POSIX](#posix)
- [Windows](#windows)