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.
This commit is contained in:
smainz 2022-11-22 23:57:12 +01:00 committed by GitHub
parent e7c8ed00e6
commit b8900cdf88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 63 deletions

View file

@ -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_BUILD_STATUS"] = "failure"
state.Pipeline.Step.Environment["CI_JOB_STATUS"] = "failure" state.Pipeline.Step.Environment["CI_JOB_STATUS"] = "failure"
} }
return nil return nil
}) })

View file

@ -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
}
}

View file

@ -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"]
}

View file

@ -246,8 +246,6 @@ func (m *Metadata) Environ() map[string]string {
params["CI_COMMIT_PULL_REQUEST"] = pullRegexp.FindString(m.Curr.Commit.Ref) params["CI_COMMIT_PULL_REQUEST"] = pullRegexp.FindString(m.Curr.Commit.Ref)
} }
m.setDroneEnviron(params)
return params return params
} }

View file

@ -175,6 +175,9 @@ func (r *Runtime) execAll(steps []*backend.Step) <-chan error {
return err return err
} }
// add compatibility for drone-ci plugins
SetDroneEnviron(step.Environment)
logger.Debug(). logger.Debug().
Str("Step", step.Name). Str("Step", step.Name).
Msg("Executing") Msg("Executing")

View file

@ -21,7 +21,7 @@ func (f TraceFunc) Trace(state *State) error {
// DefaultTracer provides a tracer that updates the CI_ environment // DefaultTracer provides a tracer that updates the CI_ environment
// variables to include the correct timestamp and status. // 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 { var DefaultTracer = TraceFunc(func(state *State) error {
if state.Process.Exited { if state.Process.Exited {
return nil return nil