Skip to content

Commit 916d8cf

Browse files
committed
zip and upload artefact
1 parent dafbca0 commit 916d8cf

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

dgctl/cmd/exec.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11+
"github.com/diggerhq/digger/dgctl/utils"
1112
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
1213
"github.com/diggerhq/digger/libs/spec"
1314
"github.com/google/go-github/v61/github"
@@ -273,6 +274,23 @@ var execCmd = &cobra.Command{
273274
var spec spec.Spec
274275
err = json.Unmarshal(specBytes, &spec)
275276

277+
// attatch zip archive to backend
278+
backendToken := spec.Job.BackendJobToken
279+
zipLocation, err := utils.ArchiveGitRepo("./")
280+
statusCode, respBody, err := utils.SendZipAsJobArtefact(diggerHostname, zipLocation, backendToken)
281+
if err != nil {
282+
log.Printf("could not attach zip artefact: %v", err)
283+
os.Exit(1)
284+
}
285+
if *statusCode != 200 {
286+
log.Printf("unexpected status code from backend: %v", *statusCode)
287+
log.Printf("server response: %v", *respBody)
288+
os.Exit(1)
289+
}
290+
291+
log.Printf("exiting early ...")
292+
os.Exit(0)
293+
276294
token := os.Getenv("GITHUB_PAT_TOKEN")
277295
if token == "" {
278296
log.Printf("missing variable: GITHUB_PAT_TOKEN")

dgctl/utils/backendutils.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"mime/multipart"
8+
"net/http"
9+
"os"
10+
"path/filepath"
11+
)
12+
13+
func SendZipAsJobArtefact(backendUrl string, zipLocation string, jobToken string) (*int, *string, error) {
14+
url := fmt.Sprintf("%v/job_artefacts/", backendUrl)
15+
filePath := zipLocation
16+
17+
// Open the file
18+
file, err := os.Open(filePath)
19+
if err != nil {
20+
fmt.Println("Error opening file:", err)
21+
return nil, nil, fmt.Errorf("Error opening file:", err)
22+
}
23+
defer file.Close()
24+
25+
// Create a buffer to store our request body as bytes
26+
var requestBody bytes.Buffer
27+
28+
// Create a multipart writer
29+
multipartWriter := multipart.NewWriter(&requestBody)
30+
31+
// Create a form file writer for our file field
32+
fileWriter, err := multipartWriter.CreateFormFile("file", filepath.Base(filePath))
33+
if err != nil {
34+
fmt.Println("Error creating form file:", err)
35+
return nil, nil, fmt.Errorf("Error creating form file:", err)
36+
}
37+
38+
// Copy the file content to the form file writer
39+
_, err = io.Copy(fileWriter, file)
40+
if err != nil {
41+
fmt.Println("Error copying file content:", err)
42+
return nil, nil, fmt.Errorf("Error copying file content:", err)
43+
}
44+
45+
// Close the multipart writer to finalize the request body
46+
multipartWriter.Close()
47+
48+
// Create a new HTTP request
49+
req, err := http.NewRequest("PUT", url, &requestBody)
50+
if err != nil {
51+
fmt.Println("Error creating request:", err)
52+
return nil, nil, fmt.Errorf("Error creating request:", err)
53+
}
54+
55+
// Set the content type header
56+
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
57+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", jobToken))
58+
59+
// Send the request
60+
client := &http.Client{}
61+
resp, err := client.Do(req)
62+
if err != nil {
63+
fmt.Println("Error sending request:", err)
64+
return nil, nil, fmt.Errorf("Error sending request:", err)
65+
}
66+
defer resp.Body.Close()
67+
68+
// Read and print the response
69+
body, err := io.ReadAll(resp.Body)
70+
if err != nil {
71+
fmt.Println("Error reading response:", err)
72+
return nil, nil, fmt.Errorf("Error reading response: %v", err)
73+
}
74+
75+
b := string(body)
76+
return &resp.StatusCode, &b, nil
77+
}

dgctl/utils/gitio.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"time"
10+
)
11+
12+
func ArchiveGitRepo(sourcePath string) (string, error) {
13+
// Generate a unique ID for the temp directory
14+
tempID := fmt.Sprintf("%d", time.Now().UnixNano())
15+
tempDir := filepath.Join(os.TempDir(), fmt.Sprintf("temp_%s", tempID))
16+
17+
// Create the temp directory
18+
if err := os.MkdirAll(tempDir, 0755); err != nil {
19+
return "", fmt.Errorf("failed to create temp directory: %w", err)
20+
}
21+
22+
// Copy the source directory to the temp location
23+
if err := copyDir(sourcePath, tempDir); err != nil {
24+
return "", fmt.Errorf("failed to copy directory: %w", err)
25+
}
26+
27+
// Initialize a new git repo in the copied location
28+
cmd := exec.Command("git", "init")
29+
cmd.Dir = tempDir
30+
if err := cmd.Run(); err != nil {
31+
return "", fmt.Errorf("failed to initialize git repo: %w", err)
32+
}
33+
34+
// Add all files to the git repo
35+
cmd = exec.Command("git", "add", ".")
36+
cmd.Dir = tempDir
37+
if err := cmd.Run(); err != nil {
38+
return "", fmt.Errorf("failed to add files to git repo: %w", err)
39+
}
40+
41+
// Commit the files
42+
cmd = exec.Command("git", "commit", "-m", "Initial commit")
43+
cmd.Dir = tempDir
44+
if err := cmd.Run(); err != nil {
45+
return "", fmt.Errorf("failed to commit files: %w", err)
46+
}
47+
48+
// Create a zip file using git archive
49+
zipFile := filepath.Join(os.TempDir(), fmt.Sprintf("archive_%s.zip", tempID))
50+
cmd = exec.Command("git", "archive", "--format=zip", "-o", zipFile, "HEAD")
51+
cmd.Dir = tempDir
52+
if err := cmd.Run(); err != nil {
53+
return "", fmt.Errorf("failed to create zip archive: %w", err)
54+
}
55+
56+
return zipFile, nil
57+
}
58+
59+
func copyDir(src, dst string) error {
60+
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
61+
if err != nil {
62+
return err
63+
}
64+
65+
// Skip .git directory
66+
if info.IsDir() && info.Name() == ".git" {
67+
return filepath.SkipDir
68+
}
69+
70+
relPath, err := filepath.Rel(src, path)
71+
if err != nil {
72+
return err
73+
}
74+
dstPath := filepath.Join(dst, relPath)
75+
76+
if info.IsDir() {
77+
return os.MkdirAll(dstPath, info.Mode())
78+
}
79+
80+
return copyFile(path, dstPath)
81+
})
82+
}
83+
84+
func copyFile(src, dst string) error {
85+
srcFile, err := os.Open(src)
86+
if err != nil {
87+
return err
88+
}
89+
defer srcFile.Close()
90+
91+
dstFile, err := os.Create(dst)
92+
if err != nil {
93+
return err
94+
}
95+
defer dstFile.Close()
96+
97+
_, err = io.Copy(dstFile, srcFile)
98+
return err
99+
}

0 commit comments

Comments
 (0)