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"
|
2024-04-16 06:04:55 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
2023-12-08 07:15:08 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
func parsePipeline(forge forge.Forge, store store.Store, currentPipeline *model.Pipeline, user *model.User, repo *model.Repo, yamls []*forge_types.FileMeta, envs map[string]string) ([]*stepbuilder.Item, error) {
|
|
|
|
netrc, err := 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
|
|
|
}
|
|
|
|
|
2024-02-11 17:42:33 +00:00
|
|
|
secretService := server.Config.Services.Manager.SecretServiceFromRepo(repo)
|
|
|
|
secs, err := secretService.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
|
|
|
}
|
|
|
|
|
2024-02-11 17:42:33 +00:00
|
|
|
registryService := server.Config.Services.Manager.RegistryServiceFromRepo(repo)
|
|
|
|
regs, err := registryService.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{}
|
|
|
|
}
|
2024-02-11 17:42:33 +00:00
|
|
|
|
|
|
|
environmentService := server.Config.Services.Manager.EnvironmentService()
|
|
|
|
if environmentService != nil {
|
|
|
|
globals, _ := environmentService.EnvironList(repo)
|
2022-06-15 19:33:29 +00:00
|
|
|
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,
|
2024-04-16 06:04:55 +00:00
|
|
|
Forge: 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
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
func createPipelineItems(c context.Context, forge forge.Forge, store store.Store,
|
2023-10-08 12:05:06 +00:00
|
|
|
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) {
|
2024-04-16 06:04:55 +00:00
|
|
|
pipelineItems, err := parsePipeline(forge, 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 {
|
2024-04-16 06:04:55 +00:00
|
|
|
updatePipelineStatus(c, forge, 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)
|
2024-04-16 06:04:55 +00:00
|
|
|
err = updatePipelinePending(c, forge, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 19:07:45 +00:00
|
|
|
// the workflows in the pipeline should be empty as only we do populate them,
|
|
|
|
// but if a pipeline was already loaded form database it might contain things, so we just clean it
|
|
|
|
pipeline.Workflows = nil
|
2023-10-08 12:05:06 +00:00
|
|
|
for _, item := range pipelineItems {
|
|
|
|
for _, stage := range item.Config.Stages {
|
|
|
|
for _, step := range stage.Steps {
|
|
|
|
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
|
|
|
|
}
|
2024-02-22 14:48:29 +00:00
|
|
|
if pipeline.Status == model.StatusBlocked {
|
|
|
|
step.State = model.StatusBlocked
|
|
|
|
}
|
2023-10-08 12:05:06 +00:00
|
|
|
item.Workflow.Children = append(item.Workflow.Children, step)
|
|
|
|
}
|
|
|
|
}
|
2024-02-22 14:48:29 +00:00
|
|
|
if pipeline.Status == model.StatusBlocked {
|
|
|
|
item.Workflow.State = model.StatusBlocked
|
|
|
|
}
|
2023-10-08 12:05:06 +00:00
|
|
|
item.Workflow.PipelineID = pipeline.ID
|
|
|
|
pipeline.Workflows = append(pipeline.Workflows, item.Workflow)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipeline
|
|
|
|
}
|