Skip to content

Instantly share code, notes, and snippets.

@smyrman
Last active February 5, 2023 03:32
Show Gist options
  • Save smyrman/fdc43a72168ec7af16c2e35600c465e9 to your computer and use it in GitHub Desktop.
Save smyrman/fdc43a72168ec7af16c2e35600c465e9 to your computer and use it in GitHub Desktop.
Taskfile example for templated container builds
Assumung forlder structure
Taskfile.yml # root task file (not included in Gist)
Gopkg.tompl # for dep
Gopkg.lock # for dep
vendor/ # generated by dep
cmd/ # go main packacges (compilable programs)
. product-api1/ # example main package
. . main.go
pkg/ # locally shared go packages
docker/
. images/ # docker images
. . Taskfile.yml # taskfile for docker images (included in Gist)
. . product-ap1/ # example docker context folder
. . . Dockerfile
. . . bin/ # generated by Taskfile.yml
. stacks/ # example folder to maintain e.g. deployment Taskfiles, composefiles and/or kuberentes pods.
## -- build groups --
default:
desc: "alias for all"
deps:
- all
all:
desc: "group for building all container images"
deps:
- apis
- maintenance
- product-web
apis:
desc: "group for building the product API containers"
deps:
- product-api1
- product-api2
- product-api3
maintenance:
desc: "group for building product maintenance containers"
deps:
- backup
# -- individual build targets --
product-api1:
desc: builds the 'company/product-api1' Docker image
deps:
- task: tmpl/go-cmd-docker-image
vars:
NAME: "product-api1"
product-api2:
desc: builds the 'company/product-api2' Docker image
deps:
- task: tmpl/go-cmd-docker-image
vars:
NAME: "product-api2
product-api3:
desc: builds the 'company/product-api3' Docker image
deps:
- task: tmpl/go-cmd-docker-image
vars:
NAME: "product-api3
product-web:
desc: builds the 'company/product-web' Docker image
dir: backup
cmds:
- docker build -t company/product-web .
backup:
desc: builds the 'company/backup' Docker image
dir: backup
cmds:
- docker build -t company/backup .
## -- template tasks --
tmpl/go-cmd-docker-image:
dir: "{{.NAME}}"
deps:
- task: tmpl/go-build-for-docker
vars:
SRC_DIR: 'cmd/{{.NAME}}'
TARGET_DIR: 'docker/images/{{.NAME}}/bin'
cmds:
- docker build -t company/{{.NAME}} .
- touch .task-done # speed up build-process slightly.
sources:
- ./**/*
generates:
- .task-done
tmpl/go-build-for-docker:
# reusable task to build a static golang binary intended for Docker scratch images
dir: ../..
deps:
- root/vendor
cmds:
- mkdir -p "{{.TARGET_DIR}}"
- go build -o "{{.TARGET_DIR}}/{{.NAME}}" -ldflags '-s -w' "./{{.SRC_DIR}}"
env:
GOOS: "linux"
GOARCH: "amd64"
CGO_ENABLED: "0"
sources:
- "pkg/**/*.go"
- "{{.SRC_DIR}}/**/*.go"
- "vendor/**/*.go"
generates:
- "{{.TARGET_DIR}}/{{.NAME}}"
## -- external tasks --
root/vendor:
# install go vendor dependencies
dir: ../../
cmds:
- task vendor
# FIXME: should get status of parent task https://github.com/go-task/task/issues/81
sources:
- Gopkg.lock
generates:
- vendor/**/*.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment