Adding commands to a devfile

A devfile allows you to specify commands to be available for execution in a workspace. Every command can contain a subset of actions. These subsets are related to specific components and are executed in the component containers. See the following tables for command properties in a devfile:

Table 1. commandObject
Key Type Required Description

exec

execObject

no

The exec command to run.

composite

compositeObject

no

The composite command to run.

Table 2. execObject
Key Type Required Description

id

string

yes

The ID of the command.

commandLine

string

yes

The command to run.

component

string

no

The component to which the action relates.

label

string

no

The optional label to describe the command.

workingDir

string

no

The working directory where you execute the command.

group

groupObject

no

The group to which the command belongs.

environment

envObject

no

The list of environment variables you use.

Table 3. compositeObject
Key Type Required Description

id

string

yes

The ID of the command.

commands

string

no

The exec commands that constitute the composite command and chains multiple commands together.

parallel

boolean

no

The flag that indicates if commands are executed in parallel.

label

string

no

The optional label to describe the command.

group

groupObject

no

The group to which the composite command belongs. The composite command cannot be of the run kind.

Table 4. groupObject
Key Type Required Description

kind

string

yes

The group to which the command belongs, such as: build, run, test, and debug.

isDefault

boolean

no

Identifies if it is the default command to run. Only one default command can be defined for each group.

Procedure
  1. Add a commands section in the devfile, containing a list of one or more commands.

  2. For each command, define an unique value for the mandatory id attribute.

  3. For each command, define a mandatory type of one of the following types:

    • exec

    • apply

    • composite

    Sample command
     commands:
       - id: build
         exec:
          component: mysql
          commandLine: mvn clean
          workingDir: /projects/spring-petclinic
  4. Define attributes for the exec command to run using the default shell in the container.

    • A commandLine attribute that is a command to execute.

    • A component attribute that specifies the container in which to execute the command.

    schemaVersion: 2.1.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"
    • A command can have only one action, though you can use composite commands to execute several commands either sequentially or in parallel.

  5. Command group

    To assign a given command to one or more groups that represent the nature of the command, use the following supported group types:

    • build

    • run

    • test

    • debug

      For each of the groups, define one default command by specifying the isDefault value.

      schemaVersion: 2.1.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
  6. Composite command

    To chain multiple commands together, define a composite command. To reference the individual commands that are called from a composite command, use the name of the command. To specify whether commands within a composite command are to be executed sequentially or in parallel, define the parallel boolean.

    schemaVersion: 2.1.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: false
Additional resources