Automate Builds, Tests & Deployments with VS Code Tasks
In this article
VS Code Tasks let you run build, test, and deployment commands directly from your editor with a single keyboard shortcut or menu click. Instead of switching to a terminal and typing long commands, you define them once in a tasks.json file, then trigger them with Ctrl+Shift+B (or Cmd+Shift+B on macOS). This guide walks you through creating tasks that automate your entire workflow, from compiling code to running tests and pushing deployments.
What You'll Need
- Visual Studio Code (any recent version, 1.60 or later is fine).
- Node.js and npm installed (for the examples, but the same principles apply to any command-line tool).
- A project folder open in VS Code. The tasks are stored in a
.vscode/tasks.jsonfile inside your project. - Basic familiarity with the command line and your project's build/test/deploy commands.
Step-by-Step
- Open the Tasks Configuration File
In VS Code, open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and type “Tasks: Configure Tasks”. If your project doesn't have atasks.jsonyet, VS Code will create a.vscodefolder and a sample file. Alternatively, you can manually create.vscode/tasks.jsonin your project root. - Understand the Basic Structure
The file is a JSON object with aversionand atasksarray. Each task has alabel, atype(usuallyshellorprocess), and acommand. For example:
The{ "version": "2.0.0", "tasks": [ { "label": "Build", "type": "shell", "command": "npm run build" } ] }labelis what you'll see in the menu,type: shellruns the command in a shell (allowing pipes, multiple commands, etc.), andcommandis the actual command. - Create a Build Task
Add a task for your build step. For a Node.js project, this might benpm run build. For a C++ project, it could bemakeorg++ -o app main.cpp. Set thegroupproperty to{"kind": "build", "isDefault": true}so it runs with Ctrl+Shift+B by default.{ "label": "Build", "type": "shell", "command": "npm run build", "group": {"kind": "build", "isDefault": true} } - Add a Test Task
Create another task for running tests. Use a different label like “Test”. You can also set a custom keyboard shortcut by assigning akeyinkeybindings.json(see Tips).{ "label": "Test", "type": "shell", "command": "npm test" } - Create a Deployment Task
For deployment, you might want to run a script likenpm run deployorrsync -avz ./dist/ user@server:/var/www/html/. Be careful with sensitive data—use environment variables or a separate config file instead of hardcoding credentials in the task.{ "label": "Deploy", "type": "shell", "command": "npm run deploy" } - Chain Tasks with Dependencies
You can run tasks in sequence by using thedependsOnproperty. For example, to run tests before deploying, add a “Deploy with Tests” task that depends on both.
When you run “Deploy with Tests”, VS Code will first run “Build” and “Test” (in parallel if they have no dependencies) and then run the deploy command.{ "label": "Deploy with Tests", "type": "shell", "command": "npm run deploy", "dependsOn": ["Build", "Test"] } - Run a Task
Press Ctrl+Shift+B to run the default build task. To run any task, use Ctrl+Shift+P and type “Tasks: Run Task”, then select the one you want. You can also click on “Terminal” in the top menu and choose “Run Task”. - Customize with Inputs and Options
For more flexibility, useinputsto prompt for values, andoptionsto set the working directory, environment variables, or shell. For example, to ask for a deployment environment:{ "version": "2.0.0", "inputs": [ { "id": "env", "type": "pickString", "options": ["staging", "production"], "default": "staging", "description": "Which environment?" } ], "tasks": [ { "label": "Deploy", "type": "shell", "command": "npm run deploy -- --env ${input:env}" } ] } - Use Problem Matchers
If your build outputs errors in a known format, add aproblemMatcherso VS Code can parse them and show them in the Problems panel. For example, for TypeScript, use"problemMatcher": "$tsc". For generic compiler output, you can define a custom matcher.
Common Mistakes
- Hardcoding secrets in the command. Never put passwords or API keys in
tasks.json. Use environment variables or a separate untracked config file. - Forgetting the
groupproperty for the build task, so Ctrl+Shift+B doesn't work as expected. - Using
processtype for commands that need shell features like&&or environment variable expansion. Useshellinstead. - Not setting
isBackgroundfor long-running tasks like watch mode. If a task runs indefinitely, mark it as"isBackground": trueand provide aproblemMatcherso VS Code knows when it's ready. - Assuming tasks run in the project root. Use
options.cwdif your command needs to run in a subdirectory.
Tips
- Use
Ctrl+Shift+Bfor the default build task, and assign custom keybindings for other tasks viakeybindings.json. For example:{ "key": "ctrl+shift+t", "command": "workbench.action.tasks.runTask", "args": "Test" } - Use
${workspaceFolder}to reference the current project path in commands, e.g.,"command": "cd ${workspaceFolder} && npm run build". - Leverage
inputsto make tasks interactive without leaving the editor. - Take advantage of
presentationoptions to control how the terminal panel behaves. For example,"presentation": {"reveal": "always", "panel": "shared"}keeps the terminal open and reuses it. - For complex workflows, consider using a task runner like npm scripts or Gulp, and call those from VS Code tasks—this keeps your
tasks.jsonsimple.
Troubleshooting
- Task not showing up: Make sure the
tasks.jsonis in the.vscodefolder of the workspace you have open. If you have multiple folders in a workspace, tasks might be scoped to a specific folder. - Command not found: The task runs in a shell that may not have the same PATH as your normal terminal. Add the full path to the command, or set
options.shellto your login shell and ensure it loads your profile. - Task runs but doesn't do anything: Check the terminal output for errors. Use
"presentation": {"reveal": "always"}to see the output automatically. - Ctrl+Shift+B not working: Ensure your build task has
"group": {"kind": "build", "isDefault": true}. If you have multiple build tasks, none is default unless you specify one. - DependsOn tasks run in parallel unexpectedly: By default,
dependsOnruns tasks in parallel. To force sequential execution, use"dependsOrder": "sequence". - Task hangs or never finishes: If your command is a watch mode or server, set
"isBackground": trueand configure aproblemMatcherto signal readiness. Otherwise, VS Code waits indefinitely.
Frequently Asked Questions
Can I use VS Code tasks for any programming language?
Yes, as long as you can run a command in the terminal. The task runs whatever command you provide, so it works with any toolchain—Python, C++, Java, etc.
How do I run tasks automatically on file save?
VS Code doesn't have built-in “run on save” for tasks. You can use an extension like “Run on Save” or configure a file watcher in your build tool (e.g., tsc --watch) as a background task.
What's the difference between shell and process type?
shell runs the command through a shell (like bash or cmd), allowing pipes, redirection, and multiple commands. process runs the command directly without a shell, which is more efficient but doesn't support shell syntax.
Can I share tasks across team members?
Yes, the tasks.json file is part of your project and can be committed to version control. Team members will get the same tasks when they clone the repo.
How do I pass arguments to a task from the command line?
You can use ${input:variable} to prompt for input, or use ${command:...} to invoke a VS Code command. For arbitrary arguments, you can use the args property in the task definition, but it's static.
Why does my task run in the wrong directory?
By default, tasks run in the workspace root. If you need a different directory, set "options": {"cwd": "/path/to/dir"} in the task.