From 41e8ab5506dce419baccbe39b5b66d96e1a67d96 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Thu, 5 Dec 2024 15:49:49 -0800 Subject: [PATCH 1/3] feat: adds events for deployments --- cli/cmd/cmds/deploy/deploy.go | 7 +++++++ foundry/api/blueprint.cue | 5 ++++- lib/project/project/project.go | 11 +++++++++++ lib/project/schema/_embed/schema.cue | 5 +++++ lib/project/schema/deployment.go | 3 +++ lib/project/schema/deployment_go_gen.cue | 3 +++ 6 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cli/cmd/cmds/deploy/deploy.go b/cli/cmd/cmds/deploy/deploy.go index 75b582ed..8792caa9 100644 --- a/cli/cmd/cmds/deploy/deploy.go +++ b/cli/cmd/cmds/deploy/deploy.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/input-output-hk/catalyst-forge/cli/pkg/deployment" + "github.com/input-output-hk/catalyst-forge/cli/pkg/events" "github.com/input-output-hk/catalyst-forge/cli/pkg/run" ) @@ -17,6 +18,12 @@ func (c *PushCmd) Run(ctx run.RunContext) error { return fmt.Errorf("could not load project: %w", err) } + eh := events.NewDefaultEventHandler(ctx.Logger) + if !eh.Firing(&project, project.GetDeploymentEvents()) { + ctx.Logger.Info("No deployment event is firing, skipping deployment") + return nil + } + deployer := deployment.NewGitopsDeployer(&project, &ctx.SecretStore, ctx.Logger) if err := deployer.Load(); err != nil { return fmt.Errorf("could not load deployer: %w", err) diff --git a/foundry/api/blueprint.cue b/foundry/api/blueprint.cue index 470ecc8f..1276adfd 100644 --- a/foundry/api/blueprint.cue +++ b/foundry/api/blueprint.cue @@ -14,6 +14,9 @@ project: { } } deployment: { + on: { + always: {} + } environment: "dev" modules: main: { container: "foundry-api-deployment" @@ -21,7 +24,7 @@ project: { values: { environment: name: "dev" server: image: { - tag: _ @forge(name="GIT_COMMIT_HASH") + tag: _ @forge(name="GIT_HASH_OR_TAG") } } } diff --git a/lib/project/project/project.go b/lib/project/project/project.go index 2079efc8..d2bb7745 100644 --- a/lib/project/project/project.go +++ b/lib/project/project/project.go @@ -81,6 +81,17 @@ func (p *Project) GetRelativePath() (string, error) { return relPath, nil } +// GetDeploymentEvents returns the deployment events for a project. +func (p *Project) GetDeploymentEvents() map[string]cue.Value { + events := make(map[string]cue.Value) + for event := range p.Blueprint.Project.Deployment.On { + config := p.RawBlueprint.Get(fmt.Sprintf("project.deployment.on.%s", event)) + events[event] = config + } + + return events +} + // GetReleaseEvents returns the release events for a release. func (p *Project) GetReleaseEvents(releaseName string) map[string]cue.Value { release, ok := p.Blueprint.Project.Release[releaseName] diff --git a/lib/project/schema/_embed/schema.cue b/lib/project/schema/_embed/schema.cue index 6c899a37..cd1920f2 100644 --- a/lib/project/schema/_embed/schema.cue +++ b/lib/project/schema/_embed/schema.cue @@ -193,6 +193,11 @@ package schema string } @go(Environment) + // On contains the events that trigger the deployment. + on: { + ... + } @go(On,map[string]any) + // Modules contains the configuration for the deployment modules for the project. // +optional modules?: null | #DeploymentModules @go(Modules,*DeploymentModules) diff --git a/lib/project/schema/deployment.go b/lib/project/schema/deployment.go index 19665e9f..9143153e 100644 --- a/lib/project/schema/deployment.go +++ b/lib/project/schema/deployment.go @@ -5,6 +5,9 @@ type Deployment struct { // Environment contains the environment to deploy the module to. Environment string `json:"environment"` + // On contains the events that trigger the deployment. + On map[string]any `json:"on"` + // Modules contains the configuration for the deployment modules for the project. // +optional Modules *DeploymentModules `json:"modules"` diff --git a/lib/project/schema/deployment_go_gen.cue b/lib/project/schema/deployment_go_gen.cue index e2dd7fb9..28fab201 100644 --- a/lib/project/schema/deployment_go_gen.cue +++ b/lib/project/schema/deployment_go_gen.cue @@ -9,6 +9,9 @@ package schema // Environment contains the environment to deploy the module to. environment: string @go(Environment) + // On contains the events that trigger the deployment. + on: {...} @go(On,map[string]any) + // Modules contains the configuration for the deployment modules for the project. // +optional modules?: null | #DeploymentModules @go(Modules,*DeploymentModules) From 7eefec867a75a46b653af0b8d4a22a78e5201b2b Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Thu, 5 Dec 2024 16:05:07 -0800 Subject: [PATCH 2/3] wip: removes conditional --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 524f5180..f9145400 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -152,7 +152,7 @@ jobs: deploy: uses: input-output-hk/catalyst-forge/.github/workflows/deploy.yml@master needs: [discover, check, build, test, release] - if: (fromJson(needs.discover.outputs.deployments)[0] != null) && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && !failure() && !cancelled() + if: (fromJson(needs.discover.outputs.deployments)[0] != null) && !failure() && !cancelled() with: deployments: ${{ needs.discover.outputs.deployments }} forge_version: ${{ inputs.forge_version }} From 08cb8a2aca62746a0e90b131f0418a87b2352f93 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Thu, 5 Dec 2024 16:14:41 -0800 Subject: [PATCH 3/3] wip: cleanup --- foundry/api/blueprint.cue | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/foundry/api/blueprint.cue b/foundry/api/blueprint.cue index 1276adfd..12672fb3 100644 --- a/foundry/api/blueprint.cue +++ b/foundry/api/blueprint.cue @@ -15,7 +15,8 @@ project: { } deployment: { on: { - always: {} + merge: {} + tag: {} } environment: "dev" modules: main: {