2022-06-15 19:33:29 +00:00
|
|
|
// 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 pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
pipeline_errors "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2023-12-29 20:19:42 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline/stepbuilder"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2022-06-15 19:33:29 +00:00
|
|
|
)
|
|
|
|
|
2023-12-29 20:19:42 +00:00
|
|
|
func parsePipeline(store store.Store, currentPipeline *model.Pipeline, user *model.User, repo *model.Repo, yamls []*forge_types.FileMeta, envs map[string]string) ([]*stepbuilder.Item, error) {
|
2022-11-04 23:35:06 +00:00
|
|
|
netrc, err := server.Config.Services.Forge.Netrc(user, repo)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msg("failed to generate netrc file")
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// get the previous pipeline so that we can send status change notifications
|
2022-11-06 11:44:04 +00:00
|
|
|
last, err := store.GetPipelineLastBefore(repo, currentPipeline.Branch, currentPipeline.ID)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Str("repo", repo.FullName).Msgf("error getting last pipeline before pipeline number '%d'", currentPipeline.Number)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
secs, err := server.Config.Services.Secrets.SecretListPipeline(repo, currentPipeline, &model.ListOptions{All: true})
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msgf("error getting secrets for %s#%d", repo.FullName, currentPipeline.Number)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
regs, err := server.Config.Services.Registries.RegistryList(repo, &model.ListOptions{All: true})
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msgf("error getting registry credentials for %s#%d", repo.FullName, currentPipeline.Number)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if envs == nil {
|
|
|
|
envs = map[string]string{}
|
|
|
|
}
|
|
|
|
if server.Config.Services.Environ != nil {
|
|
|
|
globals, _ := server.Config.Services.Environ.EnvironList(repo)
|
|
|
|
for _, global := range globals {
|
|
|
|
envs[global.Name] = global.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
for k, v := range currentPipeline.AdditionalVariables {
|
2022-09-27 09:05:00 +00:00
|
|
|
envs[k] = v
|
|
|
|
}
|
|
|
|
|
2023-12-29 20:19:42 +00:00
|
|
|
b := stepbuilder.StepBuilder{
|
2022-06-15 19:33:29 +00:00
|
|
|
Repo: repo,
|
2022-11-06 11:44:04 +00:00
|
|
|
Curr: currentPipeline,
|
2022-06-15 19:33:29 +00:00
|
|
|
Last: last,
|
|
|
|
Netrc: netrc,
|
|
|
|
Secs: secs,
|
|
|
|
Regs: regs,
|
|
|
|
Envs: envs,
|
2023-11-14 16:12:12 +00:00
|
|
|
Host: server.Config.Server.Host,
|
2022-06-15 19:33:29 +00:00
|
|
|
Yamls: yamls,
|
2023-06-04 22:15:07 +00:00
|
|
|
Forge: server.Config.Services.Forge,
|
2023-08-07 19:13:26 +00:00
|
|
|
ProxyOpts: compiler.ProxyOptions{
|
|
|
|
NoProxy: server.Config.Pipeline.Proxy.No,
|
|
|
|
HTTPProxy: server.Config.Pipeline.Proxy.HTTP,
|
|
|
|
HTTPSProxy: server.Config.Pipeline.Proxy.HTTPS,
|
|
|
|
},
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
2023-11-03 10:44:03 +00:00
|
|
|
return b.Build()
|
2023-10-08 12:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createPipelineItems(c context.Context, store store.Store,
|
|
|
|
currentPipeline *model.Pipeline, user *model.User, repo *model.Repo,
|
|
|
|
yamls []*forge_types.FileMeta, envs map[string]string,
|
2023-12-29 20:19:42 +00:00
|
|
|
) (*model.Pipeline, []*stepbuilder.Item, error) {
|
2023-10-08 12:05:06 +00:00
|
|
|
pipelineItems, err := parsePipeline(store, currentPipeline, user, repo, yamls, envs)
|
2023-12-13 13:11:05 +00:00
|
|
|
if pipeline_errors.HasBlockingErrors(err) {
|
2022-11-06 11:44:04 +00:00
|
|
|
currentPipeline, uerr := UpdateToStatusError(store, *currentPipeline, err)
|
2022-08-15 12:37:46 +00:00
|
|
|
if uerr != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(uerr).Msgf("error setting error status of pipeline for %s#%d", repo.FullName, currentPipeline.Number)
|
2023-05-14 12:18:43 +00:00
|
|
|
} else {
|
|
|
|
updatePipelineStatus(c, currentPipeline, repo, user)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
2023-11-03 10:44:03 +00:00
|
|
|
|
2023-12-13 13:11:05 +00:00
|
|
|
return currentPipeline, nil, err
|
|
|
|
} else if err != nil {
|
|
|
|
currentPipeline.Errors = pipeline_errors.GetPipelineErrors(err)
|
|
|
|
err = updatePipelinePending(c, store, currentPipeline, repo, user)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 12:05:06 +00:00
|
|
|
currentPipeline = setPipelineStepsOnPipeline(currentPipeline, pipelineItems)
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2023-11-03 10:44:03 +00:00
|
|
|
return currentPipeline, pipelineItems, err
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
2023-10-08 12:05:06 +00:00
|
|
|
|
|
|
|
// setPipelineStepsOnPipeline is the link between pipeline representation in "pipeline package" and server
|
|
|
|
// to be specific this func currently is used to convert the pipeline.Item list (crafted by StepBuilder.Build()) into
|
|
|
|
// a pipeline that can be stored in the database by the server
|
2023-12-29 20:19:42 +00:00
|
|
|
func setPipelineStepsOnPipeline(pipeline *model.Pipeline, pipelineItems []*stepbuilder.Item) *model.Pipeline {
|
2023-10-08 12:05:06 +00:00
|
|
|
var pidSequence int
|
|
|
|
for _, item := range pipelineItems {
|
|
|
|
if pidSequence < item.Workflow.PID {
|
|
|
|
pidSequence = item.Workflow.PID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range pipelineItems {
|
|
|
|
for _, stage := range item.Config.Stages {
|
|
|
|
var gid int
|
|
|
|
for _, step := range stage.Steps {
|
|
|
|
pidSequence++
|
|
|
|
if gid == 0 {
|
|
|
|
gid = pidSequence
|
|
|
|
}
|
|
|
|
step := &model.Step{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: step.Name,
|
2023-10-08 12:05:06 +00:00
|
|
|
UUID: step.UUID,
|
|
|
|
PipelineID: pipeline.ID,
|
|
|
|
PID: pidSequence,
|
|
|
|
PPID: item.Workflow.PID,
|
|
|
|
State: model.StatusPending,
|
|
|
|
Failure: step.Failure,
|
|
|
|
Type: model.StepType(step.Type),
|
|
|
|
}
|
|
|
|
if item.Workflow.State == model.StatusSkipped {
|
|
|
|
step.State = model.StatusSkipped
|
|
|
|
}
|
|
|
|
item.Workflow.Children = append(item.Workflow.Children, step)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item.Workflow.PipelineID = pipeline.ID
|
|
|
|
pipeline.Workflows = append(pipeline.Workflows, item.Workflow)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipeline
|
|
|
|
}
|