Skip to content

Commit 0216047

Browse files
authored
feat: root directory support for git (#190)
* feat: in docker config generator support to add codepath has been added * chore: remove debug statements * feat: add support for provide codepath * feat: add support for provide codepath
1 parent 62bc65e commit 0216047

File tree

13 files changed

+130
-24
lines changed

13 files changed

+130
-24
lines changed

container_manager/image.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,30 @@ import (
55
"context"
66
"errors"
77
"os"
8+
"strings"
89

910
"github.com/docker/docker/api/types"
1011
"github.com/docker/docker/pkg/archive"
1112
)
1213

13-
/*
14-
CreateImage builds a Docker image from a Dockerfile and returns a scanner to read the build logs.
15-
It takes the Dockerfile content as a string, a map of build arguments, the path to the code directory, and the name of the image to be built.
16-
It returns a scanner to read the build logs and an error if any.
17-
*/
18-
func (m Manager) CreateImage(dockerfile string, buildargs map[string]string, codepath string, imagename string) (*bufio.Scanner, error) {
19-
return m.CreateImageWithContext(m.ctx, dockerfile, buildargs, codepath, imagename)
20-
}
21-
2214
/*
2315
CreateImageWithContext builds a Docker image from a Dockerfile and returns a scanner to read the build logs.
2416
It takes the Dockerfile content as a string, a map of build arguments, the path to the code directory, and the name of the image to be built.
2517
It returns a scanner to read the build logs and an error if any.
2618
It takes a context.Context as an additional argument.
2719
*/
28-
func (m Manager) CreateImageWithContext(ctx context.Context, dockerfile string, buildargs map[string]string, codepath string, imagename string) (*bufio.Scanner, error) {
29-
// Move the dockerfile to the codepath
30-
err := os.WriteFile(codepath+"/Dockerfile", []byte(dockerfile), 0777)
20+
func (m Manager) CreateImageWithContext(ctx context.Context, dockerfile string, buildargs map[string]string, sourceCodeDirectory string, codePath string, imagename string) (*bufio.Scanner, error) {
21+
// add path
22+
codePath = strings.TrimSpace(codePath)
23+
if codePath != "" && codePath != "/" {
24+
sourceCodeDirectory = sourceCodeDirectory + "/" + codePath
25+
sourceCodeDirectory = strings.ReplaceAll(sourceCodeDirectory, "\\", "/")
26+
sourceCodeDirectory = strings.ReplaceAll(sourceCodeDirectory, "//", "/")
27+
sourceCodeDirectory = strings.ReplaceAll(sourceCodeDirectory, "../", "")
28+
sourceCodeDirectory = strings.ReplaceAll(sourceCodeDirectory, "./", "")
29+
}
30+
// Move the dockerfile to the sourceCodeDirectory
31+
err := os.WriteFile(sourceCodeDirectory+"/Dockerfile", []byte(dockerfile), 0777)
3132
if err != nil {
3233
return nil, errors.New("failed to write the dockerfile")
3334
}
@@ -40,10 +41,10 @@ func (m Manager) CreateImageWithContext(ctx context.Context, dockerfile string,
4041
*ptrValue = string(valueBytes)
4142
final_buildargs[key] = ptrValue
4243
}
43-
// tar the codepath
44-
tar, err := archive.TarWithOptions(codepath, &archive.TarOptions{})
44+
// tar the sourceCodeDirectory
45+
tar, err := archive.TarWithOptions(sourceCodeDirectory, &archive.TarOptions{})
4546
if err != nil {
46-
return nil, errors.New("failed to tar the codepath")
47+
return nil, errors.New("failed to tar the sourceCodeDirectory")
4748
}
4849
// Build the image
4950
response, err := m.client.ImageBuild(ctx, tar, types.ImageBuildOptions{

docker_config_generator/utils.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// Generate DockerConfig from git repository.
16-
func (m Manager) GenerateConfigFromGitRepository(git_url string, branch string, username string, password string) (DockerFileConfig, error) {
16+
func (m Manager) GenerateConfigFromGitRepository(git_url string, branch string, codePath string, username string, password string) (DockerFileConfig, error) {
1717
tmpFolder := "/tmp/" + uuid.New().String()
1818
if os.Mkdir(tmpFolder, 0777) != nil {
1919
return DockerFileConfig{}, errors.New("failed to create tmp folder")
@@ -25,7 +25,7 @@ func (m Manager) GenerateConfigFromGitRepository(git_url string, branch string,
2525
return DockerFileConfig{}, errors.New("failed to clone repository")
2626
}
2727
// Generate config from source code directory
28-
return m.generateConfigFromSourceCodeDirectory(tmpFolder)
28+
return m.generateConfigFromSourceCodeDirectory(tmpFolder, codePath)
2929
}
3030

3131
// Generate DockerConfig from source code .tar file.
@@ -39,11 +39,20 @@ func (m Manager) GenerateConfigFromSourceCodeTar(tarFile string) (DockerFileConf
3939
return DockerFileConfig{}, errors.New("failed to extract tar file")
4040
}
4141
// Generate config from source code directory
42-
return m.generateConfigFromSourceCodeDirectory(tmpFolder)
42+
return m.generateConfigFromSourceCodeDirectory(tmpFolder, "")
4343
}
4444

4545
// Generate DockerConfig from source code directory.
46-
func (m Manager) generateConfigFromSourceCodeDirectory(directory string) (DockerFileConfig, error) {
46+
func (m Manager) generateConfigFromSourceCodeDirectory(directory string, codePath string) (DockerFileConfig, error) {
47+
// add path
48+
codePath = strings.TrimSpace(codePath)
49+
if codePath != "" && codePath != "/" {
50+
directory = directory + "/" + codePath
51+
directory = strings.ReplaceAll(directory, "\\", "/")
52+
directory = strings.ReplaceAll(directory, "//", "/")
53+
directory = strings.ReplaceAll(directory, "../", "")
54+
directory = strings.ReplaceAll(directory, "./", "")
55+
}
4756
// Try to find docker file
4857
file, err := os.ReadFile(directory + "/Dockerfile")
4958
if err != nil {

swiftwave_service/core/application.operations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func (application *Application) Create(ctx context.Context, db gorm.DB, dockerMa
172172
RepositoryName: application.LatestDeployment.RepositoryName,
173173
RepositoryBranch: application.LatestDeployment.RepositoryBranch,
174174
CommitHash: application.LatestDeployment.CommitHash,
175+
CodePath: application.LatestDeployment.CodePath,
175176
// Fields for UpstreamType = SourceCode
176177
SourceCodeCompressedFileName: application.LatestDeployment.SourceCodeCompressedFileName,
177178
// Fields for UpstreamType = Image

swiftwave_service/core/deployment.operations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (deployment *Deployment) Update(ctx context.Context, db gorm.DB) (*Deployme
105105
deployment.RepositoryName != latestDeployment.RepositoryName ||
106106
deployment.RepositoryBranch != latestDeployment.RepositoryBranch ||
107107
deployment.CommitHash != latestDeployment.CommitHash ||
108+
deployment.CodePath != latestDeployment.CodePath ||
108109
deployment.SourceCodeCompressedFileName != latestDeployment.SourceCodeCompressedFileName ||
109110
deployment.DockerImage != latestDeployment.DockerImage ||
110111
deployment.ImageRegistryCredentialID != latestDeployment.ImageRegistryCredentialID ||

swiftwave_service/core/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type Deployment struct {
132132
RepositoryOwner string `json:"repositoryOwner"`
133133
RepositoryName string `json:"repositoryName"`
134134
RepositoryBranch string `json:"repositoryBranch"`
135+
CodePath string `json:"codePath"`
135136
CommitHash string `json:"commitHash"`
136137
// Fields for UpstreamType = SourceCode
137138
SourceCodeCompressedFileName string `json:"sourceCodeCompressedFileName"`

swiftwave_service/graphql/docker_config_generator.resolvers.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swiftwave_service/graphql/generated.go

Lines changed: 83 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swiftwave_service/graphql/graphql_object_mapper.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func applicationInputToDeploymentDatabaseObject(record *model.ApplicationInput)
131131
RepositoryName: DefaultString(record.RepositoryName, ""),
132132
RepositoryBranch: DefaultString(record.RepositoryBranch, ""),
133133
CommitHash: "",
134+
CodePath: DefaultString(record.CodePath, ""),
134135
SourceCodeCompressedFileName: DefaultString(record.SourceCodeCompressedFileName, ""),
135136
DockerImage: DefaultString(record.DockerImage, ""),
136137
ImageRegistryCredentialID: record.ImageRegistryCredentialID,
@@ -195,6 +196,7 @@ func deploymentToGraphqlObject(record *core.Deployment) *model.Deployment {
195196
RepositoryName: record.RepositoryName,
196197
RepositoryBranch: record.RepositoryBranch,
197198
CommitHash: record.CommitHash,
199+
CodePath: record.CodePath,
198200
SourceCodeCompressedFileName: record.SourceCodeCompressedFileName,
199201
DockerImage: record.DockerImage,
200202
ImageRegistryCredentialID: imageRegistryCredentialId,

swiftwave_service/graphql/model/models_gen.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swiftwave_service/graphql/schema/application.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ input ApplicationInput {
3939
repositoryOwner: String
4040
repositoryName: String
4141
repositoryBranch: String
42+
codePath: String
4243
# required for upstreamType = "SourceCode"
4344
sourceCodeCompressedFileName: String
4445
# required for upstreamType = "Image"

0 commit comments

Comments
 (0)