buildUpdateCacheMemoryJob builds a job that updates cache-memory after detection passes This job downloads cache-memory artifacts and saves them to GitHub Actions cache
(data *WorkflowData, threatDetectionEnabled bool)
| 906 | // buildUpdateCacheMemoryJob builds a job that updates cache-memory after detection passes |
| 907 | // This job downloads cache-memory artifacts and saves them to GitHub Actions cache |
| 908 | func (c *Compiler) buildUpdateCacheMemoryJob(data *WorkflowData, threatDetectionEnabled bool) (*Job, error) { |
| 909 | if data.CacheMemoryConfig == nil || len(data.CacheMemoryConfig.Caches) == 0 { |
| 910 | return nil, nil |
| 911 | } |
| 912 | |
| 913 | // Only create this job if threat detection is enabled |
| 914 | // Otherwise, cache is updated automatically by actions/cache post-action |
| 915 | if !threatDetectionEnabled { |
| 916 | return nil, nil |
| 917 | } |
| 918 | |
| 919 | cacheLog.Printf("Building update_cache_memory job for %d caches (threatDetectionEnabled=%v)", len(data.CacheMemoryConfig.Caches), threatDetectionEnabled) |
| 920 | |
| 921 | var steps []string |
| 922 | |
| 923 | // Build steps for each cache |
| 924 | // In workflow_call context, use the per-invocation prefix from the agent job. |
| 925 | cacheArtifactPrefix := artifactPrefixExprForAgentDownstreamJob(data) |
| 926 | |
| 927 | for _, cache := range data.CacheMemoryConfig.Caches { |
| 928 | // Skip restore-only caches |
| 929 | if cache.RestoreOnly { |
| 930 | continue |
| 931 | } |
| 932 | |
| 933 | // Determine artifact name and cache directory. |
| 934 | // Apply the workflow_call prefix to ensure we download the correct invocation's artifact. |
| 935 | cacheDir := cacheMemoryDirFor(cache.ID) |
| 936 | var artifactName string |
| 937 | if cache.ID == "default" { |
| 938 | artifactName = cacheArtifactPrefix + "cache-memory" |
| 939 | } else { |
| 940 | artifactName = cacheArtifactPrefix + "cache-memory-" + cache.ID |
| 941 | } |
| 942 | |
| 943 | // Download artifact step |
| 944 | var downloadStep strings.Builder |
| 945 | // Generate a safe step ID from cache ID (replace hyphens with underscores) |
| 946 | downloadStepID := strings.ReplaceAll("download_cache_"+cache.ID, "-", "_") |
| 947 | fmt.Fprintf(&downloadStep, " - name: Download cache-memory artifact (%s)\n", cache.ID) |
| 948 | fmt.Fprintf(&downloadStep, " id: %s\n", downloadStepID) |
| 949 | fmt.Fprintf(&downloadStep, " uses: %s\n", c.getActionPin("actions/download-artifact")) |
| 950 | downloadStep.WriteString(" continue-on-error: true\n") |
| 951 | downloadStep.WriteString(" with:\n") |
| 952 | fmt.Fprintf(&downloadStep, " name: %s\n", artifactName) |
| 953 | fmt.Fprintf(&downloadStep, " path: %s\n", cacheDir) |
| 954 | steps = append(steps, downloadStep.String()) |
| 955 | |
| 956 | // Check if cache folder exists and is not empty |
| 957 | var checkStep strings.Builder |
| 958 | checkStepID := strings.ReplaceAll("check_cache_"+cache.ID, "-", "_") |
| 959 | fmt.Fprintf(&checkStep, " - name: Check if cache-memory folder has content (%s)\n", cache.ID) |
| 960 | fmt.Fprintf(&checkStep, " id: %s\n", checkStepID) |
| 961 | checkStep.WriteString(" shell: bash\n") |
| 962 | checkStep.WriteString(" run: |\n") |
| 963 | fmt.Fprintf(&checkStep, " if [ -d \"%s\" ] && [ \"$(ls -A %s 2>/dev/null)\" ]; then\n", cacheDir, cacheDir) |
| 964 | checkStep.WriteString(" echo \"has_content=true\" >> \"$GITHUB_OUTPUT\"\n") |
| 965 | checkStep.WriteString(" else\n") |