-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathcache_save.go
More file actions
102 lines (77 loc) · 2.79 KB
/
Copy pathcache_save.go
File metadata and controls
102 lines (77 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package clicommand
import (
"context"
"fmt"
"slices"
"github.com/buildkite/agent/v3/api"
"github.com/buildkite/agent/v3/internal/cache"
"github.com/urfave/cli"
"go.opentelemetry.io/otel"
)
const cacheSaveHelpDescription = `Usage:
buildkite-agent cache save [options]
Description:
Saves files to the cache for the current build based on the cache configuration
defined in your cache config file (defaults to .buildkite/cache.yml).
The cache configuration file defines which files or directories should be cached
and their associated cache keys. Caches are scoped by organization, pipeline, and
branch.
Note: This feature is currently in development and subject to change. It is not
yet available to all customers.
Example:
$ buildkite-agent cache save
This will save all caches defined in .buildkite/cache.yml. You can also save
specific caches by providing their IDs:
$ buildkite-agent cache save --ids "node"
The cache will be stored in the bucket specified by --bucket-url or your
cache configuration. If a cache with the same key already exists, it will
not be overwritten.
Configuration File Format:
The cache configuration file should be in YAML format:
dependencies:
- id: node
key: '{{ id }}-{{ agent.os }}-{{ agent.arch }}-{{ checksum "package-lock.json" }}'
fallback_keys:
- '{{ id }}-{{ agent.os }}-{{ agent.arch }}-'
paths:
- node_modules
The command automatically uses the following environment variables when available:
- BUILDKITE_BRANCH (for branch scoping)
- BUILDKITE_PIPELINE_SLUG (for pipeline scoping)
- BUILDKITE_ORGANIZATION_SLUG (for organization scoping)`
type CacheSaveConfig struct {
GlobalConfig
APIConfig
CacheConfig
}
var CacheSaveCommand = cli.Command{
Name: "save",
Usage: "Saves files to the cache",
Description: cacheSaveHelpDescription,
Flags: slices.Concat(globalFlags(), apiFlags(), cacheFlags()),
Action: func(c *cli.Context) error {
ctx := context.Background()
ctx, cfg, l, _, done := setupLoggerAndConfig[CacheSaveConfig](ctx, c)
defer done()
ctx, span := otel.Tracer("buildkite-agent").Start(ctx, "cache-save")
defer span.End()
l.Infof("Cache save command executed")
apiCfg := loadAPIClientConfig(cfg, "AgentAccessToken")
if apiCfg.Token == "" {
return fmt.Errorf("an API token must be provided to save caches")
}
apiClient := api.NewClient(l, apiCfg)
// Build cache configuration
cacheCfg := cache.Config{
BucketURL: cfg.BucketURL,
Branch: cfg.Branch,
Pipeline: cfg.Pipeline,
Organization: cfg.Organization,
CacheConfigFile: cfg.CacheConfigFile,
Ids: cfg.Ids,
Concurrency: cfg.Concurrency,
}
// Perform cache save (logging happens inside)
return cache.RunSave(ctx, l, apiClient, cacheCfg)
},
}