ci / test (push) Canceled after 0s
ci / cross-build (amd64, darwin) (push) Canceled after 0s
ci / cross-build (amd64, linux) (push) Canceled after 0s
ci / cross-build (amd64, windows) (push) Canceled after 0s
ci / cross-build (arm64, darwin) (push) Canceled after 0s
ci / cross-build (arm64, linux) (push) Canceled after 0s
ci / cross-build (arm64, windows) (push) Canceled after 0s
- archive: use Docker's own patternmatcher for .dockerignore/.gitignore/.gyignore + hard secret/VCS/build-output exclusions - crypto: AES-256-CTR encrypt the source archive before upload (key/IV to gateway via HTTPS) - deploy: --max-upload / GY_MAX_UPLOAD (default 500 MB) - Makefile + CI matrix: cross-compile linux/darwin/windows x amd64/arm64 - tests: crypto round-trip + ignore rules
194 lines
4.3 KiB
Go
194 lines
4.3 KiB
Go
// Package source packs a local project directory into a source archive and
|
|
// detects how to build it. The archive is a gzip'd tar of the directory's
|
|
// contents (so it extracts cleanly into the builder's /workspace/app) and is
|
|
// encrypted with AES-256-CTR before upload.
|
|
package source
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/moby/patternmatcher"
|
|
)
|
|
|
|
// exactExclude are basenames that are never shipped to the cloud (VCS dirs,
|
|
// secrets, build output). These are hard exclusions — user ignore files cannot
|
|
// re-include them.
|
|
var exactExclude = []string{
|
|
".git", ".gy", ".hg", ".svn",
|
|
"node_modules", "bower_components", ".next", ".nuxt", ".cache", ".turbo",
|
|
"target", "dist", "build", "out",
|
|
".DS_Store", "Thumbs.db",
|
|
".env", ".env.local",
|
|
"id_rsa", "id_dsa", "id_ed25519",
|
|
}
|
|
|
|
// globExclude are basename globs (secrets, logs, keys) that are never shipped.
|
|
var globExclude = []string{
|
|
".env.*", "*.log", "*.log.*",
|
|
"*.pem", "*.key", "*.pfx", "*.p12", "*.keystore", "*.jks",
|
|
"id_rsa.*", "id_ed25519.*",
|
|
"*.secret", "*.env",
|
|
}
|
|
|
|
// ignore matches paths against the project's .dockerignore / .gitignore /
|
|
// .gyignore (using Docker's own pattern engine) plus the hard exclude list.
|
|
type ignore struct {
|
|
pm *patternmatcher.PatternMatcher
|
|
}
|
|
|
|
func newIgnore(dir string) (*ignore, error) {
|
|
var pats []string
|
|
// Read in increasing precedence: gyignore < gitignore < dockerignore, so a
|
|
// .dockerignore rule wins when they conflict.
|
|
for _, fn := range []string{".gyignore", ".gitignore", ".dockerignore"} {
|
|
data, err := os.ReadFile(filepath.Join(dir, fn))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
pats = append(pats, line)
|
|
}
|
|
}
|
|
pm, err := patternmatcher.New(pats)
|
|
if err != nil {
|
|
pm, _ = patternmatcher.New(nil) // malformed patterns: fall back to hard excludes only
|
|
}
|
|
return &ignore{pm: pm}, nil
|
|
}
|
|
|
|
func (ig *ignore) ignored(rel string, isDir bool) bool {
|
|
if alwaysExcluded(rel) {
|
|
return true
|
|
}
|
|
matched, err := ig.pm.MatchesOrParentMatches(rel)
|
|
return err == nil && matched
|
|
}
|
|
|
|
func alwaysExcluded(rel string) bool {
|
|
base := filepath.Base(rel)
|
|
for _, e := range exactExclude {
|
|
if base == e {
|
|
return true
|
|
}
|
|
}
|
|
for _, g := range globExclude {
|
|
if ok, _ := filepath.Match(g, base); ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ArchiveToTemp packs dir into a temporary *.tar.gz and returns its path and
|
|
// compressed size. The caller must remove the file.
|
|
func ArchiveToTemp(dir string) (path string, size int64, err error) {
|
|
abs, err := filepath.Abs(dir)
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
|
|
ignore, err := newIgnore(abs)
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
|
|
f, err := os.CreateTemp("", "gy-deploy-*.tar.gz")
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
name := f.Name()
|
|
cleanup := func() {
|
|
f.Close()
|
|
os.Remove(name)
|
|
}
|
|
|
|
gz := gzip.NewWriter(f)
|
|
tw := tar.NewWriter(gz)
|
|
|
|
err = filepath.WalkDir(abs, func(p string, d os.DirEntry, walkErr error) error {
|
|
if walkErr != nil {
|
|
return walkErr
|
|
}
|
|
if p == abs {
|
|
return nil
|
|
}
|
|
rel, rerr := filepath.Rel(abs, p)
|
|
if rerr != nil {
|
|
return rerr
|
|
}
|
|
rel = filepath.ToSlash(rel)
|
|
if ignore.ignored(rel, d.IsDir()) {
|
|
if d.IsDir() {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
|
|
info, ierr := d.Info()
|
|
if ierr != nil {
|
|
return ierr
|
|
}
|
|
|
|
header, herr := tar.FileInfoHeader(info, "")
|
|
if herr != nil {
|
|
return herr
|
|
}
|
|
header.Name = rel
|
|
|
|
if d.IsDir() {
|
|
if !strings.HasSuffix(header.Name, "/") {
|
|
header.Name += "/"
|
|
}
|
|
return tw.WriteHeader(header)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return nil // skip symlinks/sockets/etc.
|
|
}
|
|
|
|
if err := tw.WriteHeader(header); err != nil {
|
|
return err
|
|
}
|
|
file, oerr := os.Open(p)
|
|
if oerr != nil {
|
|
return oerr
|
|
}
|
|
_, cerr := io.Copy(tw, file)
|
|
file.Close()
|
|
return cerr
|
|
})
|
|
|
|
if err != nil {
|
|
cleanup()
|
|
return "", 0, fmt.Errorf("archive %s: %w", abs, err)
|
|
}
|
|
if err := tw.Close(); err != nil {
|
|
cleanup()
|
|
return "", 0, err
|
|
}
|
|
if err := gz.Close(); err != nil {
|
|
cleanup()
|
|
return "", 0, err
|
|
}
|
|
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
cleanup()
|
|
return "", 0, err
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
os.Remove(name)
|
|
return "", 0, err
|
|
}
|
|
return name, info.Size(), nil
|
|
}
|