2022-10-18 01:24:12 +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.
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2017-03-10 10:58:25 +00:00
|
|
|
"net/url"
|
2023-11-01 10:29:44 +00:00
|
|
|
"path"
|
2017-03-05 07:56:08 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option configures a compiler option.
|
|
|
|
type Option func(*Compiler)
|
|
|
|
|
2023-08-18 11:09:28 +00:00
|
|
|
func noopOption() Option {
|
|
|
|
return func(*Compiler) {}
|
|
|
|
}
|
|
|
|
|
2017-03-19 09:07:21 +00:00
|
|
|
// WithOption configures the compiler with the given option if
|
|
|
|
// boolean b evaluates to true.
|
|
|
|
func WithOption(option Option, b bool) Option {
|
|
|
|
switch {
|
|
|
|
case b:
|
|
|
|
return option
|
|
|
|
default:
|
2024-02-08 21:49:07 +00:00
|
|
|
return func(_ *Compiler) {}
|
2017-03-19 09:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 16:35:56 +00:00
|
|
|
// WithVolumes configures the compiler with default volumes that
|
2017-03-05 07:56:08 +00:00
|
|
|
// are mounted to each container in the pipeline.
|
|
|
|
func WithVolumes(volumes ...string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.volumes = volumes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 16:04:25 +00:00
|
|
|
// WithRegistry configures the compiler with registry credentials
|
|
|
|
// that should be used to download images.
|
|
|
|
func WithRegistry(registries ...Registry) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.registries = registries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:39:50 +00:00
|
|
|
// WithSecret configures the compiler with external secrets
|
|
|
|
// to be injected into the container at runtime.
|
|
|
|
func WithSecret(secrets ...Secret) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
for _, secret := range secrets {
|
|
|
|
compiler.secrets[strings.ToLower(secret.Name)] = secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// WithMetadata configures the compiler with the repository, pipeline
|
2017-03-05 07:56:08 +00:00
|
|
|
// and system metadata. The metadata is used to remove steps from
|
|
|
|
// the compiled pipeline configuration that should be skipped. The
|
|
|
|
// metadata is also added to each container as environment variables.
|
2023-06-04 22:15:07 +00:00
|
|
|
func WithMetadata(metadata metadata.Metadata) Option {
|
2017-03-05 07:56:08 +00:00
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.metadata = metadata
|
|
|
|
|
|
|
|
for k, v := range metadata.Environ() {
|
|
|
|
compiler.env[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNetrc configures the compiler with netrc authentication
|
|
|
|
// credentials added by default to every container in the pipeline.
|
|
|
|
func WithNetrc(username, password, machine string) Option {
|
2021-11-25 20:30:03 +00:00
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.cloneEnv["CI_NETRC_USERNAME"] = username
|
|
|
|
compiler.cloneEnv["CI_NETRC_PASSWORD"] = password
|
|
|
|
compiler.cloneEnv["CI_NETRC_MACHINE"] = machine
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithWorkspace configures the compiler with the workspace base
|
|
|
|
// and path. The workspace base is a volume created at runtime and
|
|
|
|
// mounted into all containers in the pipeline. The base and path
|
2022-10-18 01:24:12 +00:00
|
|
|
// are joined to provide the working directory for all pipeline and
|
2017-03-05 07:56:08 +00:00
|
|
|
// plugin steps in the pipeline.
|
|
|
|
func WithWorkspace(base, path string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
2024-07-18 20:52:22 +00:00
|
|
|
compiler.workspaceBase = base
|
|
|
|
compiler.workspacePath = path
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:58:25 +00:00
|
|
|
// WithWorkspaceFromURL configures the compiler with the workspace
|
|
|
|
// base and path based on the repository url.
|
2023-11-14 16:12:12 +00:00
|
|
|
func WithWorkspaceFromURL(base, u string) Option {
|
2023-11-01 10:29:44 +00:00
|
|
|
srcPath := "src"
|
2023-11-14 16:12:12 +00:00
|
|
|
parsed, err := url.Parse(u)
|
2017-03-10 10:58:25 +00:00
|
|
|
if err == nil {
|
2023-11-01 10:29:44 +00:00
|
|
|
srcPath = path.Join(srcPath, parsed.Hostname(), parsed.Path)
|
2017-03-10 10:58:25 +00:00
|
|
|
}
|
2023-11-01 10:29:44 +00:00
|
|
|
return WithWorkspace(base, srcPath)
|
2017-03-10 10:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
// WithEscalated configures the compiler to automatically execute
|
|
|
|
// images as privileged containers if the match the given list.
|
|
|
|
func WithEscalated(images ...string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.escalated = images
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPrefix configures the compiler with the prefix. The prefix is
|
|
|
|
// used to prefix container, volume and network names to avoid
|
|
|
|
// collision at runtime.
|
|
|
|
func WithPrefix(prefix string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.prefix = prefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLocal configures the compiler with the local flag. The local
|
|
|
|
// flag indicates the pipeline execution is running in a local development
|
|
|
|
// environment with a mounted local working directory.
|
|
|
|
func WithLocal(local bool) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.local = local
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithEnviron configures the compiler with environment variables
|
|
|
|
// added by default to every container in the pipeline.
|
|
|
|
func WithEnviron(env map[string]string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
for k, v := range env {
|
|
|
|
compiler.env[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 16:35:56 +00:00
|
|
|
// WithNetworks configures the compiler with additional networks
|
2024-05-13 20:58:21 +00:00
|
|
|
// to be connected to pipeline containers.
|
2017-04-10 16:27:34 +00:00
|
|
|
func WithNetworks(networks ...string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.networks = networks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:06:28 +00:00
|
|
|
// WithResourceLimit configures the compiler with default resource limits that
|
|
|
|
// are applied each container in the pipeline.
|
2024-05-24 20:35:04 +00:00
|
|
|
func WithResourceLimit(swap, mem, shmSize, cpuQuota, cpuShares int64, cpuSet string) Option {
|
2017-06-22 19:06:28 +00:00
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.reslimit = ResourceLimit{
|
|
|
|
MemSwapLimit: swap,
|
|
|
|
MemLimit: mem,
|
2024-05-24 20:35:04 +00:00
|
|
|
ShmSize: shmSize,
|
2017-06-22 19:06:28 +00:00
|
|
|
CPUQuota: cpuQuota,
|
|
|
|
CPUShares: cpuShares,
|
|
|
|
CPUSet: cpuSet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:05:19 +00:00
|
|
|
func WithDefaultCloneImage(cloneImage string) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.defaultCloneImage = cloneImage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// WithTrusted configures the compiler with the trusted repo option.
|
2023-03-20 20:17:49 +00:00
|
|
|
func WithTrusted(trusted bool) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.trustedPipeline = trusted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// WithNetrcOnlyTrusted configures the compiler with the netrcOnlyTrusted repo option.
|
2023-03-20 20:17:49 +00:00
|
|
|
func WithNetrcOnlyTrusted(only bool) Option {
|
|
|
|
return func(compiler *Compiler) {
|
|
|
|
compiler.netrcOnlyTrusted = only
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 19:13:26 +00:00
|
|
|
type ProxyOptions struct {
|
|
|
|
NoProxy string
|
|
|
|
HTTPProxy string
|
|
|
|
HTTPSProxy string
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2023-08-07 19:13:26 +00:00
|
|
|
// WithProxy configures the compiler with HTTP_PROXY, HTTPS_PROXY,
|
|
|
|
// and NO_PROXY environment variables added by default to every
|
|
|
|
// container in the pipeline.
|
|
|
|
func WithProxy(opt ProxyOptions) Option {
|
2023-08-18 11:09:28 +00:00
|
|
|
if opt.HTTPProxy == "" &&
|
|
|
|
opt.HTTPSProxy == "" &&
|
|
|
|
opt.NoProxy == "" {
|
|
|
|
return noopOption()
|
|
|
|
}
|
2023-08-07 19:13:26 +00:00
|
|
|
return WithEnviron(
|
|
|
|
map[string]string{
|
|
|
|
"no_proxy": opt.NoProxy,
|
|
|
|
"NO_PROXY": opt.NoProxy,
|
|
|
|
"http_proxy": opt.HTTPProxy,
|
|
|
|
"HTTP_PROXY": opt.HTTPProxy,
|
|
|
|
"HTTPS_PROXY": opt.HTTPSProxy,
|
|
|
|
"https_proxy": opt.HTTPSProxy,
|
|
|
|
},
|
|
|
|
)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|