Skip to content

Commit

Permalink
feat: allow multiple modules in a single file
Browse files Browse the repository at this point in the history
More than one module can now be defined in the same garden.yml file.

This is useful e.g. where more than one Dockerfile is used to build the
same container (e.g. for development and production).

Within a garden.yml file, each module definition lives within a YAML
document (separated by `---`).
  • Loading branch information
thsig committed Feb 19, 2019
1 parent 0b859ce commit ff4d370
Show file tree
Hide file tree
Showing 35 changed files with 745 additions and 117 deletions.
18 changes: 18 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ Configuration for a Garden project. This should be specified in the garden.yml f
| Type | Required |
| ---- | -------- |
| `object` | Yes
### `project.apiVersion`
[project](#project) > apiVersion

The schema version of this project's config (currently not used).

| Type | Required | Allowed Values |
| ---- | -------- | -------------- |
| `string` | Yes | "0"
### `project.name`
[project](#project) > name

Expand Down Expand Up @@ -199,6 +207,7 @@ project:
## Project YAML schema
```yaml
project:
apiVersion: '0'
name:
defaultEnvironment: ''
environmentDefaults:
Expand All @@ -224,6 +233,14 @@ Configure a module whose sources are located in this directory.
| Type | Required |
| ---- | -------- |
| `object` | Yes
### `module.apiVersion`
[module](#module) > apiVersion

The schema version of this module's config (currently not used).

| Type | Required | Allowed Values |
| ---- | -------- | -------------- |
| `string` | Yes | "0"
### `module.type`
[module](#module) > type

Expand Down Expand Up @@ -371,6 +388,7 @@ POSIX-style path or filename to copy the directory or file(s) to (defaults to sa
## Module YAML schema
```yaml
module:
apiVersion: '0'
type:
name:
description:
Expand Down
42 changes: 42 additions & 0 deletions docs/using-garden/configuration-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,48 @@ in your tests.

Tests can be run via `garden test`, as well as `garden dev`.

## Multiple modules in the same file

Sometimes, it's useful to define several modules in the same `garden.yml` file. One common situation is where more than
one Dockerfile is in use (e.g. one for a development build and one for a production build).

Another is when the dev configuration and the production configuration have different integration testing suites,
which may depend on different external services being available.

To do this, simply add a document separator (`---`) between the module definitions. Here's a simple example:

```yaml
module:
description: Hello world container - dev configuration
type: container
dockerfile: Dockerfile-dev
...
tests:
- name: unit
args: [npm, test]
- name: integ
args: [npm, run, integ-dev]
dependencies:
- hello-function
- dev-integration-testing-backend
---
module:
description: Hello world container - production configuration
type: container
dockerfile: Dockerfile-prod
...
tests:
- name: unit
args: [npm, test]
- name: integ
args: [npm, run, integ-prod]
dependencies:
- hello-function
- prod-integration-testing-backend
```

## Next steps

We highly recommend browsing through the [Example projects](../examples/README.md) to see different examples of how projects and modules can be configured.
Expand Down
31 changes: 31 additions & 0 deletions examples/multiple-modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Example project demonstrating several modules/Dockerfiles in one directory

This project shows how you can configure several modules in a single directory.

This is useful, for exmample, when you want to use more than one Dockerfile (e.g. one for development, one for production).

```shell
$ garden deploy
Deploy 🚀

✔ dev → Building dev:602ae70cb8-1550064758... → Done (took 9.1 sec)
✔ prod → Building prod:602ae70cb8-1550064758... → Done (took 8.9 sec)
✔ prod → Deploying version 602ae70cb8-1550064758... → Done (took 4 sec)
✔ dev → Deploying version 602ae70cb8-1550064758... → Done (took 3.9 sec)

Done! ✔️

$ garden call dev
✔ Sending HTTP GET request to http://multiple-modules.local.app.garden/hello-dev

200 OK

Greetings! This container was built with Dockerfile-dev.

$ garden call prod
✔ Sending HTTP GET request to http://multiple-modules.local.app.garden/hello-prod

200 OK

Greetings! This container was built with Dockerfile-prod.
```
6 changes: 6 additions & 0 deletions examples/multiple-modules/garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
project:
name: multiple-modules
environments:
- name: local
providers:
- name: local-kubernetes
4 changes: 4 additions & 0 deletions examples/multiple-modules/node-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
Dockerfile
garden.yml
app.yaml
14 changes: 14 additions & 0 deletions examples/multiple-modules/node-service/Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:9-alpine

ENV PORT=8080
ENV ENVIRONMENT=dev
ENV HELLO_PATH=/hello-dev
EXPOSE ${PORT}
WORKDIR /app

ADD package.json /app
RUN npm install

ADD . /app

CMD ["npm", "start"]
14 changes: 14 additions & 0 deletions examples/multiple-modules/node-service/Dockerfile-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:9-alpine

ENV PORT=8080
ENV ENVIRONMENT=prod
ENV HELLO_PATH=/hello-prod
EXPOSE ${PORT}
WORKDIR /app

ADD package.json /app
RUN npm install

ADD . /app

CMD ["npm", "start"]
12 changes: 12 additions & 0 deletions examples/multiple-modules/node-service/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const app = express();

// These environment variables are set differently in Dockerfile-dev and Dockerfile-prod
const envName = process.env.ENVIRONMENT;
const helloPath = process.env.HELLO_PATH

const helloMsg = `Greetings! This container was built with Dockerfile-${envName}.`;

app.get(helloPath, (req, res) => res.send(helloMsg));

module.exports = { app }
37 changes: 37 additions & 0 deletions examples/multiple-modules/node-service/garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module:
name: dev
description: Node service (dev mode)
dockerfile: Dockerfile-dev
type: container
services:
- name: dev
command: [npm, start]
ports:
- name: http
containerPort: 8080
ingresses:
- path: /hello-dev
port: http
tests:
- name: unit
args: [npm, test]

---

module:
name: prod
description: Node service (production mode)
dockerfile: Dockerfile-prod
type: container
services:
- name: prod
command: [npm, start]
ports:
- name: http
containerPort: 8080
ingresses:
- path: /hello-prod
port: http
tests:
- name: unit
args: [npm, test]
3 changes: 3 additions & 0 deletions examples/multiple-modules/node-service/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { app } = require('./app');

app.listen(process.env.PORT, '0.0.0.0', () => console.log('Node service started'));
22 changes: 22 additions & 0 deletions examples/multiple-modules/node-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "node-service",
"version": "1.0.0",
"description": "Simple Node.js docker service",
"main": "main.js",
"scripts": {
"start": "node main.js",
"test": "echo OK",
"integ": "node_modules/mocha/bin/mocha test/integ.js"
},
"author": "garden.io <[email protected]>",
"license": "ISC",
"dependencies": {
"express": "^4.16.2",
"request": "^2.83.0",
"request-promise": "^4.2.2"
},
"devDependencies": {
"mocha": "^5.1.1",
"supertest": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ const DEFAULT_CLI_LOGGER_TYPE = LoggerType.fancy

// For initializing garden without a project config
export const MOCK_CONFIG: GardenConfig = {
version: "0",
dirname: "/",
path: process.cwd(),
project: {
apiVersion: "0",
name: "mock-project",
defaultEnvironment: "local",
environments: defaultEnvironments,
Expand Down
Loading

0 comments on commit ff4d370

Please sign in to comment.