-
Notifications
You must be signed in to change notification settings - Fork 3
/
remotebuild.go
110 lines (99 loc) · 3.12 KB
/
remotebuild.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package build
import (
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/hashicorp/actions-go-build/internal/unzipper"
"github.com/hashicorp/composite-action-framework-go/pkg/fs"
)
// RemoteBuild is a build where the source code is hosted remotely.
// This can be a "primary" or verification build.
type RemoteBuild struct {
*core
sourceURL string
cacheID string
}
func NewRemoteBuild(c Config, options ...Option) (Build, error) {
if c.Product.IsDirty() {
return nil, fmt.Errorf("cannot verify a dirty build remotely")
}
parts := strings.Split(c.Product.Module, "/")
if len(parts) < 3 {
return nil, fmt.Errorf("module %q not supported, must be in the format %q", c.Product.Module, "github.com/<user>/<repo>...")
}
user, repo, revision := parts[1], parts[2], c.Product.Revision
sourceURL := fmt.Sprintf("https://github.com/%s/%s/archive/%s.zip", user, repo, revision)
core, err := newCore("remote build", c, options...)
if err != nil {
return nil, err
}
if err := core.UpdateBuildRoot(); err != nil {
return nil, err
}
return &RemoteBuild{
core: core,
sourceURL: sourceURL,
}, nil
}
func (rb *RemoteBuild) Steps() []Step {
var sourceDLDir, sourceArchivePath string
pre := []Step{
newStep("change build root to temporary directory", func() error {
if rb.IsVerification() {
return rb.ChangeToVerificationRoot()
}
return rb.ChangeToPrimaryRoot()
}),
newStep("create temporary paths", func() error {
sourceDLDir = rb.Dirs().SourceDownloadDir()
return fs.MkdirEmpty(sourceDLDir)
}),
newStep(fmt.Sprintf("get %s", rb.sourceURL), func() error {
c := rb.Config()
sourceArchiveName := fmt.Sprintf("%s-%s.zip", c.Product.Name, c.Product.Revision)
sourceArchivePath = filepath.Join(sourceDLDir, sourceArchiveName)
destFile, err := os.Create(sourceArchivePath)
if err != nil {
return err
}
var closeErr error
defer func() { closeErr = destFile.Close() }()
response, err := http.Get(rb.sourceURL)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("%s", response.Status)
}
var bodyCloseErr error
defer func() { bodyCloseErr = response.Body.Close() }()
if _, err := io.Copy(destFile, response.Body); err != nil {
return err
}
if closeErr != nil {
return closeErr
}
return bodyCloseErr
}),
newStep("extract source code to temporary directory", func() error {
// Extract the downloaded zip file directly in the same dir as the zip.
// These zips contain a directory that contains all the code, so we'll
// use that directory as the build root.
return unzipper.New(rb.Debug).Unzip(sourceArchivePath, sourceDLDir)
}),
newStep("move source code to build root", func() error {
c := rb.Config()
// This innerDirName is GitHub-specific.
innerDirName := fmt.Sprintf("%s-%s", path.Base(c.Product.Repository), c.Product.Revision)
sourcePath := filepath.Join(sourceDLDir, innerDirName)
return fs.Move(sourcePath, rb.Config().Paths.WorkDir)
}),
}
return append(pre, rb.core.Steps()...)
}