From 849b02a433c375edc1a8ebc52bf38d8e13263ba3 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Sun, 26 Nov 2023 19:59:36 +0100 Subject: [PATCH] Fix pipeline-related environment (#2876) closes https://github.com/woodpecker-ci/woodpecker/issues/2672 pipeline model must be persisted first to have some fields like `Number` and `ID` --- server/pipeline/create.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/server/pipeline/create.go b/server/pipeline/create.go index 78637d847..762b888ed 100644 --- a/server/pipeline/create.go +++ b/server/pipeline/create.go @@ -54,6 +54,13 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline // update some pipeline fields pipeline.RepoID = repo.ID pipeline.Status = model.StatusPending + setGatedState(repo, pipeline) + err = _store.CreatePipeline(pipeline) + if err != nil { + msg := fmt.Errorf("failed to save pipeline for %s", repo.FullName) + log.Error().Str("repo", repo.FullName).Err(err).Msg(msg.Error()) + return nil, msg + } // fetch the pipeline file from the forge configFetcher := forge.NewConfigFetcher(server.Config.Services.Forge, server.Config.Services.Timeout, server.Config.Services.ConfigService, repoUser, repo, pipeline) @@ -61,13 +68,13 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline if configFetchErr != nil { log.Debug().Str("repo", repo.FullName).Err(configFetchErr).Msgf("cannot find config '%s' in '%s' with user: '%s'", repo.Config, pipeline.Ref, repoUser.Login) - return nil, persistPipelineWithErr(ctx, _store, pipeline, repo, repoUser, fmt.Errorf("pipeline definition not found in %s", repo.FullName)) + return nil, updatePipelineWithErr(ctx, _store, pipeline, repo, repoUser, fmt.Errorf("pipeline definition not found in %s", repo.FullName)) } pipelineItems, parseErr := parsePipeline(_store, pipeline, repoUser, repo, forgeYamlConfigs, nil) if errors.HasBlockingErrors(parseErr) { log.Debug().Str("repo", repo.FullName).Err(parseErr).Msg("failed to parse yaml") - return nil, persistPipelineWithErr(ctx, _store, pipeline, repo, repoUser, parseErr) + return nil, updatePipelineWithErr(ctx, _store, pipeline, repo, repoUser, parseErr) } else if parseErr != nil { pipeline.Errors = errors.GetPipelineErrors(parseErr) } @@ -77,15 +84,6 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline return nil, ErrFiltered } - setGatedState(repo, pipeline) - - err = _store.CreatePipeline(pipeline) - if err != nil { - msg := fmt.Errorf("failed to save pipeline for %s", repo.FullName) - log.Error().Str("repo", repo.FullName).Err(err).Msg(msg.Error()) - return nil, msg - } - pipeline = setPipelineStepsOnPipeline(pipeline, pipelineItems) // persist the pipeline config for historical correctness, restarts, etc @@ -121,12 +119,12 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline return pipeline, nil } -func persistPipelineWithErr(ctx context.Context, _store store.Store, pipeline *model.Pipeline, repo *model.Repo, repoUser *model.User, err error) error { +func updatePipelineWithErr(ctx context.Context, _store store.Store, pipeline *model.Pipeline, repo *model.Repo, repoUser *model.User, err error) error { pipeline.Started = time.Now().Unix() pipeline.Finished = pipeline.Started pipeline.Status = model.StatusError pipeline.Errors = errors.GetPipelineErrors(err) - dbErr := _store.CreatePipeline(pipeline) + dbErr := _store.UpdatePipeline(pipeline) if dbErr != nil { msg := fmt.Errorf("failed to save pipeline for %s", repo.FullName) log.Error().Err(dbErr).Msg(msg.Error())