diff --git a/api/deployment/create.go b/api/deployment/create.go index f4c3f5ca1..11ccd9bf3 100644 --- a/api/deployment/create.go +++ b/api/deployment/create.go @@ -101,6 +101,29 @@ func CreateDeployment(c *gin.Context) { input.SetRef(fmt.Sprintf("refs/heads/%s", r.GetBranch())) } + deployConfigYAML, err := getDeploymentConfig(c, l, u, r, input.GetRef()) + if err != nil { + retErr := fmt.Errorf("unable to get deployment config for %s: %w", r.GetFullName(), err) + + util.HandleError(c, http.StatusInternalServerError, retErr) + + return + } + + deployConfig := deployConfigYAML.ToPipeline() + + if !deployConfig.Empty() { + err := deployConfig.Validate(input.GetTarget(), input.GetPayload()) + + if err != nil { + retErr := fmt.Errorf("unable to validate deployment config for %s: %w", r.GetFullName(), err) + + util.HandleError(c, http.StatusBadRequest, retErr) + + return + } + } + // send API call to create the deployment err = scm.FromContext(c).CreateDeployment(ctx, u, r, input) if err != nil { diff --git a/api/deployment/get_config.go b/api/deployment/get_config.go index bf8323e33..987f7dced 100644 --- a/api/deployment/get_config.go +++ b/api/deployment/get_config.go @@ -11,7 +11,9 @@ import ( "github.com/sirupsen/logrus" "gorm.io/gorm" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/compiler" + "github.com/go-vela/server/compiler/types/yaml/yaml" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" @@ -72,11 +74,25 @@ func GetDeploymentConfig(c *gin.Context) { r := repo.Retrieve(c) u := user.Retrieve(c) - ctx := c.Request.Context() - // capture ref from parameters - use default branch if not provided ref := util.QueryParameter(c, "ref", r.GetBranch()) + deployConfig, err := getDeploymentConfig(c, l, u, r, ref) + if err != nil { + retErr := fmt.Errorf("unable to get deployment config for %s: %w", r.GetFullName(), err) + + util.HandleError(c, http.StatusInternalServerError, retErr) + + return + } + + c.JSON(http.StatusOK, deployConfig) +} + +// getDeploymentConfig is a helper function that lightly compiles a Vela pipeline to retrieve the deployment configuration. +func getDeploymentConfig(c *gin.Context, l *logrus.Entry, u *types.User, r *types.Repo, ref string) (yaml.Deployment, error) { + ctx := c.Request.Context() + entry := fmt.Sprintf("%s@%s", r.GetFullName(), ref) l.Debugf("reading deployment config %s", entry) @@ -91,19 +107,11 @@ func GetDeploymentConfig(c *gin.Context) { config, err = scm.FromContext(c).ConfigBackoff(ctx, u, r, ref) if err != nil { - retErr := fmt.Errorf("unable to get pipeline configuration for %s: %w", entry, err) - - util.HandleError(c, http.StatusNotFound, retErr) - - return + return yaml.Deployment{}, fmt.Errorf("unable to get pipeline configuration for %s: %w", entry, err) } } else { // some other error - retErr := fmt.Errorf("unable to get pipeline for %s: %w", entry, err) - - util.HandleError(c, http.StatusInternalServerError, retErr) - - return + return yaml.Deployment{}, fmt.Errorf("unable to get pipeline for %s: %w", entry, err) } } else { l.Debugf("pipeline %s found in database", entry) @@ -117,12 +125,8 @@ func GetDeploymentConfig(c *gin.Context) { // compile the pipeline pipeline, _, err := compiler.CompileLite(ctx, config, nil, true) if err != nil { - retErr := fmt.Errorf("unable to compile pipeline %s: %w", entry, err) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return + return yaml.Deployment{}, fmt.Errorf("unable to compile pipeline %s: %w", entry, err) } - c.JSON(http.StatusOK, pipeline.Deployment) + return pipeline.Deployment, nil } diff --git a/compiler/types/pipeline/deployment.go b/compiler/types/pipeline/deployment.go index 475946dea..26b2efefa 100644 --- a/compiler/types/pipeline/deployment.go +++ b/compiler/types/pipeline/deployment.go @@ -58,7 +58,7 @@ func (d *Deployment) Validate(target string, inputParams map[string]string) erro // validate targets if len(d.Targets) > 0 && !slices.Contains(d.Targets, target) { - return fmt.Errorf("deployment target %s not found in deployment config targets", target) + return fmt.Errorf("deployment target `%s` not found in deployment config targets", target) } // validate params