-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): add local flag to exec module type
When local is set to true, the build command, tests and tasks are run in the module directory, as opposed in the .garden/build directory. Furthermore, the source code for local exec modules does not get synced into the .garden/build directory.
- Loading branch information
Showing
28 changed files
with
552 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Local Exec (Executing local commands with Garden) | ||
|
||
> Note: You need to have Go installed to run this project. | ||
This example project demonstrates how you can use the `exec` module type to run build commands, tasks and tests in the module directory, by setting `local: true` in the module config. By default the commands are executed in the `.garden/build` directory. | ||
|
||
The idea is to use a local `exec` module to run pre-build commands for a container module. | ||
|
||
## Project Structure | ||
|
||
The project consists of a `builder` module and a `backend` module. Both modules are in the same `garden.yml` file in the `backend` directory. | ||
|
||
The `backend` module is a simple `container` module that acts as a web server written in Go. The corresponding Dockerfile expects the web server binary to already be built before adding it to the image. | ||
|
||
To achieve this, we add a `go build` command to the `builder` module, set `local: true`, and then declare it as a build dependency in the `backend` module. We also tell Garden to copy the built binary to the `backend` build context since we're git ignoring it. This way, it's available with rest of the `backend` build context at `./garden/build/backend`. These are the relevant parts of the config | ||
|
||
```yaml | ||
# backend/garden.yml | ||
kind: Module | ||
type: exec | ||
local: true | ||
... | ||
build: | ||
command: [go, build, -o, bin/backend] | ||
--- | ||
kind: Module | ||
type: container | ||
build: | ||
dependencies: | ||
- name: builder | ||
copy: | ||
- source: bin | ||
target: . | ||
... | ||
``` | ||
|
||
This ensures that Garden runs `go build` in the module directory before it attempts to build the Docker image for the `backend` module. | ||
|
||
## Usage | ||
|
||
Run `garden deploy` to deploy the project. You'll notice that Garden first builds the Go binary, before it's added to the container image. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM golang:1.8.3-alpine | ||
|
||
WORKDIR /server/ | ||
COPY bin/backend . | ||
|
||
ENTRYPOINT ./backend | ||
|
||
EXPOSE 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
kind: Module | ||
type: exec | ||
local: true | ||
name: builder | ||
# This runs in the module directory before the backend container that's specified below is built | ||
build: | ||
command: [go, build, -o, bin/backend] | ||
env: | ||
GOOS: linux | ||
GOARCH: amd64 | ||
--- | ||
# This module's Dockerfile expects the Go binary to already be built | ||
kind: Module | ||
type: container | ||
name: backend | ||
# Setting the "builder" module as a dependency ensures that the Go binary is built | ||
# before the build step for this module is executed. | ||
build: | ||
dependencies: | ||
- name: builder | ||
copy: | ||
- source: bin | ||
target: . | ||
services: | ||
- name: backend | ||
ports: | ||
- name: http | ||
containerPort: 8080 | ||
# Maps service:80 -> container:8080 | ||
servicePort: 80 | ||
ingresses: | ||
- path: /hello-backend | ||
port: http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "Hello from Go!") | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/hello-backend", handler) | ||
fmt.Println("Server running...") | ||
|
||
http.ListenAndServe(":8080", nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Project | ||
name: local-exec | ||
environments: | ||
- name: local | ||
providers: | ||
- name: local-kubernetes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.