Skip to content

Commit

Permalink
Merge pull request #913 from traPtitech/ignore-dockerignore
Browse files Browse the repository at this point in the history
Commandビルドの場合は.dockerignoreを無視する
  • Loading branch information
pirosiki197 committed May 12, 2024
2 parents 9fbbdb7 + 9ca5ee2 commit ebc3d0d
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion pkg/usecase/builder/build_buildkit.go
Expand Up @@ -24,7 +24,8 @@ import (
)

const (
buildScriptName = "neoshowcase_internal_build.sh"
buildScriptName = "neoshowcase_internal_build.sh"
temporaryDockerignoreName = "neoshowcase_temporary_dockerignore"
)

func withBuildkitProgress(ctx context.Context, logger io.Writer, buildFn func(ctx context.Context, ch chan *buildkit.SolveStatus) error) error {
Expand Down Expand Up @@ -86,6 +87,11 @@ func createScriptFile(filename string, script string) error {
return createFile(filename, "#!/bin/sh\nset -eux\n"+script)
}

func dockerignoreExists(dir string) bool {
info, err := os.Stat(filepath.Join(dir, ".dockerignore"))
return err == nil && !info.IsDir()
}

func (s *builderService) authSessions() []session.Attachable {
if s.imageConfig.Registry.Username == "" && s.imageConfig.Registry.Password == "" {
return nil
Expand Down Expand Up @@ -139,6 +145,17 @@ func (s *builderService) buildRuntimeCmd(
ch chan *buildkit.SolveStatus,
bc *domain.BuildConfigRuntimeCmd,
) error {
// If .dockerignore exists, rename to prevent it from being picked up by buildkitd,
// as this is not the behavior we want in 'Command' build which is supposed to execute commands against raw repository files.
// See https://github.com/traPtitech/NeoShowcase/issues/877 for more details.
dockerignoreExists := dockerignoreExists(st.repositoryTempDir)
if dockerignoreExists {
err := os.Rename(filepath.Join(st.repositoryTempDir, ".dockerignore"), filepath.Join(st.repositoryTempDir, temporaryDockerignoreName))
if err != nil {
return errors.Wrap(err, "renaming .dockerignore")
}
}

var dockerfile strings.Builder
if bc.BaseImage == "" {
dockerfile.WriteString("FROM scratch\n")
Expand All @@ -153,6 +170,9 @@ func (s *builderService) buildRuntimeCmd(

dockerfile.WriteString("WORKDIR /srv\n")
dockerfile.WriteString("COPY . .\n")
if dockerignoreExists {
dockerfile.WriteString(fmt.Sprintf("RUN mv %s .dockerignore\n", temporaryDockerignoreName))
}

if bc.BuildCmd != "" {
err := createScriptFile(filepath.Join(st.repositoryTempDir, buildScriptName), bc.BuildCmd)
Expand Down Expand Up @@ -204,6 +224,17 @@ func (s *builderService) buildStaticCmd(
ch chan *buildkit.SolveStatus,
bc *domain.BuildConfigStaticCmd,
) error {
// If .dockerignore exists, rename to prevent it from being picked up by buildkitd,
// as this is not the behavior we want in 'Command' build which is supposed to execute commands against raw repository files.
// See https://github.com/traPtitech/NeoShowcase/issues/877 for more details.
dockerignoreExists := dockerignoreExists(st.repositoryTempDir)
if dockerignoreExists {
err := os.Rename(filepath.Join(st.repositoryTempDir, ".dockerignore"), filepath.Join(st.repositoryTempDir, temporaryDockerignoreName))
if err != nil {
return errors.Wrap(err, "renaming .dockerignore")
}
}

var dockerfile strings.Builder

dockerfile.WriteString(fmt.Sprintf(
Expand All @@ -218,6 +249,9 @@ func (s *builderService) buildStaticCmd(

dockerfile.WriteString("WORKDIR /srv\n")
dockerfile.WriteString("COPY . .\n")
if dockerignoreExists {
dockerfile.WriteString(fmt.Sprintf("RUN mv %s .dockerignore\n", temporaryDockerignoreName))
}

if bc.BuildCmd != "" {
err := createScriptFile(filepath.Join(st.repositoryTempDir, buildScriptName), bc.BuildCmd)
Expand Down

0 comments on commit ebc3d0d

Please sign in to comment.