Define deploy command as sequence of build phases (#129)

This commit is contained in:
Pieter Noordhuis 2022-12-12 12:49:25 +01:00 committed by GitHub
parent 8640696b4b
commit c255bd686a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 126 additions and 0 deletions

View File

@ -1,3 +1,9 @@
// Package bundle is the top level package for Databricks Application Bundles.
//
// A bundle is represented by the [Bundle] type. It consists of configuration
// and runtime state, such as a client to a Databricks workspace.
// Every mutation to a bundle's configuration or state is represented as a [Mutator].
// This interface makes every mutation observable and lets us reason about sequencing.
package bundle package bundle
import ( import (

20
bundle/phases/build.go Normal file
View File

@ -0,0 +1,20 @@
package phases
import (
"github.com/databricks/bricks/bundle"
"github.com/databricks/bricks/bundle/artifacts"
"github.com/databricks/bricks/bundle/config/interpolation"
)
// The build phase builds artifacts.
func Build() bundle.Mutator {
return newPhase(
"build",
[]bundle.Mutator{
artifacts.BuildAll(),
interpolation.Interpolate(
interpolation.IncludeLookupsInPath("artifacts"),
),
},
)
}

20
bundle/phases/deploy.go Normal file
View File

@ -0,0 +1,20 @@
package phases
import (
"github.com/databricks/bricks/bundle"
"github.com/databricks/bricks/bundle/artifacts"
"github.com/databricks/bricks/bundle/deploy/terraform"
)
// The deploy phase deploys artifacts and resources.
func Deploy() bundle.Mutator {
return newPhase(
"deploy",
[]bundle.Mutator{
artifacts.UploadAll(),
terraform.Interpolate(),
terraform.Write(),
terraform.Apply(),
},
)
}

View File

@ -0,0 +1,24 @@
package phases
import (
"github.com/databricks/bricks/bundle"
"github.com/databricks/bricks/bundle/config/interpolation"
"github.com/databricks/bricks/bundle/config/mutator"
)
// The initialize phase fills in defaults and connects to the workspace.
// Interpolation of fields referring to the "bundle" and "workspace" keys
// happens upon completion of this phase.
func Initialize() bundle.Mutator {
return newPhase(
"initialize",
[]bundle.Mutator{
mutator.PopulateCurrentUser(),
mutator.DefaultArtifactPath(),
interpolation.Interpolate(
interpolation.IncludeLookupsInPath("bundle"),
interpolation.IncludeLookupsInPath("workspace"),
),
},
)
}

30
bundle/phases/phase.go Normal file
View File

@ -0,0 +1,30 @@
// Package phases defines build phases as logical groups of [bundle.Mutator] instances.
package phases
import (
"context"
"github.com/databricks/bricks/bundle"
)
// This phase type groups mutators that belong to a lifecycle phase.
// It expands into the specific mutators when applied.
type phase struct {
name string
mutators []bundle.Mutator
}
func newPhase(name string, mutators []bundle.Mutator) bundle.Mutator {
return &phase{
name: name,
mutators: mutators,
}
}
func (p *phase) Name() string {
return p.name
}
func (p *phase) Apply(context.Context, *bundle.Bundle) ([]bundle.Mutator, error) {
return p.mutators, nil
}

26
cmd/bundle/deploy.go Normal file
View File

@ -0,0 +1,26 @@
package bundle
import (
"github.com/databricks/bricks/bundle"
"github.com/databricks/bricks/bundle/phases"
"github.com/spf13/cobra"
)
var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy bundle",
PreRunE: ConfigureBundle,
RunE: func(cmd *cobra.Command, args []string) error {
b := bundle.Get(cmd.Context())
return bundle.Apply(cmd.Context(), b, []bundle.Mutator{
phases.Initialize(),
phases.Build(),
phases.Deploy(),
})
},
}
func init() {
AddCommand(deployCmd)
}