2022-11-23 14:35:24 +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 agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2024-08-30 09:44:56 +00:00
|
|
|
"sync"
|
2022-11-23 14:35:24 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
2022-11-23 14:35:24 +00:00
|
|
|
)
|
|
|
|
|
2024-08-30 09:44:56 +00:00
|
|
|
func (r *Runner) createTracer(ctxMeta context.Context, uploads *sync.WaitGroup, logger zerolog.Logger, workflow *rpc.Workflow) pipeline.TraceFunc {
|
2022-11-23 14:35:24 +00:00
|
|
|
return func(state *pipeline.State) error {
|
2024-08-30 09:44:56 +00:00
|
|
|
uploads.Add(1)
|
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
stepLogger := logger.With().
|
2022-11-23 14:35:24 +00:00
|
|
|
Str("image", state.Pipeline.Step.Image).
|
2024-08-30 09:44:56 +00:00
|
|
|
Str("workflow_id", workflow.ID).
|
2022-11-23 14:35:24 +00:00
|
|
|
Err(state.Process.Error).
|
|
|
|
Int("exit_code", state.Process.ExitCode).
|
|
|
|
Bool("exited", state.Process.Exited).
|
|
|
|
Logger()
|
|
|
|
|
2024-07-01 09:20:55 +00:00
|
|
|
stepState := rpc.StepState{
|
2024-01-09 14:39:09 +00:00
|
|
|
StepUUID: state.Pipeline.Step.UUID,
|
2022-11-23 14:35:24 +00:00
|
|
|
Exited: state.Process.Exited,
|
|
|
|
ExitCode: state.Process.ExitCode,
|
2024-05-13 20:58:21 +00:00
|
|
|
Started: time.Now().Unix(), // TODO: do not do this
|
2022-11-23 14:35:24 +00:00
|
|
|
Finished: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
if state.Process.Error != nil {
|
|
|
|
stepState.Error = state.Process.Error.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2024-05-24 20:35:04 +00:00
|
|
|
stepLogger.Debug().Msg("update step status")
|
2022-11-23 14:35:24 +00:00
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
if err := r.client.Update(ctxMeta, workflow.ID, stepState); err != nil {
|
|
|
|
stepLogger.Debug().
|
|
|
|
Err(err).
|
2022-11-23 14:35:24 +00:00
|
|
|
Msg("update step status error")
|
|
|
|
}
|
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
stepLogger.Debug().Msg("update step status complete")
|
2024-08-30 09:44:56 +00:00
|
|
|
uploads.Done()
|
2022-11-23 14:35:24 +00:00
|
|
|
}()
|
|
|
|
if state.Process.Exited {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if state.Pipeline.Step.Environment == nil {
|
|
|
|
state.Pipeline.Step.Environment = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: find better way to update this state and move it to pipeline to have the same env in cli-exec
|
|
|
|
state.Pipeline.Step.Environment["CI_MACHINE"] = r.hostname
|
|
|
|
|
|
|
|
state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = "success"
|
2024-07-01 09:20:55 +00:00
|
|
|
state.Pipeline.Step.Environment["CI_PIPELINE_STARTED"] = strconv.FormatInt(state.Pipeline.Started, 10)
|
2022-11-23 14:35:24 +00:00
|
|
|
state.Pipeline.Step.Environment["CI_PIPELINE_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
|
|
|
|
state.Pipeline.Step.Environment["CI_STEP_STATUS"] = "success"
|
2024-07-01 09:20:55 +00:00
|
|
|
state.Pipeline.Step.Environment["CI_STEP_STARTED"] = strconv.FormatInt(state.Pipeline.Started, 10)
|
2022-11-23 14:35:24 +00:00
|
|
|
state.Pipeline.Step.Environment["CI_STEP_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
|
2023-07-28 12:58:20 +00:00
|
|
|
state.Pipeline.Step.Environment["CI_SYSTEM_PLATFORM"] = runtime.GOOS + "/" + runtime.GOARCH
|
2022-11-23 14:35:24 +00:00
|
|
|
|
|
|
|
if state.Pipeline.Error != nil {
|
|
|
|
state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = "failure"
|
|
|
|
state.Pipeline.Step.Environment["CI_STEP_STATUS"] = "failure"
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|