mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-04-26 21:44:44 +00:00
Fix to show build pipeline parse error (#1066)
This commit is contained in:
parent
fc862bf627
commit
5a945c10e9
6 changed files with 107 additions and 32 deletions
30
pipeline/frontend/yaml/error.go
Normal file
30
pipeline/frontend/yaml/error.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
// Copyright 2022 Woodpecker Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package yaml
|
||||||
|
|
||||||
|
// PipelineParseError is an error that occurs when the pipeline parsing fails.
|
||||||
|
type PipelineParseError struct {
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e PipelineParseError) Error() string {
|
||||||
|
return e.Err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e PipelineParseError) Is(target error) bool {
|
||||||
|
_, ok1 := target.(PipelineParseError)
|
||||||
|
_, ok2 := target.(*PipelineParseError)
|
||||||
|
return ok1 || ok2
|
||||||
|
}
|
|
@ -3,6 +3,8 @@ package matrix
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
pipeline "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -99,8 +101,10 @@ func parse(raw []byte) (Matrix, error) {
|
||||||
data := struct {
|
data := struct {
|
||||||
Matrix map[string][]string
|
Matrix map[string][]string
|
||||||
}{}
|
}{}
|
||||||
err := yaml.Unmarshal(raw, &data)
|
if err := yaml.Unmarshal(raw, &data); err != nil {
|
||||||
return data.Matrix, err
|
return nil, &pipeline.PipelineParseError{Err: err}
|
||||||
|
}
|
||||||
|
return data.Matrix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseList(raw []byte) ([]Axis, error) {
|
func parseList(raw []byte) ([]Axis, error) {
|
||||||
|
@ -110,6 +114,8 @@ func parseList(raw []byte) ([]Axis, error) {
|
||||||
}
|
}
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
err := yaml.Unmarshal(raw, &data)
|
if err := yaml.Unmarshal(raw, &data); err != nil {
|
||||||
return data.Matrix.Include, err
|
return nil, &pipeline.PipelineParseError{Err: err}
|
||||||
|
}
|
||||||
|
return data.Matrix.Include, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ package pipeline
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
@ -51,31 +52,31 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, build *mo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
remoteYamlConfigs []*remote.FileMeta
|
||||||
|
configFetchErr error
|
||||||
|
filtered bool
|
||||||
|
parseErr error
|
||||||
|
)
|
||||||
|
|
||||||
// fetch the build file from the remote
|
// fetch the build file from the remote
|
||||||
configFetcher := shared.NewConfigFetcher(server.Config.Services.Remote, server.Config.Services.ConfigService, repoUser, repo, build)
|
configFetcher := shared.NewConfigFetcher(server.Config.Services.Remote, server.Config.Services.ConfigService, repoUser, repo, build)
|
||||||
remoteYamlConfigs, err := configFetcher.Fetch(ctx)
|
remoteYamlConfigs, configFetchErr = configFetcher.Fetch(ctx)
|
||||||
if err != nil {
|
if configFetchErr == nil {
|
||||||
msg := fmt.Sprintf("cannot find config '%s' in '%s' with user: '%s'", repo.Config, build.Ref, repoUser.Login)
|
filtered, parseErr = branchFiltered(build, remoteYamlConfigs)
|
||||||
log.Debug().Err(err).Str("repo", repo.FullName).Msg(msg)
|
if parseErr == nil {
|
||||||
return nil, ErrNotFound{Msg: msg}
|
if filtered {
|
||||||
}
|
err := ErrFiltered{Msg: "branch does not match restrictions defined in yaml"}
|
||||||
|
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
filtered, err := branchFiltered(build, remoteYamlConfigs)
|
if zeroSteps(build, remoteYamlConfigs) {
|
||||||
if err != nil {
|
err := ErrFiltered{Msg: "step conditions yield zero runnable steps"}
|
||||||
msg := "failure to parse yaml from hook"
|
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
|
||||||
log.Debug().Err(err).Str("repo", repo.FullName).Msg(msg)
|
return nil, err
|
||||||
return nil, ErrBadRequest{Msg: msg}
|
}
|
||||||
}
|
}
|
||||||
if filtered {
|
|
||||||
err := ErrFiltered{Msg: "branch does not match restrictions defined in yaml"}
|
|
||||||
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if zeroSteps(build, remoteYamlConfigs) {
|
|
||||||
err := ErrFiltered{Msg: "step conditions yield zero runnable steps"}
|
|
||||||
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update some build fields
|
// update some build fields
|
||||||
|
@ -83,8 +84,20 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, build *mo
|
||||||
build.Verified = true
|
build.Verified = true
|
||||||
build.Status = model.StatusPending
|
build.Status = model.StatusPending
|
||||||
|
|
||||||
// TODO(336) extend gated feature with an allow/block List
|
if configFetchErr != nil {
|
||||||
if repo.IsGated {
|
log.Debug().Str("repo", repo.FullName).Err(configFetchErr).Msgf("cannot find config '%s' in '%s' with user: '%s'", repo.Config, build.Ref, repoUser.Login)
|
||||||
|
build.Started = time.Now().Unix()
|
||||||
|
build.Finished = build.Started
|
||||||
|
build.Status = model.StatusError
|
||||||
|
build.Error = fmt.Sprintf("pipeline definition not found in %s", repo.FullName)
|
||||||
|
} else if parseErr != nil {
|
||||||
|
log.Debug().Str("repo", repo.FullName).Err(parseErr).Msg("failed to parse yaml")
|
||||||
|
build.Started = time.Now().Unix()
|
||||||
|
build.Finished = build.Started
|
||||||
|
build.Status = model.StatusError
|
||||||
|
build.Error = fmt.Sprintf("failed to parse pipeline: %s", parseErr.Error())
|
||||||
|
} else if repo.IsGated {
|
||||||
|
// TODO(336) extend gated feature with an allow/block List
|
||||||
build.Status = model.StatusBlocked
|
build.Status = model.StatusBlocked
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,6 +118,18 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, build *mo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if build.Status == model.StatusError {
|
||||||
|
if err := publishToTopic(ctx, build, repo); err != nil {
|
||||||
|
log.Error().Err(err).Msg("publishToTopic")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := updateBuildStatus(ctx, build, repo, repoUser); err != nil {
|
||||||
|
log.Error().Err(err).Msg("updateBuildStatus")
|
||||||
|
}
|
||||||
|
|
||||||
|
return build, nil
|
||||||
|
}
|
||||||
|
|
||||||
build, buildItems, err := createBuildItems(ctx, _store, build, repoUser, repo, remoteYamlConfigs, nil)
|
build, buildItems, err := createBuildItems(ctx, _store, build, repoUser, repo, remoteYamlConfigs, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
||||||
|
|
|
@ -73,10 +73,11 @@ func createBuildItems(ctx context.Context, store store.Store, build *model.Build
|
||||||
}
|
}
|
||||||
buildItems, err := b.Build()
|
buildItems, err := b.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, err := shared.UpdateToStatusError(store, *build, err); err != nil {
|
build, uerr := shared.UpdateToStatusError(store, *build, err)
|
||||||
|
if uerr != nil {
|
||||||
log.Error().Err(err).Msgf("Error setting error status of build for %s#%d", repo.FullName, build.Number)
|
log.Error().Err(err).Msgf("Error setting error status of build for %s#%d", repo.FullName, build.Number)
|
||||||
}
|
}
|
||||||
return nil, nil, err
|
return build, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
build = shared.SetBuildStepsOnBuild(b.Curr, buildItems)
|
build = shared.SetBuildStepsOnBuild(b.Curr, buildItems)
|
||||||
|
|
|
@ -16,14 +16,17 @@ package pipeline
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
||||||
"github.com/woodpecker-ci/woodpecker/server"
|
"github.com/woodpecker-ci/woodpecker/server"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||||
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -77,6 +80,13 @@ func Restart(ctx context.Context, store store.Store, lastBuild *model.Build, use
|
||||||
return nil, fmt.Errorf(msg)
|
return nil, fmt.Errorf(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(configs) == 0 {
|
||||||
|
newBuild, uerr := shared.UpdateToStatusError(store, *newBuild, errors.New("pipeline definition not found"))
|
||||||
|
if uerr != nil {
|
||||||
|
log.Debug().Err(uerr).Msg("failure to update pipeline status")
|
||||||
|
}
|
||||||
|
return newBuild, nil
|
||||||
|
}
|
||||||
if err := persistBuildConfigs(store, configs, newBuild.ID); err != nil {
|
if err := persistBuildConfigs(store, configs, newBuild.ID); err != nil {
|
||||||
msg := fmt.Sprintf("failure to persist build config for %s.", repo.FullName)
|
msg := fmt.Sprintf("failure to persist build config for %s.", repo.FullName)
|
||||||
log.Error().Err(err).Msg(msg)
|
log.Error().Err(err).Msg(msg)
|
||||||
|
@ -85,6 +95,9 @@ func Restart(ctx context.Context, store store.Store, lastBuild *model.Build, use
|
||||||
|
|
||||||
newBuild, buildItems, err := createBuildItems(ctx, store, newBuild, user, repo, pipelineFiles, envs)
|
newBuild, buildItems, err := createBuildItems(ctx, store, newBuild, user, repo, pipelineFiles, envs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, &yaml.PipelineParseError{}) {
|
||||||
|
return newBuild, nil
|
||||||
|
}
|
||||||
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
||||||
log.Error().Err(err).Msg(msg)
|
log.Error().Err(err).Msg(msg)
|
||||||
return nil, fmt.Errorf(msg)
|
return nil, fmt.Errorf(msg)
|
||||||
|
|
|
@ -107,14 +107,14 @@ func (b *ProcBuilder) Build() ([]*BuildItem, error) {
|
||||||
// parse yaml pipeline
|
// parse yaml pipeline
|
||||||
parsed, err := yaml.ParseString(substituted)
|
parsed, err := yaml.ParseString(substituted)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, &yaml.PipelineParseError{Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lint pipeline
|
// lint pipeline
|
||||||
if err := linter.New(
|
if err := linter.New(
|
||||||
linter.WithTrusted(b.Repo.IsTrusted),
|
linter.WithTrusted(b.Repo.IsTrusted),
|
||||||
).Lint(parsed); err != nil {
|
).Lint(parsed); err != nil {
|
||||||
return nil, err
|
return nil, &yaml.PipelineParseError{Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !parsed.Branches.Match(b.Curr.Branch) && (b.Curr.Event != model.EventDeploy && b.Curr.Event != model.EventTag) {
|
if !parsed.Branches.Match(b.Curr.Branch) && (b.Curr.Event != model.EventDeploy && b.Curr.Event != model.EventTag) {
|
||||||
|
|
Loading…
Reference in a new issue