-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_build_php_packages.go
More file actions
203 lines (169 loc) · 6.69 KB
/
function_build_php_packages.go
File metadata and controls
203 lines (169 loc) · 6.69 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"fmt"
"github.com/dchest/uniuri"
"github.com/spaulg/php-dev-containers/internal/dagger"
"runtime"
"strings"
)
type PhpVersionAsset struct {
Filename string `json:"filename"`
Name string `json:"name"`
}
type PhpVersion struct {
Source []PhpVersionAsset `json:"source"`
Museum bool `json:"museum"`
}
func (m *PhpDevContainers) BuildPhpPackages(
ctx context.Context,
// Source archive file path
sourceArchive *dagger.File,
// List of architectures to build packages for, in addition to the native architecture
//+optional
architectures *string,
) (*dagger.Directory, error) {
var err error
var container *dagger.Container
var buildArchitectures []string
// Process architecture list, ensuring the current runtime arch is first
buildArchitectures = append(buildArchitectures, runtime.GOARCH)
if architectures != nil {
architectureList := strings.Split(*architectures, ",")
for _, architecture := range architectureList {
if architecture != runtime.GOARCH {
buildArchitectures = append(buildArchitectures, strings.TrimSpace(architecture))
}
}
}
// Download source archive
sourceArchiveFileName := fmt.Sprintf("%s_%s.orig.tar.gz", m.PackageName, m.Version)
// Start container
container, err = dag.Container().
From(m.BaseImage).
Sync(ctx)
if err != nil {
return nil, err
}
// Bust cache if required
if m.NoCache {
container, err = container.
WithEnvVariable("BURST_CACHE", uniuri.New()).
Sync(ctx)
if err != nil {
return nil, err
}
}
// Prepare environment
container, err = container.
WithEnvVariable("DEBIAN_FRONTEND", "noninteractive").
WithExec([]string{"apt-get", "clean"}).
WithExec([]string{"rm", "-rf", "/var/lib/apt/lists/*"}).
WithExec([]string{"apt", "update", "-y"}).
WithExec([]string{"apt", "upgrade", "-y"}).
WithExec([]string{"apt", "install", "-y", "build-essential", "devscripts", "quilt", "git", "sudo"}).
WithExec([]string{"sh", "-c", "echo \"Cmnd_Alias DPKG_ADD_ARCH=/usr/bin/dpkg --add-architecture *\" >> /etc/sudoers.d/build"}).
WithExec([]string{"sh", "-c", "echo \"Cmnd_Alias APT_INSTALL=/usr/bin/apt install -y *\" >> /etc/sudoers.d/build"}).
WithExec([]string{"sh", "-c", "echo \"Cmnd_Alias APT_UPDATE=/usr/bin/apt update -y\" >> /etc/sudoers.d/build"}).
WithExec([]string{"sh", "-c", "echo \"Cmnd_Alias APT_AUTOREMOVE=/usr/bin/apt autoremove * \" >> /etc/sudoers.d/build"}).
WithExec([]string{"sh", "-c", "echo \"Cmnd_Alias MK_BUILD_DEPS=/usr/bin/mk-build-deps * \" >> /etc/sudoers.d/build"}).
WithExec([]string{"sh", "-c", "echo \"build ALL=(ALL) NOPASSWD:MK_BUILD_DEPS, DPKG_ADD_ARCH, APT_INSTALL, APT_UPDATE, APT_AUTOREMOVE\" >> /etc/sudoers.d/build"}).
WithExec([]string{"useradd", "-s", "/bin/bash", "-d", "/home/build", "-m", "-U", "build"}).
WithWorkdir("/home/build").
WithUser("build").
WithExec([]string{"mkdir", "-p", "/home/build/packages"}).
WithDirectory("/home/build/source", dag.CurrentModule().Source().Directory("assets/source/")).
WithFile("/home/build/source/"+sourceArchiveFileName, sourceArchive).
Sync(ctx)
if err != nil {
return nil, err
}
for architectureIndex, architecture := range buildArchitectures {
if architectureIndex > 0 {
// Install native packages for building phar in next architecture
directory := container.Directory("/home/build/packages")
files, err := directory.Glob(ctx, "**.deb")
if err != nil {
return nil, err
}
aptInstallCommand := []string{"sudo", "apt", "install", "-y", "--no-install-recommends", "--no-install-suggests"}
for _, file := range files {
if strings.HasSuffix(file, "_"+buildArchitectures[0]+".deb") || strings.HasSuffix(file, "_all.deb") {
if strings.HasPrefix(file, "php"+m.ShortVersion+"-") {
aptInstallCommand = append(aptInstallCommand, "/home/build/packages/"+file)
}
}
}
container, err = container.
WithExec(aptInstallCommand).
Sync(ctx)
if err != nil {
return nil, err
}
}
container, err = container.
WithWorkdir("/home/build").
WithExec([]string{"rm", "-rf", m.BuildDirectoryPath}).
WithExec([]string{"mkdir", "-p", m.BuildDirectoryPath}).
WithWorkdir(m.BuildDirectoryPath).
WithExec([]string{"cp", "/home/build/source/" + sourceArchiveFileName, m.BuildDirectoryRootPath + "/" + sourceArchiveFileName}).
WithExec([]string{"tar", "-xzf", m.BuildDirectoryRootPath + "/" + sourceArchiveFileName, "--strip-components=1", "--exclude", "debian"}).
WithExec([]string{"cp", "-R", "/home/build/source/" + m.ShortVersion, m.BuildDirectoryPath + "/debian"}).
WithExec([]string{"rm", "-f", "debian/changelog"}).
WithExec([]string{"debchange", "--create", "--package", m.PackageName, "--Distribution", "stable", "-v", m.Version + "-1php+dev+containers", m.Version + "-1php+dev+containers automated build"}).
WithExec([]string{"make", "-f", "debian/rules", "prepare"}).
WithExec([]string{"sudo", "dpkg", "--add-architecture", architecture}).
WithExec([]string{"sudo", "apt", "update", "-y"}).
WithExec([]string{"sudo", "mk-build-deps", "-i", "-t", "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y", "--host-arch", architecture}).
Sync(ctx)
if err != nil {
return nil, err
}
// Clean mk-build-deps files and delete
buildDirectory := container.Directory(m.BuildDirectoryPath)
var removeFiles []string
for _, globPattern := range []string{"**.deb", "**.changes", "**.buildinfo"} {
globFiles, err := buildDirectory.Glob(ctx, globPattern)
if err != nil {
return nil, fmt.Errorf("unable to list glob files for cleanup: %v", err)
}
for _, file := range globFiles {
file = m.BuildDirectoryPath + "/" + file
removeFiles = append(removeFiles, file)
}
}
if len(removeFiles) > 0 {
container, err = container.
WithExec(append([]string{"rm", "-f"}, removeFiles...)).
Sync(ctx)
if err != nil {
return nil, err
}
}
// Final build
buildDependenciesPackage := ""
if architectureIndex == 0 {
buildDependenciesPackage = m.PackageName + "-build-deps"
} else {
buildDependenciesPackage = m.PackageName + "-cross-build-deps"
}
container, err = container.
WithExec([]string{"debuild", "-us", "-uc", "-a" + architecture}).
WithExec([]string{"sudo", "apt", "autoremove", "-y", buildDependenciesPackage}).
Sync(ctx)
if err != nil {
return nil, fmt.Errorf("failed to remove package build dependencies: %w", err)
}
}
directory := container.Directory("/home/build/packages/")
entries, err := directory.Entries(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list files from build: %w", err)
}
for _, file := range entries {
if strings.HasSuffix(file, ".deb") == false {
directory = directory.WithoutFile(file)
}
}
return directory, nil
}