From 7c291c3df1903fc9c6c3045871fe51e8789ca2d2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 19 May 2025 20:30:15 +0000 Subject: [PATCH 1/3] Initial plan for issue From 8f8fcc50f7ba32bc44c94394c8019c590f480ab3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 19 May 2025 20:34:31 +0000 Subject: [PATCH 2/3] Add directory creation script to CacheV2 task Co-authored-by: fadnavistanmay <43892780+fadnavistanmay@users.noreply.github.com> --- Tasks/CacheV2/README.md | 41 +++++++++++++++++++ .../CacheV2/_buildConfigs/Node20/package.json | 22 ++++++++++ Tasks/CacheV2/package.json | 22 ++++++++++ Tasks/CacheV2/task.json | 9 ++++ 4 files changed, 94 insertions(+) create mode 100644 Tasks/CacheV2/README.md create mode 100644 Tasks/CacheV2/_buildConfigs/Node20/package.json create mode 100644 Tasks/CacheV2/package.json diff --git a/Tasks/CacheV2/README.md b/Tasks/CacheV2/README.md new file mode 100644 index 000000000000..3bc57dc8d8f2 --- /dev/null +++ b/Tasks/CacheV2/README.md @@ -0,0 +1,41 @@ +# Cache Task + +This task improves build performance by caching files between pipeline runs. + +## Description +The Cache task can save time by caching files from one run to another. The task helps you deliver faster builds by restoring previously cached files, so you don't have to regenerate or redownload them. + +## Common usage scenarios + +- **Package dependencies**: Cache your package manager's cache directory to avoid downloading package dependencies for every build. +- **Build outputs**: Cache compiled or processed files that don't change often to speed up builds. + +## Example: Caching Yarn packages + +```yaml +variables: + YARN_CACHE_FOLDER: $(Pipeline.Workspace)/.yarn + +steps: +- task: Cache@2 + inputs: + key: 'yarn | "$(Agent.OS)" | yarn.lock' + restoreKeys: | + yarn | "$(Agent.OS)" + path: $(YARN_CACHE_FOLDER) + displayName: Cache Yarn packages +``` + +## Notes +- The cache directory (specified in the `path` input) will be created automatically if it doesn't exist +- For Yarn, make sure to set the `YARN_CACHE_FOLDER` variable to point to the cache location +- For other package managers, set the appropriate environment variables according to the package manager's documentation + +## Inputs + +| Input | Description | +| ----- | ----------- | +| key | Key (unique identifier) for the cache. This should be a string that can be segmented using '|'. File paths can be absolute or relative to $(System.DefaultWorkingDirectory). | +| path | Path of the folder to cache. Can be fully-qualified or relative to $(System.DefaultWorkingDirectory). Wildcards are not supported. | +| cacheHitVar | Variable to set to 'true' when the cache is restored (i.e. a cache hit), otherwise set to 'false'. | +| restoreKeys | Additional restore key prefixes that are used if the primary key misses. This can be a newline-delimited list of key prefixes. | \ No newline at end of file diff --git a/Tasks/CacheV2/_buildConfigs/Node20/package.json b/Tasks/CacheV2/_buildConfigs/Node20/package.json new file mode 100644 index 000000000000..d1fc6ac39f50 --- /dev/null +++ b/Tasks/CacheV2/_buildConfigs/Node20/package.json @@ -0,0 +1,22 @@ +{ + "name": "vsts-tasks-cache", + "version": "2.0.0", + "description": "Azure Pipelines Cache Task", + "main": "restoreCache.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/Microsoft/azure-pipelines-tasks.git" + }, + "author": "Microsoft Corporation", + "license": "MIT", + "bugs": { + "url": "https://github.com/Microsoft/azure-pipelines-tasks/issues" + }, + "homepage": "https://github.com/Microsoft/azure-pipelines-tasks#readme", + "dependencies": { + "azure-pipelines-task-lib": "^4.16.0" + } +} \ No newline at end of file diff --git a/Tasks/CacheV2/package.json b/Tasks/CacheV2/package.json new file mode 100644 index 000000000000..d1fc6ac39f50 --- /dev/null +++ b/Tasks/CacheV2/package.json @@ -0,0 +1,22 @@ +{ + "name": "vsts-tasks-cache", + "version": "2.0.0", + "description": "Azure Pipelines Cache Task", + "main": "restoreCache.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/Microsoft/azure-pipelines-tasks.git" + }, + "author": "Microsoft Corporation", + "license": "MIT", + "bugs": { + "url": "https://github.com/Microsoft/azure-pipelines-tasks/issues" + }, + "homepage": "https://github.com/Microsoft/azure-pipelines-tasks#readme", + "dependencies": { + "azure-pipelines-task-lib": "^4.16.0" + } +} \ No newline at end of file diff --git a/Tasks/CacheV2/task.json b/Tasks/CacheV2/task.json index 0ce4145643d3..79121051c88f 100644 --- a/Tasks/CacheV2/task.json +++ b/Tasks/CacheV2/task.json @@ -51,6 +51,15 @@ ], "instanceNameFormat": "Cache", "execution": { + "Node10": { + "target": "restoreCache.js" + }, + "Node16": { + "target": "restoreCache.js" + }, + "Node20": { + "target": "restoreCache.js" + }, "AgentPlugin": { "target": "Agent.Plugins.PipelineCache.RestorePipelineCacheV0, Agent.Plugins" } From 13f882f4e4c2943871d8232c310faa5d5f5d1a8c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 19 May 2025 20:35:39 +0000 Subject: [PATCH 3/3] Add restoreCache.js script for directory creation Co-authored-by: fadnavistanmay <43892780+fadnavistanmay@users.noreply.github.com> --- Tasks/CacheV2/restoreCache.js | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Tasks/CacheV2/restoreCache.js diff --git a/Tasks/CacheV2/restoreCache.js b/Tasks/CacheV2/restoreCache.js new file mode 100644 index 000000000000..52282deb8036 --- /dev/null +++ b/Tasks/CacheV2/restoreCache.js @@ -0,0 +1,38 @@ +const tl = require('azure-pipelines-task-lib/task'); +const path = require('path'); +const fs = require('fs'); + +/** + * Ensures that the directory exists, creating it if necessary + */ +function ensureDirectoryExists(directoryPath) { + if (!fs.existsSync(directoryPath)) { + console.log(`Directory does not exist, creating: ${directoryPath}`); + try { + fs.mkdirSync(directoryPath, { recursive: true }); + console.log(`Successfully created directory: ${directoryPath}`); + } catch (err) { + console.warn(`Failed to create directory: ${directoryPath}. Error: ${err.message}`); + } + } else { + console.log(`Directory already exists: ${directoryPath}`); + } +} + +async function run() { + try { + // Get the path from task input + const cachePath = tl.getPathInput('path', true); + console.log(`Ensuring cache directory exists: ${cachePath}`); + + ensureDirectoryExists(cachePath); + + console.log('Directory preparation completed successfully'); + } catch (err) { + console.warn(`Error occurred while ensuring directory exists: ${err.message}`); + // We don't want to fail the task if directory creation fails, + // as this is just a preparation step for the actual cache task + } +} + +run(); \ No newline at end of file