From b8900cdf8811d18edd392b1d678f8e16fe62a25f Mon Sep 17 00:00:00 2001 From: smainz Date: Tue, 22 Nov 2022 23:57:12 +0100 Subject: [PATCH] Fix wrong drone env vars (#1419) Provide up to date drone compatibility environment variables to each step execution. closes #1416 Before a step is executed, some environemnt variables are updated. This ensures, that the updated environment variables are copied to their corresponding `DRONE_` environemt variables. Side effect is that the `DRONE_` environemnt variables are no longer available in the metadata which should not harm as they are not used inside woodpecker. --- agent/runner.go | 1 + pipeline/drone_compatibility.go | 71 ++++++++++++++++++++++++ pipeline/frontend/drone_compatibility.go | 60 -------------------- pipeline/frontend/metadata.go | 2 - pipeline/pipeline.go | 3 + pipeline/tracer.go | 2 +- 6 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 pipeline/drone_compatibility.go delete mode 100644 pipeline/frontend/drone_compatibility.go diff --git a/agent/runner.go b/agent/runner.go index d974024d4..a4d7081bc 100644 --- a/agent/runner.go +++ b/agent/runner.go @@ -316,6 +316,7 @@ func (r *Runner) Run(ctx context.Context) error { state.Pipeline.Step.Environment["CI_BUILD_STATUS"] = "failure" state.Pipeline.Step.Environment["CI_JOB_STATUS"] = "failure" } + return nil }) diff --git a/pipeline/drone_compatibility.go b/pipeline/drone_compatibility.go new file mode 100644 index 000000000..11d11e591 --- /dev/null +++ b/pipeline/drone_compatibility.go @@ -0,0 +1,71 @@ +// 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 + +// SetDroneEnviron set dedicated to DroneCI environment vars as compatibility +// layer. Main purpose is to be compatible with drone plugins. +func SetDroneEnviron(env map[string]string) { + // webhook + copy("CI_COMMIT_BRANCH", "DRONE_BRANCH", env) + copy("CI_COMMIT_PULL_REQUEST", "DRONE_PULL_REQUEST", env) + copy("CI_COMMIT_TAG", "DRONE_TAG", env) + copy("CI_COMMIT_SOURCE_BRANCH", "DRONE_SOURCE_BRANCH", env) + copy("CI_COMMIT_TARGET_BRANCH", "DRONE_TARGET_BRANCH", env) + // pipeline + copy("CI_PIPELINE_NUMBER", "DRONE_BUILD_NUMBER", env) + copy("CI_PIPELINE_PARENT", "DRONE_BUILD_PARENT", env) + copy("CI_PIPELINE_EVENT", "DRONE_BUILD_EVENT", env) + copy("CI_PIPELINE_STATUS", "DRONE_BUILD_STATUS", env) + copy("CI_PIPELINE_LINK", "DRONE_BUILD_LINK", env) + copy("CI_PIPELINE_CREATED", "DRONE_BUILD_CREATED", env) + copy("CI_PIPELINE_STARTED", "DRONE_BUILD_STARTED", env) + copy("CI_PIPELINE_FINISHED", "DRONE_BUILD_FINISHED", env) + // commit + copy("CI_COMMIT_SHA", "DRONE_COMMIT", env) + copy("CI_COMMIT_SHA", "DRONE_COMMIT_SHA", env) + copy("CI_PREV_COMMIT_SHA", "DRONE_COMMIT_BEFORE", env) + copy("CI_COMMIT_REF", "DRONE_COMMIT_REF", env) + copy("CI_COMMIT_BRANCH", "DRONE_COMMIT_BRANCH", env) + copy("CI_COMMIT_LINK", "DRONE_COMMIT_LINK", env) + copy("CI_COMMIT_MESSAGE", "DRONE_COMMIT_MESSAGE", env) + copy("CI_COMMIT_AUTHOR", "DRONE_COMMIT_AUTHOR", env) + copy("CI_COMMIT_AUTHOR", "DRONE_COMMIT_AUTHOR_NAME", env) + copy("CI_COMMIT_AUTHOR_EMAIL", "DRONE_COMMIT_AUTHOR_EMAIL", env) + copy("CI_COMMIT_AUTHOR_AVATAR", "DRONE_COMMIT_AUTHOR_AVATAR", env) + // repo + copy("CI_REPO", "DRONE_REPO", env) + copy("CI_REPO_SCM", "DRONE_REPO_SCM", env) + copy("CI_REPO_OWNER", "DRONE_REPO_OWNER", env) + copy("CI_REPO_NAME", "DRONE_REPO_NAME", env) + copy("CI_REPO_LINK", "DRONE_REPO_LINK", env) + copy("CI_REPO_DEFAULT_BRANCH", "DRONE_REPO_BRANCH", env) + copy("CI_REPO_PRIVATE", "DRONE_REPO_PRIVATE", env) + // clone + copy("CI_REPO_CLONE_URL", "DRONE_REMOTE_URL", env) + copy("CI_REPO_CLONE_URL", "DRONE_GIT_HTTP_URL", env) + // misc + copy("CI_SYSTEM_HOST", "DRONE_SYSTEM_HOST", env) + copy("CI_STEP_NUMBER", "DRONE_STEP_NUMBER", env) +} + +func copy(woodpecker, drone string, env map[string]string) { + var present bool + var value string + + value, present = env[woodpecker] + if present { + env[drone] = value + } +} diff --git a/pipeline/frontend/drone_compatibility.go b/pipeline/frontend/drone_compatibility.go deleted file mode 100644 index 687440e44..000000000 --- a/pipeline/frontend/drone_compatibility.go +++ /dev/null @@ -1,60 +0,0 @@ -// 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 frontend - -// setDroneEnviron set dedicated to DroneCI environment vars as compatibility layer -func (m *Metadata) setDroneEnviron(env map[string]string) { - // webhook - env["DRONE_BRANCH"] = env["CI_COMMIT_BRANCH"] - env["DRONE_PULL_REQUEST"] = env["CI_COMMIT_PULL_REQUEST"] - env["DRONE_TAG"] = env["CI_COMMIT_TAG"] - env["DRONE_SOURCE_BRANCH"] = env["CI_COMMIT_SOURCE_BRANCH"] - env["DRONE_TARGET_BRANCH"] = env["CI_COMMIT_TARGET_BRANCH"] - // pipeline - env["DRONE_BUILD_NUMBER"] = env["CI_PIPELINE_NUMBER"] - env["DRONE_BUILD_PARENT"] = env["CI_PIPELINE_PARENT"] - env["DRONE_BUILD_EVENT"] = env["CI_PIPELINE_EVENT"] - env["DRONE_BUILD_STATUS"] = env["CI_PIPELINE_STATUS"] - env["DRONE_BUILD_LINK"] = env["CI_PIPELINE_LINK"] - env["DRONE_BUILD_CREATED"] = env["CI_PIPELINE_CREATED"] - env["DRONE_BUILD_STARTED"] = env["CI_PIPELINE_STARTED"] - env["DRONE_BUILD_FINISHED"] = env["CI_PIPELINE_FINISHED"] - // commit - env["DRONE_COMMIT"] = env["CI_COMMIT_SHA"] - env["DRONE_COMMIT_SHA"] = env["CI_COMMIT_SHA"] - env["DRONE_COMMIT_BEFORE"] = env["CI_PREV_COMMIT_SHA"] - env["DRONE_COMMIT_REF"] = env["CI_COMMIT_REF"] - env["DRONE_COMMIT_BRANCH"] = env["CI_COMMIT_BRANCH"] - env["DRONE_COMMIT_LINK"] = env["CI_COMMIT_LINK"] - env["DRONE_COMMIT_MESSAGE"] = env["CI_COMMIT_MESSAGE"] - env["DRONE_COMMIT_AUTHOR"] = env["CI_COMMIT_AUTHOR"] - env["DRONE_COMMIT_AUTHOR_NAME"] = env["CI_COMMIT_AUTHOR"] - env["DRONE_COMMIT_AUTHOR_EMAIL"] = env["CI_COMMIT_AUTHOR_EMAIL"] - env["DRONE_COMMIT_AUTHOR_AVATAR"] = env["CI_COMMIT_AUTHOR_AVATAR"] - // repo - env["DRONE_REPO"] = env["CI_REPO"] - env["DRONE_REPO_SCM"] = env["CI_REPO_SCM"] - env["DRONE_REPO_OWNER"] = env["CI_REPO_OWNER"] - env["DRONE_REPO_NAME"] = env["CI_REPO_NAME"] - env["DRONE_REPO_LINK"] = env["CI_REPO_LINK"] - env["DRONE_REPO_BRANCH"] = env["CI_REPO_DEFAULT_BRANCH"] - env["DRONE_REPO_PRIVATE"] = env["CI_REPO_PRIVATE"] - // clone - env["DRONE_REMOTE_URL"] = env["CI_REPO_CLONE_URL"] - env["DRONE_GIT_HTTP_URL"] = env["CI_REPO_CLONE_URL"] - // misc - env["DRONE_SYSTEM_HOST"] = env["CI_SYSTEM_HOST"] - env["DRONE_STEP_NUMBER"] = env["CI_STEP_NUMBER"] -} diff --git a/pipeline/frontend/metadata.go b/pipeline/frontend/metadata.go index 197ccf1af..7797c09cf 100644 --- a/pipeline/frontend/metadata.go +++ b/pipeline/frontend/metadata.go @@ -246,8 +246,6 @@ func (m *Metadata) Environ() map[string]string { params["CI_COMMIT_PULL_REQUEST"] = pullRegexp.FindString(m.Curr.Commit.Ref) } - m.setDroneEnviron(params) - return params } diff --git a/pipeline/pipeline.go b/pipeline/pipeline.go index 572ea84fb..1ae1a2eea 100644 --- a/pipeline/pipeline.go +++ b/pipeline/pipeline.go @@ -175,6 +175,9 @@ func (r *Runtime) execAll(steps []*backend.Step) <-chan error { return err } + // add compatibility for drone-ci plugins + SetDroneEnviron(step.Environment) + logger.Debug(). Str("Step", step.Name). Msg("Executing") diff --git a/pipeline/tracer.go b/pipeline/tracer.go index 38c338b80..8c4b5a6fe 100644 --- a/pipeline/tracer.go +++ b/pipeline/tracer.go @@ -21,7 +21,7 @@ func (f TraceFunc) Trace(state *State) error { // DefaultTracer provides a tracer that updates the CI_ environment // variables to include the correct timestamp and status. -// TODO(bradrydzewski) find either a new home or better name for this. +// TODO: find either a new home or better name for this. var DefaultTracer = TraceFunc(func(state *State) error { if state.Process.Exited { return nil