Skip to content

Commit 73d340d

Browse files
committed
Fix method to read
1 parent a95d98b commit 73d340d

5 files changed

Lines changed: 87 additions & 27 deletions

File tree

README.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ go install github.com/richardartoul/gobuildcache@latest
3333

3434
## Usage
3535

36+
You can configure `gobuildcache` by passing arguments directly to the program via `GOCACHEPROG`, or by using environment variables. The CLI argument approach is recommended for CI/CD workflows.
37+
38+
### Using CLI Arguments (Recommended)
39+
3640
```bash
37-
export GOCACHEPROG=gobuildcache
41+
export GOCACHEPROG="gobuildcache --backend=disk"
3842
go build ./...
3943
go test ./...
4044
```
@@ -43,61 +47,91 @@ By default, `gobuildcache` uses an on-disk cache stored in the OS default tempor
4347

4448
For "production" use-cases in CI, you'll want to configure `gobuildcache` to use S3 Express One Zone, Google Cloud Storage, or a similarly low latency distributed backend.
4549

50+
### Using Environment Variables (Alternative)
51+
52+
You can also configure `gobuildcache` using environment variables:
53+
54+
```bash
55+
export GOCACHEPROG=gobuildcache
56+
export BACKEND_TYPE=disk
57+
go build ./...
58+
go test ./...
59+
```
60+
61+
**Note:** CLI arguments take precedence over environment variables when both are provided.
62+
4663
### Using S3
4764

65+
**Using CLI arguments (recommended):**
66+
4867
```bash
49-
export BACKEND_TYPE=s3
50-
export S3_BUCKET=$BUCKET_NAME
68+
export GOCACHEPROG="gobuildcache --backend=s3 --s3-bucket=$BUCKET_NAME --s3-prefix=go-build-cache/"
69+
export AWS_REGION=$BUCKET_REGION
70+
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY
71+
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
72+
go build ./...
73+
go test ./...
5174
```
5275

53-
You'll also have to provide AWS credentials. `gobuildcache` embeds the AWS V2 S3 SDK so any method of providing credentials to that library will work, but the simplest is to use environment variables as demonstrated below.
76+
**Using environment variables:**
5477

5578
```bash
5679
export GOCACHEPROG=gobuildcache
5780
export BACKEND_TYPE=s3
5881
export S3_BUCKET=$BUCKET_NAME
82+
export S3_PREFIX=go-build-cache/
5983
export AWS_REGION=$BUCKET_REGION
6084
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY
6185
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
6286
go build ./...
6387
go test ./...
6488
```
6589

90+
You'll need to provide AWS credentials. `gobuildcache` embeds the AWS V2 S3 SDK so any method of providing credentials to that library will work (environment variables, IAM roles, etc.).
91+
6692
### Using Google Cloud Storage (GCS)
6793

94+
**Using CLI arguments (recommended):**
95+
96+
```bash
97+
export GOCACHEPROG="gobuildcache --backend=gcs --gcs-bucket=$BUCKET_NAME --gcs-prefix=go-build-cache/"
98+
go build ./...
99+
go test ./...
100+
```
101+
102+
**Using environment variables:**
103+
68104
```bash
105+
export GOCACHEPROG=gobuildcache
69106
export BACKEND_TYPE=gcs
70107
export GCS_BUCKET=$BUCKET_NAME
108+
export GCS_PREFIX=go-build-cache/
109+
go build ./...
110+
go test ./...
71111
```
72112

73113
GCS authentication uses Application Default Credentials. You can provide credentials in one of the following ways:
74114

75115
1. **Service Account JSON file** (recommended for CI):
76116
```bash
77117
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json
78-
export GOCACHEPROG=gobuildcache
79-
export BACKEND_TYPE=gcs
80-
export GCS_BUCKET=$BUCKET_NAME
118+
export GOCACHEPROG="gobuildcache --backend=gcs --gcs-bucket=$BUCKET_NAME"
81119
go build ./...
82120
go test ./...
83121
```
84122

85123
2. **Metadata service** (when running on GCP):
86124
```bash
87125
# No credentials file needed - uses metadata service automatically
88-
export GOCACHEPROG=gobuildcache
89-
export BACKEND_TYPE=gcs
90-
export GCS_BUCKET=$BUCKET_NAME
126+
export GOCACHEPROG="gobuildcache --backend=gcs --gcs-bucket=$BUCKET_NAME"
91127
go build ./...
92128
go test ./...
93129
```
94130

95131
3. **gcloud CLI credentials** (for local development):
96132
```bash
97133
gcloud auth application-default login
98-
export GOCACHEPROG=gobuildcache
99-
export BACKEND_TYPE=gcs
100-
export GCS_BUCKET=$BUCKET_NAME
134+
export GOCACHEPROG="gobuildcache --backend=gcs --gcs-bucket=$BUCKET_NAME"
101135
go build ./...
102136
go test ./...
103137
```

examples/github_actions_gcs.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
name: Run Tests
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
- name: Set up Go
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version: '1.25'
21+
# Use our own cache, obviously.
22+
cache: false
23+
- name: Install gobuildcache
24+
run: go install github.com/richardartoul/gobuildcache@latest
25+
- name: Authenticate to Google Cloud
26+
uses: google-github-actions/auth@v2
27+
with:
28+
credentials_json: ${{ secrets.GCP_SA_KEY }}
29+
- name: Run short tests
30+
env:
31+
GOCACHEPROG: "gobuildcache --backend=gcs --gcs-bucket=${{ secrets.GCS_BUCKET }} --gcs-prefix=go-build-cache/${{ github.repository }}"
32+
run: go test ./...

examples/github_actions_s3.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ jobs:
2424
run: go install github.com/richardartoul/gobuildcache@latest
2525
- name: Run short tests
2626
env:
27-
BACKEND_TYPE: s3
28-
S3_BUCKET: EXAMPLE_BUCKET
29-
S3_PREFIX: EXAMPLE_PREFIX
3027
AWS_REGION: EXAMPLE_REGION
3128
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
3229
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33-
GOCACHEPROG: gobuildcache
30+
GOCACHEPROG: "gobuildcache --backend=s3 --s3-bucket=EXAMPLE_BUCKET --s3-prefix=EXAMPLE_PREFIX"
3431
run: go test ./...
3532

3633

examples/github_actions_tigris.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ jobs:
2424
run: go install github.com/richardartoul/gobuildcache@latest
2525
- name: Run short tests
2626
env:
27-
BACKEND_TYPE: s3
28-
S3_BUCKET: EXAMPLE_BUCKET
29-
S3_PREFIX: EXAMPLE_PREFIX
30-
AWS_REGION: EXAMPLE_REGION
31-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
32-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3327
AWS_ENDPOINT_URL_S3: https://t3.storage.dev
3428
AWS_ENDPOINT_URL_IAM: https://iam.storage.dev
3529
AWS_REGION: auto
36-
GOCACHEPROG: gobuildcache
30+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
31+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
32+
GOCACHEPROG: "gobuildcache --backend=s3 --s3-bucket=EXAMPLE_BUCKET --s3-prefix=EXAMPLE_PREFIX"
3733
run: go test ./...

pkg/backends/gcs.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ type GCS struct {
2828
func NewGCS(bucket, prefix string) (*GCS, error) {
2929
ctx := context.Background()
3030

31-
// Create GCS client using Application Default Credentials
32-
// This will use GOOGLE_APPLICATION_CREDENTIALS env var or metadata service
33-
client, err := storage.NewClient(ctx)
31+
// Create GCS client using Application Default Credentials.
32+
// WithJSONReads forces the JSON API for downloads (default is XML).
33+
// This is required for GCS Anywhere Cache compatibility.
34+
client, err := storage.NewClient(ctx, storage.WithJSONReads())
3435
if err != nil {
3536
return nil, fmt.Errorf("failed to create GCS client: %w", err)
3637
}

0 commit comments

Comments
 (0)