Automations: Conditional Params or other strategies for multi-env

Migrating our org to spaces and have it mostly working, but now looking to reduce duplication.

We have multiple environments (prod/dev/demo etc) which ultimately are just docker images and use ArgoCD to manage deployments.

It's all working, but we have to have a build for each environment which is a lot of duplication.

Any suggestions to clean this up? I was thinking:

  • If we could set params per branch, that would solve it (if dev, set ‘server’ to dev)
  • Some sort of templating support but I don't think that exists?

Thanks.

0
2 comments

Hi Dustin ,

We have a task in the backlog to implement templates in Space Automation. Unfortunately, there is no ETA on it yet. Feel free to follow the updates here: https://youtrack.jetbrains.com/issue/SPACE-17672/Space-Automation-Templates

Currently, there is another solution that should work for this case. Space Automation allows the use of external libraries hosted in a Maven repository. Thus, you can create and public a Maven library containing the jobs/steps/any Kotlin code to reuse in different .space.kts files or recall it:

https://blog.jetbrains.com/space/2020/10/20/space-automation-using-external-libraries/

https://www.jetbrains.com/help/space/run-kotlin-code.html#using-external-packages-in-kotlinscript

As for setting parameters per branch, the `kotlinScript` can be used to create a set of parameters with stable names and assign project values dynamically. The “{{run:git-checkout.ref}}” parameter can be used here: https://www.jetbrains.com/help/space/automation-parameters.html#provided-parameters

Here is a short sample of a similar conditions:

job("Build") {
	
	host {
		kotlinScript { api ->
			if (api.parameters["env"] == "dev") {
				api.parameters["target-url"] = Ref("project:dev-target-url")
				api.secrets["target-password"] = Ref("project:dev-target-password")
			} else {
				api.parameters["target-url"] = Ref("project:prod-target-url")
				api.secrets["target-password"] = Ref("project:prod-target-password")
			}
		}
	}

	host {
		shellScript {
			env["URL"] = "{{ target-url }}"
			env["PASSWORD"] = "{{ target-password }}"
		}
	}
}
0

Thanks so much Oleg Beriashvili this is perfect! Happy Holidays!

0

Please sign in to leave a comment.