builder

package
v0.0.0-...-5e0d270 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const DockerSrc = "/src/"

Variables

View Source
var DotnetBuilder = &Builder{
	BuilderNames: []string{"dotnet"},
	GetBuildArguments: func(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error) {
		builderConfig := &DotnetBuilderConfig{}
		err := json.Unmarshal(conf.Builder, builderConfig)
		if err != nil {
			return nil, err
		}

		projectFile, err := findProjectFileInPath(conf.ProjectPath)
		if err != nil {
			return nil, err
		}

		log.Printf("Found projectfile in %s", projectFile.Name())

		projectDependencies, err := findProjectDependenciesInProjectFile(conf.ProjectPath, projectFile.Name())

		if err != nil {
			return nil, err
		}

		copyProjectDependenciesProjectFiles := getDockerCopyCommandForDependency(projectDependencies)
		projectDir := path.Join(DockerSrc, conf.ProjectPath)
		copyProjectDependencies := getDockerCopyCommandForDependency(getDirOfPaths(projectDependencies))
		projectName := strings.Replace(projectFile.Name(), ".csproj", "", 1)
		dockerRuntimeImage, err := getDotnetRuntime(builderConfig, projectDependencies)

		if err != nil {
			return nil, err
		}

		dockercontent := fmt.Sprintf(`
			 FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env

			# Copy csproj and restore as distinct layers
			%s
		
			WORKDIR %s
			RUN dotnet restore
		
			# Copy everything else and build
			%s
		
			RUN dotnet publish -c Release -o out
		
			# Build runtime image
			FROM %s:6.0
			WORKDIR /app
			COPY --from=build-env %s/out .
			ENTRYPOINT ["dotnet", "%s.dll"]
		`, copyProjectDependenciesProjectFiles, projectDir, copyProjectDependencies, dockerRuntimeImage, projectDir, projectName)

		tmpDir, err := ioutil.TempDir("", "")

		if err != nil {
			return nil, err
		}

		dockerFilePath := path.Join(tmpDir, "Dockerfile")
		bytesToSend := []byte(dockercontent)
		err = os.WriteFile(dockerFilePath, bytesToSend, 0755)
		if err != nil {
			return nil, err
		}

		arguments := &BuildArguments{
			DockerBuildContextPaths: map[string]string{
				".":    "",
				tmpDir: "",
			},
		}

		return arguments, nil
	},
}
View Source
var GoBuilder = &Builder{
	BuilderNames: []string{"go"},
	GetBuildArguments: func(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error) {
		builderConfig := &GoBuilderConfig{}
		err := json.Unmarshal(conf.Builder, builderConfig)
		if err != nil {
			return nil, err
		}

		arguments := &BuildArguments{
			DockerBuildContextPaths: map[string]string{
				".": "",
			},
		}

		return arguments, nil
	},
}
View Source
var ManualBuilder = &Builder{
	BuilderNames: []string{"manual", ""},
	GetBuildArguments: func(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error) {
		builderConfig := &ManualBuilderConf{}
		err := json.Unmarshal(conf.Builder, builderConfig)
		if err != nil {
			return nil, err
		}

		contextPaths := map[string]string{}

		if builderConfig.BuildContext == "root" || builderConfig.BuildContext == "" {
			return &BuildArguments{
				DockerBuildContextPaths: map[string]string{
					".": "",
				},
				DockerFilePath: filepath.Join(conf.ProjectPath, builderConfig.DockerFile),
			}, nil

		} else if builderConfig.BuildContext == "projectdir" {
			return &BuildArguments{
				DockerBuildContextPaths: map[string]string{
					".": "",
				},
				DockerFilePath: filepath.Join(conf.ProjectPath, builderConfig.DockerFile),
			}, nil
			contextPaths[conf.ProjectPath] = ""
		}

		return nil, fmt.Errorf("invalid buildContext value. given %s", builderConfig.BuildContext)
	},
}
View Source
var NodeBuilder = &Builder{
	BuilderNames: []string{"nodejs"},
	GetBuildArguments: func(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error) {
		builderConfig := &NodejsBuilderConfig{}
		err := json.Unmarshal(conf.Builder, builderConfig)
		if err != nil {
			return nil, err
		}

		nodeProjectType, err := GetNodeProjectType(conf)

		if err != nil {
			return nil, err
		}

		if nodeProjectType == Unknown {
			return nil, errors.New(fmt.Sprintf("Could not get node project type for project %s at path %s", conf.ServiceName, conf.ProjectPath))
		}

		lockFile := "yarn.lock"
		if nodeProjectType == Npm {
			lockFile = "package-lock.json"
		}

		installCommand := getInstallCommand(nodeProjectType)

		buildCommand := ""
		if builderConfig.BuildCommand != "" {
			buildCommand = fmt.Sprintf("RUN %s", builderConfig.BuildCommand)
		}

		if builderConfig.RunCommand == "" {
			return nil, errors.New(fmt.Sprintf("No runcommand given in project %s at path %s", conf.ServiceName, conf.ProjectPath))
		}

		dockercontent := fmt.Sprintf(`
			FROM node:%s-alpine

			WORKDIR /usr/src/app
			COPY package.json %s ./

			%s

			COPY / ./

			%s

			CMD %s
		`, builderConfig.NodeVersion, lockFile, installCommand, buildCommand, builderConfig.RunCommand)

		tmpDir, err := ioutil.TempDir("", "")

		if err != nil {
			return nil, err
		}

		dockerFilePath := path.Join(tmpDir, "Dockerfile")
		bytesToSend := []byte(dockercontent)
		err = os.WriteFile(dockerFilePath, bytesToSend, 0755)
		if err != nil {
			return nil, err
		}

		arguments := &BuildArguments{
			DockerBuildContextPaths: map[string]string{
				conf.ProjectPath: "",
				tmpDir:           "",
			},
		}

		return arguments, nil
	},
}

Functions

This section is empty.

Types

type BuildArguments

type BuildArguments struct {
	DockerBuildContextPaths map[string]string
	DockerFilePath          string
}

type Builder

type Builder struct {
	BuilderNames      []string
	GetBuildArguments func(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error)
}

type BuilderManager

type BuilderManager struct {
	Builders []*Builder
}

func (*BuilderManager) GetBuildArgumentsForProject

func (m *BuilderManager) GetBuildArgumentsForProject(conf structs.ConfigurationWithProjectPath) (*BuildArguments, error)

func (*BuilderManager) PrintBuilders

func (m *BuilderManager) PrintBuilders()

type DotnetBuilderConfig

type DotnetBuilderConfig struct {
	Type          string `json:"type"`
	DotnetRuntime string `json:"dotnetruntime"`
}

type GoBuilderConfig

type GoBuilderConfig struct {
	Type string `json:"type"`
}

type ManualBuilderConf

type ManualBuilderConf struct {
	BuildContext string `json:buildcontext`
	DockerFile   string `json:dockerfile`
}

type NodeProjectType

type NodeProjectType int
const (
	Unknown NodeProjectType = iota
	Npm
	Yarn
)

type NodejsBuilderConfig

type NodejsBuilderConfig struct {
	Type         string `json:"type"`
	NodeVersion  string `json:"nodeversion"`
	BuildCommand string `json:"buildcommand"`
	RunCommand   string `json:"runcommand"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL