Commands
Adding commands
A devfile allows to specify commands to be available for execution in a workspace. Every command can contain a subset of actions, which are related to a specific component in whose container it will be executed.
Prerequisites
Procedure
Add a
commandssection in the devfile, containing a list of one or more commands.For each command, define an unique value for the mandatory
idattribute.For each command, define a mandatory type of one of the following types:
execorvscode-tasks.Sample command
devfile.yamlcommands: - id: build exec: component: mysql commandLine: mvn clean workingDir: /projects/spring-petclinicDefine attributes for the
execcommand to run using the default shell in the container.A
commandLineattribute that is a command to execute.A
componentattribute that specifies the container in which to execute the command.
devfile.yamlschemaVersion: 2.0.0 metadata: name: MyDevfile projects: - name: my-go-project clonePath: go/src/github.com/acme/my-go-project git: remotes: origin: "https://github.com/acme/my-go-project.git" components: - name: go-cli container: image: golang memoryLimit: 512Mi mountSources: true command: ['sleep', 'infinity'] env: - name: GOPATH value: $(PROJECTS_ROOT)/go - name: GOCACHE value: /tmp/go-cache commands: - id: compile and run exec: component: go-cli commandLine: "go get -d && go run main.go" workingDir: "${PROJECTS_ROOT}/src/github.com/acme/my-go-project" runAsUser: rootAny component on which commands are executed must define a
nameattribute. This name is used to reference the component in the command definition. Example:name: go-cliin the component definition andcomponent: go-cliin the command definition.A command can have only one action, though you can use
compositecommands to execute several commands either sequentially or in parallel.
Define attributes for the
vscode-taskorvscode-launchcommand to run using the Editor.If the editor in the workspace supports it, the devfile can specify additional configuration in the editor-specific format. This is dependent on the integration code present in the workspace editor itself and so is not a generic mechanism.
devfile.yamlschemaVersion: 2.0.0 metadata: name: MyDevfile projects: - name: my-go-project clonePath: go/src/github.com/acme/my-go-project git: remotes: origin: "https://github.com/acme/my-go-project.git" commands: - id: task vscode-task: name: tasks referenceContent: > { "version": "2.0.0", "tasks": [ { "label": "create test file", "type": "shell", "command": "touch ${workspaceFolder}/test.file" } ] }
This example shows association of a tasks.json file with a devfile. Notice the vscode-task type that instructs the editor to interpret this command as a tasks definition and referenceContent attribute that contains the contents of the file itself. You can also save this file separately from the devfile and use reference attribute to specify a relative or absolute URL to it.
Command group
A given command can be assigned to one or more groups that represents the nature of the command. The support groups are:
build,run,testanddebug. For each of the groups, one default command can be defined in each group by specifying theisDefaultvalue.devfile.yamlschemaVersion: 2.0.0 metadata: name: MyDevfile projects: - name: my-maven-project clonePath: maven/src/github.com/acme/my-maven-project git: remotes: origin: "https://github.com/acme/my-maven-project.git" components: - name: maven container: image: eclipse/maven-jdk8:latest mountSources: true command: ['tail'] commands: - id: package exec: component: maven commandLine: "mvn package" group: kind: build - id: install exec: component: maven commandLine: "mvn install" group: kind: build isDefault: trueComposite command
A composite command can be defined to chain multiple commands together. The individual commands that are called from a composite command can be referenced by the
nameof the command. Aparallelboolean can be specified to determine if the commands within a composite command are being executed sequentially or in parallel.devfile.yamlschemaVersion: 2.0.0 metadata: name: MyDevfile projects: - name: my-maven-project clonePath: maven/src/github.com/acme/my-maven-project git: remotes: origin: "https://github.com/acme/my-maven-project.git" components: - name: maven container: image: eclipse/maven-jdk8:latest mountSources: true command: ['tail'] commands: - id: package exec: component: maven commandLine: "mvn package" group: kind: build - id: install exec: component: maven commandLine: "mvn install" group: kind: build isDefault: true - id: installAndPackage composite: commands: - install - package parallel: falseCommand preview URL
It is possible to specify a preview URL for commands that expose web UI. This URL is offered for opening when the command is executed.
devfile.yamlcommands: - id: tasks exec: previewUrl: port: 8080 path: /myweb component: go-cli commandLine: "go run webserver.go" workingDir: ${PROJECTS_ROOT}/webserverTCP port where the application listens. Mandatory parameter.
The path part of the URL to the UI. Optional parameter. The default is root (
/).
The example above opens ++http://__<server-domain>__/myweb++, where _<server-domain>_ is the URL to the dynamically created Kubernetes Ingress or OpenShift Route.