Authoring devfiles
Creating devfiles
Most dev tools can utilize the devfile registry to fetch the stacks needed to begin development on projects. Sometimes, however, you might need to create a devfile from scratch or to create your own devfile template(s). This guide runs through the process, starting from a minimum devfile and building sample templates for common use cases.
Prerequisites
Procedure
- Creating a minimal devfile:
schemaVersionis the only required root elementmetadatais optional but it is recommended to have in your templates
Minimal Devfile
devfile.yamlschemaVersion: 2.2.0Minimal Devfile with Metadata
devfile.yamlschemaVersion: 2.2.0 metadata: name: devfile-sample version: 2.0.0 - Creating a web service template:
- Template
metadata- Set the template
nameandversion - Set a
descriptionfor the template - Set the
projectTypeandlanguageto describe what kind of stack is being used - Set
providerto tell who is providing this template - Set
tagsto give keyword which describe the elements of the stack - Set
architecturesto specify the platforms support in the template - For improved readability on devfile registries, set
displayNameto the title that will be the display text for this template andiconto tie a stack icon to the template:
schemaVersion: 2.2.0 metadata: name: web-service version: 1.0.0 description: A web service template. projectType: Go language: Go provider: Red Hat tags: [ 'Go', 'Gin', 'pq' ] architectures: [ 'amd64' ] displayName: Simple Web Service icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg - Set the template
- Setup
components- A
nameis required for a component - If a
containerentity is defined, animageproperty must be specified - Though
endpointsis optional, it is needed to expose the port for web connections and requires:- An endpoint
name - A
targetPortto expose, for example,8080if that is your http port
- An endpoint
- The web service might need to connect to an external database using environment variables. In this case, define environment variable names and values for the container component under
env:
components: - name: web container: endpoints: - name: http targetPort: 8080 env: DATABASE_HOST: db.example.com DATABASE_PORT: 5432 DATABASE_NAME: dev DATABASE_USER: devuser DATABASE_PASSWORD: devpassword image: quay.io/devfile/golang:latest - A
- Adding
commands- An
idto identify the command - An
execentity must be defined with acommandLinestring and a reference to acomponent- This implies that at least one component entity is defined under the root
componentselement
- This implies that at least one component entity is defined under the root
- The
workingDiris set to where the project source is stored - Command groups can be used to define automation that is useful for executing a web service:
commands: - id: build exec: commandLine: go build main.go component: web workingDir: ${PROJECT_SOURCE} group: kind: build isDefault: true - id: run exec: commandLine: ./main component: web workingDir: ${PROJECT_SOURCE} group: kind: run isDefault: true - An
- Define starter projects
- Add a starter project under
starterProjectsincluding at least anameand remote location, eithergitorzip - It is recommended to include a starter project description:
starterProjects: - name: web-starter description: A web service starting point. git: remotes: origin: https://github.com/devfile-samples/devfile-stack-go.git
- Add a starter project under
- Completing the content, the complete devfile should look like the following:
Complete Web Service Template
devfile.yamlschemaVersion: 2.2.0 metadata: name: web-service version: 1.0.0 description: A web service template. projectType: Go language: Go provider: Red Hat tags: [ 'Go', 'Gin', 'pq' ] architectures: [ 'amd64' ] displayName: Simple Web Service icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg components: - name: web container: endpoints: - name: http targetPort: 8080 env: DATABASE_HOST: db.example.com DATABASE_PORT: 5432 DATABASE_NAME: dev DATABASE_USER: devuser DATABASE_PASSWORD: devpassword image: quay.io/devfile/golang:latest commands: - id: build exec: commandLine: go build main.go component: web workingDir: ${PROJECT_SOURCE} group: kind: build isDefault: true - id: run exec: commandLine: ./main component: web workingDir: ${PROJECT_SOURCE} group: kind: run isDefault: true starterProjects: - name: web-starter description: A web service starting point. git: remotes: origin: https://github.com/devfile-samples/devfile-stack-go.git - Template