Skip to content

Commit

Permalink
fix(testworkflows): support overriding repository files with `content…
Browse files Browse the repository at this point in the history
….files` (#5555)
  • Loading branch information
rangoo94 authored Jun 10, 2024
1 parent 8f5a629 commit 4196ff9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
23 changes: 22 additions & 1 deletion cmd/tcl/testworkflow-toolkit/commands/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"net/url"
"os"
"path/filepath"
"strings"

"github.com/otiai10/copy"

"github.com/kballard/go-shellquote"
"github.com/spf13/cobra"
Expand All @@ -39,7 +42,7 @@ func NewCloneCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
uri, err := url.Parse(args[0])
ui.ExitOnError("repository uri", err)
outputPath, err := filepath.Abs(args[1])
destinationPath, err := filepath.Abs(args[1])
ui.ExitOnError("output path", err)

// Disable interactivity
Expand Down Expand Up @@ -74,6 +77,9 @@ func NewCloneCmd() *cobra.Command {
os.Setenv("GIT_SSH_COMMAND", shellquote.Join("ssh", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-i", sshKeyPath))
}

// Keep the files in temporary directory
outputPath := filepath.Join(constants.DefaultTmpDirPath, "repo")

// Mark directory as safe
configArgs := []string{"-c", fmt.Sprintf("safe.directory=%s", outputPath), "-c", "advice.detachedHead=false"}

Expand Down Expand Up @@ -105,6 +111,21 @@ func NewCloneCmd() *cobra.Command {
ui.ExitOnError("fetching head", err)
}
}

// Copy files to the expected directory. Ignore errors, only inform warn about them.
err = copy.Copy(outputPath, destinationPath, copy.Options{
OnError: func(src, dest string, err error) error {
if err != nil {
if src == outputPath && strings.Contains(err.Error(), "chmod") {
// Ignore chmod error on mounted directory
return nil
}
fmt.Printf("warn: copying to %s: %s\n", dest, err.Error())
}
return nil
},
})
ui.ExitOnError("copying files to destination", err)
},
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/tcl/testworkflowstcl/testworkflowprocessor/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ func ProcessContentGit(_ InternalProcessor, layer Intermediate, container Contai
mountPath = filepath.Join(constants.DefaultDataPath, "repo")
}

// Build a temporary volume to clone the repository initially.
// This will allow mounting files in the destination at the same level (i.e. overriding the configuration).
container.AppendVolumeMounts(layer.AddEmptyDirVolume(nil, constants.DefaultTmpDirPath))

// Build volume pair and share with all siblings
volumeMount := layer.AddEmptyDirVolume(nil, mountPath)
container.AppendVolumeMounts(volumeMount)
Expand Down Expand Up @@ -275,11 +279,6 @@ func ProcessContentGit(_ InternalProcessor, layer Intermediate, container Contai
args = append(args, "-s", step.Content.Git.SshKey)
}

// Provide temporary directory for SSH key to avoid issues with permissions
if step.Content.Git.SshKeyFrom != nil || step.Content.Git.SshKey != "" {
container.AppendVolumeMounts(layer.AddEmptyDirVolume(nil, constants.DefaultTmpDirPath))
}

// Provide auth type
if step.Content.Git.AuthType != "" {
args = append(args, "-a", string(step.Content.Git.AuthType))
Expand Down

0 comments on commit 4196ff9

Please sign in to comment.