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"
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2022-11-06 11:44:04 +00:00
|
|
|
forge_types "github.com/woodpecker-ci/woodpecker/server/forge/types"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
|
|
|
)
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
func createPipelineItems(ctx context.Context, store store.Store,
|
|
|
|
currentPipeline *model.Pipeline, user *model.User, repo *model.Repo,
|
|
|
|
yamls []*forge_types.FileMeta, envs map[string]string,
|
|
|
|
) (*model.Pipeline, []*pipeline.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 {
|
|
|
|
log.Error().Err(err).Msg("Failed to generate netrc file")
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-11-06 11:44:04 +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
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
secs, err := server.Config.Services.Secrets.SecretListPipeline(repo, currentPipeline)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2022-11-06 11:44:04 +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
|
|
|
}
|
|
|
|
|
|
|
|
regs, err := server.Config.Services.Registries.RegistryList(repo)
|
|
|
|
if err != nil {
|
2022-11-06 11:44:04 +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
|
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
b := pipeline.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,
|
|
|
|
Link: server.Config.Server.Host,
|
|
|
|
Yamls: yamls,
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineItems, err := b.Build()
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
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 {
|
2022-11-06 11:44:04 +00:00
|
|
|
log.Error().Err(err).Msgf("Error setting error status of pipeline for %s#%d", repo.FullName, currentPipeline.Number)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
2022-11-06 11:44:04 +00:00
|
|
|
return currentPipeline, nil, err
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
currentPipeline = pipeline.SetPipelineStepsOnPipeline(b.Curr, pipelineItems)
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
return currentPipeline, pipelineItems, nil
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|