flat-state-47578
01/22/2024, 1:24 AMkind: Build
type: container
name: my-service
disabled: ${local.env.GARDEN_DEV_SERVICE == "my-service"}
Then in the deploy action
kind: Deploy
type: container
name: my-service
spec:
image: ${local.env.GARDEN_DEV_SERVICE == "my-service" ? var.registryRoot + "/my-service:latest" : actions.build.my-service.outputs.deploymentImageId}
One question I have is what we do with the build
attribute of the deploy. The documentation is a bit confusing over its purpose - we've been sorta using it as expressing the dependency on the build, but I'm not sure if that's strictly necessary. There's also no obvious way of not specifying it (would null
or undefined
work?). Or should we express the build dependency using the dependencies
attribute?
Any thoughts or ideas appreciated.flat-state-47578
01/22/2024, 2:00 AMdisabled: true
for all builds, as they will be built if marked as a dependency even if disabled. So we need one less piece of conditional logic there.
One big stumbling block seems to be that if we don't have a particular environment variable defined at all, Garden falls in a heap. There's no defaulting to an empty string:
Invalid template string (${local.env.GARDEN_DEV_SERVICE == …): Could not find key GARDEN_DEV_SERVICE under local.env. Available keys: ...
flat-state-47578
01/22/2024, 3:55 AMproviders
- name: kubernetes
dependencies: [exec]
- name: exec
initScript: "export GARDEN_DEV_SERVICE"
I guess because local environment variables have already been captured and mutating them makes no difference, or the exec provider runs in a subshell and nothing that changes there makes any difference to Garden's environment.quaint-dress-831
01/23/2024, 8:55 AMquaint-dress-831
01/23/2024, 8:58 AMkaniko
and cluster-buildkit
will pull caches from deployment registriesflat-state-47578
01/23/2024, 8:58 AMflat-state-47578
01/23/2024, 8:58 AMflat-state-47578
01/23/2024, 8:59 AMquaint-dress-831
01/23/2024, 9:00 AMquaint-dress-831
01/23/2024, 9:02 AMquaint-dress-831
01/23/2024, 9:03 AMquaint-dress-831
01/23/2024, 9:12 AMbuild
key resolves actions from the context of the Build
action it refers to. For strict dependencies, you'll want to use dependencies
. See e.g. https://docs.garden.io/reference/action-types/deploy/helm#buildflat-state-47578
01/23/2024, 9:28 AMflat-state-47578
01/23/2024, 9:29 AMbuild
or spec.image
flat-state-47578
01/23/2024, 9:31 AMr6i.xlarge
) but as you know, the cluster buildkit only processes one build at a time so we have to consider the end-to-end total build time. At least one JVM service takes perhaps 6 minutes of that, another service might take 5 minutes. It all adds up.quaint-dress-831
01/23/2024, 9:38 AMflat-state-47578
01/23/2024, 9:38 AMflat-state-47578
01/23/2024, 9:39 AMflat-state-47578
01/23/2024, 9:40 AMquaint-dress-831
01/23/2024, 9:40 AMquaint-dress-831
01/23/2024, 9:41 AMflat-state-47578
01/23/2024, 9:41 AMflat-state-47578
01/24/2024, 4:15 AMquaint-dress-831
01/24/2024, 7:21 AMflat-state-47578
01/24/2024, 7:41 AMquaint-dress-831
01/24/2024, 7:43 AMquaint-dress-831
01/24/2024, 7:44 AMflat-state-47578
01/24/2024, 7:44 AMquaint-dress-831
01/24/2024, 7:45 AMquaint-dress-831
01/24/2024, 7:45 AMflat-state-47578
01/24/2024, 7:45 AMflat-state-47578
01/24/2024, 7:46 AMflat-state-47578
01/24/2024, 7:46 AMspec:
image: "${local.env.GARDEN_DEV_SERVICE == 'gateway' ? var.registryRoot + '/api-gateway:latest' : actions.build.gateway.outputs.deploymentImageId }"
but if we don't do something like GARDEN_DEV_SERVICE=gateway garden deploy
i.e. we don't provide GARDEN_DEV_SERVICE
at all, we get this:
Invalid template string (${local.env.GARDEN_DEV_SERVICE == …): Could not find key GARDEN_DEV_SERVICE under local.env. Available keys: ...
flat-state-47578
01/24/2024, 7:47 AMgarden deploy
for example, without that variable in the environmentflat-state-47578
01/24/2024, 7:47 AMflat-state-47578
01/24/2024, 7:48 AMquaint-dress-831
01/24/2024, 10:03 AMquaint-dress-831
01/24/2024, 10:13 AMlocal.env.GARDEN_DEV_SERVICE
to be an optional value it should workquaint-dress-831
01/24/2024, 10:13 AMbrief-restaurant-63679
01/24/2024, 11:00 AMbuild
field is confusing and we're planning on deprecating and removing it.
So you should definitely use spec.image
.
As for using the template string, this a bit of an awkward limitation of the template system that you can workaround by falling back to an empty string so the full line becomes:
spec:
image: "${(local.env.GARDEN_DEV_SERVICE || == 'gateway' ? var.registryRoot + '/api-gateway:latest' : actions.build.gateway.outputs.deploymentImageId }"
flat-state-47578
01/25/2024, 2:52 AMflat-state-47578
01/25/2024, 3:21 AMflat-state-47578
01/25/2024, 3:21 AM${(local.env.GARDEN_DEV_SERVICE || ) ...
perhaps?flat-state-47578
01/25/2024, 3:24 AMflat-state-47578
01/25/2024, 3:32 AMvariables:
devService: ${local.env.GARDEN_DEV_SERVICE || ''}
(using gardenDevService
seems to make it complain, I guess garden
prefixes are forbidden in variables?)
And in the services we do this:
spec:
image: "${var.devService == 'myservice' ? var.registryRoot + '/myservice:latest' : actions.build.myservice.outputs.deploymentImageId }"
just so we don't repeat the same default value again and again.
It's at least passing syntax checks and seems to have deployed but I'll rebuild the stack from scratch and play with it a bit to make sure it's working as expected.flat-state-47578
01/25/2024, 3:58 AMflat-state-47578
01/25/2024, 4:52 AMflat-state-47578
01/25/2024, 4:56 AMgarden update-remote all
so I have the latest code for one service up to date in the .garden
working directory.
Then I try to deploy it with GARDEN_DEV_SERVICE=myservice garden deploy myservice
which in theory should cause it to have a reference to actions.build.myservice.outputs.deploymentImageId
, but something here gets messed up.
The build in question has disabled: true
, but despite the reference to the build output in the deploy action, I think the source doesn't get synced to the cluster, nor does the build action actually run. Something is generating the version hash though, because then we get this error:
Pod myservice-6958dc8bb9-m88q6: Pulling - Pulling image "000000000000.dkr.ecr.us-east-1.amazonaws.com/xxxx/garden/myservice:v-a749b1cbac"
Pod myservice-6958dc8bb9-m88q6: Failed - Failed to pull image "
000000000000.dkr.ecr.us-east-1.amazonaws.com/xxxx/garden/myservice:v-a749b1cbac": rpc error: code = NotFound desc = failed to pull and unpack image "000000000000.dkr.ecr.us-east-1.amazonaws.com/xxxx/garden/myservice:v-a749b1cbac": failed to resolve reference "000000000000.dkr.ecr.us-east-1.amazonaws.com/xxxx/garden/myservice:v-a749b1cbac": 000000000000.dkr.ecr.us-east-1.amazonaws.com/xxxx/garden/myservice:v-a749b1cbac: not found
flat-state-47578
01/25/2024, 8:34 AMdisabled
attribute of the build, but that didn't make any difference:
disabled: ${var.devService == 'myservice'}
flat-state-47578
01/25/2024, 8:35 AMbuild
attribute of the deploy action to be there to do that, but it seems to need to be specified or just not present. I'll see if it is happy with null or undefined.flat-state-47578
01/25/2024, 8:41 AMbuild:
$if: ${var.devService == "myservice"}
$then: myservice
quaint-dress-831
01/25/2024, 9:25 AMflat-state-47578
01/25/2024, 9:57 AMbrief-restaurant-63679
01/25/2024, 11:20 AMimage: "${(local.env.GARDEN_DEV_SERVICE || '') == 'gateway' ? var.registryRoot + '/api-gateway:latest' : actions.build.gateway.outputs.deploymentImageId }"
brief-restaurant-63679
01/25/2024, 11:23 AMbrief-restaurant-63679
01/25/2024, 11:44 AMkind: Build
name: api
description: Build the backend
type: container
---
kind: Deploy
name: api
type: container
# You can set this irrespective of whether the image name is hardcoded or not
# If it is hardcoded, Garden will simply skip the build
dependencies: [build.api]
spec:
image: "${(local.env.GARDEN_USE_PREBUILT || '' ) == 'true' ? 'my-prebuilt-image:latest' : actions.build.api.outputs.deploymentImageId}"
# ...
brief-restaurant-63679
01/25/2024, 11:45 AMGARDEN_USE_PREBUILT=true garden deploy
it skips the build and uses the prebuilt image.
If I skip the flag it'll build the image from source and deploy that version.
Would this approach work for your use case?flat-state-47578
01/25/2024, 10:06 PMflat-state-47578
01/25/2024, 10:07 PMflat-state-47578
01/25/2024, 10:07 PMbrief-restaurant-63679
01/29/2024, 2:14 PMkind: Build
name: api
description: Build the backend
type: container
---
kind: Deploy
name: api
type: container
variables:
buildApi: ${(local.env.GARDEN_USE_PREBUILT || '') == 'true'} # <--- Set as a variable so we can re-use it
# You can set this irrespective of whether the image name is hardcoded or not
# If it is hardcoded, Garden will simply skip the build
dependencies:
- "${var.buildApi ? 'build.api' : null }" # <--- Conditionally depend on build.api
spec:
image: "${var.buildApi ? 'my-prebuilt-image:latest' : actions.build.api.outputs.deploymentImageId}"
# ...
flat-state-47578
01/29/2024, 9:29 PMflat-state-47578
01/29/2024, 9:29 PMorange-ability-1812
02/01/2024, 1:02 AM