From d6f1073fc783be41753dae427996072f453f49f8 Mon Sep 17 00:00:00 2001 From: Ali AKCA Date: Fri, 15 Dec 2023 17:04:19 +0100 Subject: [PATCH] refactor: improve artifact copy for artifact service (#198) * chore: add warning message for native docker * chore: add warning message for native docker * refactor: improve artifact copy for artifact service --- actions/artifacts/main.go | 29 +++++++++++++++++++++-------- main.go | 4 ++++ runner.go | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/actions/artifacts/main.go b/actions/artifacts/main.go index 4eb7270..cda3ea7 100644 --- a/actions/artifacts/main.go +++ b/actions/artifacts/main.go @@ -2,7 +2,7 @@ package main import ( "context" - "fmt" + "time" ) func New() *ActionsArtifactService { @@ -74,14 +74,27 @@ func (m *ActionsArtifactService) Artifacts( // run id of the workflow run to get artifacts for. If not provided, all artifacts saved in the cache will be returned. runID string, ) *Directory { - var ( - cache = m.Data - opts = ContainerWithMountedCacheOpts{Sharing: Shared} - copySH = fmt.Sprintf("if [ -d /artifacts/%s ]; then cp -r /artifacts/%s /exported_artifacts; else mkdir /exported_artifacts; fi", runID, runID) - ) + copySH := `#!/bin/sh + +echo "Copying artifacts for runID: $1" + +if [ -d /artifacts/$1/ ]; then + echo "Copying artifacts for runID: $1" + cp -av /artifacts/$1/ /exported_artifacts/ +else + echo "Artifacts for runID: $1 not found" + + # list of run ids currently in the cache to help with debugging + echo "List of run ids in the cache:" + ls -la /artifacts/ + + mkdir -p /exported_artifacts +fi +` return dag.Container().From("alpine:latest"). - WithMountedCache("/artifacts", cache, opts). - WithExec([]string{"sh", "-c", copySH}). + WithMountedCache("/artifacts", m.Data, ContainerWithMountedCacheOpts{Sharing: Shared}). + WithNewFile("/usr/local/bin/copy-artifacts.sh", ContainerWithNewFileOpts{Contents: copySH, Permissions: 0700}). + WithExec([]string{"copy-artifacts.sh", runID, time.Now().Format(time.RFC3339Nano)}). // current time just to ensure exec is not cached. Directory("/exported_artifacts") } diff --git a/main.go b/main.go index a9c23c3..947be84 100644 --- a/main.go +++ b/main.go @@ -138,6 +138,10 @@ func (g *Gale) Run( log.Warnf("Both enableDocker and useDind are enabled. Using DinD to run docker commands.") } + if useNativeDocker { + log.Warnf("Using native docker support. Please ensure that DOCKER_HOST is set correctly.", "DOCKER_HOST", dockerHost) + } + if useDind { log.Warnf("Enabling DinD may lead to longer execution times.") } diff --git a/runner.go b/runner.go index e712a03..466177e 100644 --- a/runner.go +++ b/runner.go @@ -58,6 +58,8 @@ func (r *Runner) Container(runID string) (*RunnerContainer, error) { ctr = ctr.WithEnvVariable("DOCKER_HOST", "unix:///var/run/docker.sock") case strings.HasPrefix(r.RunnerOpts.DockerHost, "tcp://"): ctr = ctr.WithEnvVariable("DOCKER_HOST", r.RunnerOpts.DockerHost) + default: + return nil, fmt.Errorf("unsupported docker host: %s", r.RunnerOpts.DockerHost) } }