Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit bd6ca40

Browse files
authored
wolfi: Rebuild wolfi base images when env WOLFI_BASE_REBUILD=true (#57541)
* Rebuild wolfi base images when env WOLFI_BASE_REBUILD=true * Return operations from wolfiRebuildAllBaseImages * Move wolfiRebuildAllBaseImages * Also refactor addWolfiOps * Update docs
1 parent e6ea7b2 commit bd6ca40

File tree

7 files changed

+121
-50
lines changed

7 files changed

+121
-50
lines changed

dev/ci/internal/ci/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ func NewConfig(now time.Time) Config {
6565
tag = os.Getenv("BUILDKITE_TAG")
6666
// evaluates what type of pipeline run this is
6767
runType = runtype.Compute(tag, branch, map[string]string{
68-
"BEXT_NIGHTLY": os.Getenv("BEXT_NIGHTLY"),
69-
"RELEASE_NIGHTLY": os.Getenv("RELEASE_NIGHTLY"),
70-
"VSCE_NIGHTLY": os.Getenv("VSCE_NIGHTLY"),
68+
"BEXT_NIGHTLY": os.Getenv("BEXT_NIGHTLY"),
69+
"RELEASE_NIGHTLY": os.Getenv("RELEASE_NIGHTLY"),
70+
"VSCE_NIGHTLY": os.Getenv("VSCE_NIGHTLY"),
71+
"WOLFI_BASE_REBUILD": os.Getenv("WOLFI_BASE_REBUILD"),
7172
})
7273
// defaults to 0
7374
buildNumber, _ = strconv.Atoi(os.Getenv("BUILDKITE_BUILD_NUMBER"))

dev/ci/internal/ci/operations.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ func addSgLints(targets []string) func(pipeline *bk.Pipeline) {
8888
tag = os.Getenv("BUILDKITE_TAG")
8989
// evaluates what type of pipeline run this is
9090
runType = runtype.Compute(tag, branch, map[string]string{
91-
"BEXT_NIGHTLY": os.Getenv("BEXT_NIGHTLY"),
92-
"RELEASE_NIGHTLY": os.Getenv("RELEASE_NIGHTLY"),
93-
"VSCE_NIGHTLY": os.Getenv("VSCE_NIGHTLY"),
91+
"BEXT_NIGHTLY": os.Getenv("BEXT_NIGHTLY"),
92+
"RELEASE_NIGHTLY": os.Getenv("RELEASE_NIGHTLY"),
93+
"VSCE_NIGHTLY": os.Getenv("VSCE_NIGHTLY"),
94+
"WOLFI_BASE_REBUILD": os.Getenv("WOLFI_BASE_REBUILD"),
9495
})
9596
)
9697

dev/ci/internal/ci/pipeline.go

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ func GeneratePipeline(c Config) (*bk.Pipeline, error) {
143143
ops.Merge(securityOps)
144144

145145
// Wolfi package and base images
146-
addWolfiOps(c, ops)
146+
packageOps, baseImageOps := addWolfiOps(c)
147+
if packageOps != nil {
148+
ops.Merge(packageOps)
149+
}
150+
if baseImageOps != nil {
151+
ops.Merge(baseImageOps)
152+
}
147153

148154
// Now we set up conditional operations that only apply to pull requests.
149155
if c.Diff.Has(changed.Client) {
@@ -189,6 +195,13 @@ func GeneratePipeline(c Config) (*bk.Pipeline, error) {
189195
addVsceTests,
190196
)
191197

198+
case runtype.WolfiBaseRebuild:
199+
// If this is a Wolfi base image rebuild, rebuild all Wolfi base images and push to registry
200+
baseImageOps := wolfiRebuildAllBaseImages(c)
201+
if baseImageOps != nil {
202+
ops.Merge(baseImageOps)
203+
}
204+
192205
case runtype.CandidatesNoTest:
193206
imageBuildOps := operations.NewNamedSet("Image builds")
194207
imageBuildOps.Append(bazelBuildCandidateDockerImages(legacyDockerImages, c.Version, c.candidateImageTag(), c.RunType))
@@ -302,7 +315,13 @@ func GeneratePipeline(c Config) (*bk.Pipeline, error) {
302315
))
303316

304317
// Wolfi package and base images
305-
addWolfiOps(c, ops)
318+
packageOps, baseImageOps := addWolfiOps(c)
319+
if packageOps != nil {
320+
ops.Merge(packageOps)
321+
}
322+
if baseImageOps != nil {
323+
ops.Merge(baseImageOps)
324+
}
306325

307326
// All operations before this point are required
308327
ops.Append(wait)
@@ -403,33 +422,3 @@ func withAgentLostRetries(s *bk.Step) {
403422
ExitStatus: -1,
404423
})
405424
}
406-
407-
// addWolfiOps adds operations to rebuild modified Wolfi packages and base images.
408-
func addWolfiOps(c Config, ops *operations.Set) {
409-
// Rebuild Wolfi packages that have config changes
410-
var updatedPackages []string
411-
if c.Diff.Has(changed.WolfiPackages) {
412-
var packageOps *operations.Set
413-
packageOps, updatedPackages = WolfiPackagesOperations(c.ChangedFiles[changed.WolfiPackages])
414-
ops.Merge(packageOps)
415-
}
416-
417-
// Rebuild Wolfi base images
418-
// Inspect package dependencies, and rebuild base images with updated packages
419-
_, imagesWithChangedPackages, err := GetDependenciesOfPackages(updatedPackages, "sourcegraph")
420-
if err != nil {
421-
panic(err)
422-
}
423-
// Rebuild base images with package changes AND with config changes
424-
imagesToRebuild := append(imagesWithChangedPackages, c.ChangedFiles[changed.WolfiBaseImages]...)
425-
imagesToRebuild = sortUniq(imagesToRebuild)
426-
427-
if len(imagesToRebuild) > 0 {
428-
baseImageOps, _ := WolfiBaseImagesOperations(
429-
imagesToRebuild,
430-
c.Version,
431-
(len(updatedPackages) > 0),
432-
)
433-
ops.Merge(baseImageOps)
434-
}
435-
}

dev/ci/internal/ci/wolfi_operations.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/yaml.v2"
1212

1313
bk "github.com/sourcegraph/sourcegraph/dev/ci/internal/buildkite"
14+
"github.com/sourcegraph/sourcegraph/dev/ci/internal/ci/changed"
1415
"github.com/sourcegraph/sourcegraph/dev/ci/internal/ci/operations"
1516
"github.com/sourcegraph/sourcegraph/dev/sg/root"
1617
"github.com/sourcegraph/sourcegraph/internal/lazyregexp"
@@ -265,3 +266,62 @@ func getPackagesFromBaseImageConfig(configFile string) ([]string, error) {
265266

266267
return config.Contents.Packages, nil
267268
}
269+
270+
// addWolfiOps adds operations to rebuild modified Wolfi packages and base images.
271+
func addWolfiOps(c Config) (packageOps, baseImageOps *operations.Set) {
272+
// Rebuild Wolfi packages that have config changes
273+
var updatedPackages []string
274+
if c.Diff.Has(changed.WolfiPackages) {
275+
packageOps, updatedPackages = WolfiPackagesOperations(c.ChangedFiles[changed.WolfiPackages])
276+
}
277+
278+
// Rebuild Wolfi base images
279+
// Inspect package dependencies, and rebuild base images with updated packages
280+
_, imagesWithChangedPackages, err := GetDependenciesOfPackages(updatedPackages, "sourcegraph")
281+
if err != nil {
282+
panic(err)
283+
}
284+
// Rebuild base images with package changes AND with config changes
285+
imagesToRebuild := append(imagesWithChangedPackages, c.ChangedFiles[changed.WolfiBaseImages]...)
286+
imagesToRebuild = sortUniq(imagesToRebuild)
287+
288+
if len(imagesToRebuild) > 0 {
289+
baseImageOps, _ = WolfiBaseImagesOperations(
290+
imagesToRebuild,
291+
c.Version,
292+
(len(updatedPackages) > 0),
293+
)
294+
}
295+
296+
return packageOps, baseImageOps
297+
}
298+
299+
// wolfiRebuildAllBaseImages adds operations to rebuild all Wolfi base images and push to registry
300+
func wolfiRebuildAllBaseImages(c Config) *operations.Set {
301+
// List all YAML files in wolfi-images/
302+
dir := "wolfi-images"
303+
files, err := os.ReadDir(dir)
304+
if err != nil {
305+
panic(err)
306+
}
307+
308+
var wolfiBaseImages []string
309+
for _, f := range files {
310+
if filepath.Ext(f.Name()) == ".yaml" {
311+
fullPath := filepath.Join(dir, f.Name())
312+
wolfiBaseImages = append(wolfiBaseImages, fullPath)
313+
}
314+
}
315+
316+
// Rebuild all images
317+
var baseImageOps *operations.Set
318+
if len(wolfiBaseImages) > 0 {
319+
baseImageOps, _ = WolfiBaseImagesOperations(
320+
wolfiBaseImages,
321+
c.Version,
322+
false,
323+
)
324+
}
325+
326+
return baseImageOps
327+
}

dev/ci/runtype/runtype.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ const (
1818

1919
// Nightly builds - must be first because they take precedence
2020

21-
ReleaseNightly // release branch nightly healthcheck builds
22-
BextNightly // browser extension nightly build
23-
VsceNightly // vs code extension nightly build
24-
AppRelease // app release build
25-
AppInsiders // app insiders build
21+
ReleaseNightly // release branch nightly healthcheck builds
22+
BextNightly // browser extension nightly build
23+
VsceNightly // vs code extension nightly build
24+
AppRelease // app release build
25+
AppInsiders // app insiders build
26+
WolfiBaseRebuild // wolfi base image build
2627

2728
// Release branches
2829

@@ -108,6 +109,12 @@ func (t RunType) Matcher() *RunTypeMatcher {
108109
Branch: "vsce/release",
109110
BranchExact: true,
110111
}
112+
case WolfiBaseRebuild:
113+
return &RunTypeMatcher{
114+
EnvIncludes: map[string]string{
115+
"WOLFI_BASE_REBUILD": "true",
116+
},
117+
}
111118

112119
case AppRelease:
113120
return &RunTypeMatcher{
@@ -192,6 +199,8 @@ func (t RunType) String() string {
192199
return "Browser extension nightly release build"
193200
case VsceNightly:
194201
return "VS Code extension nightly release build"
202+
case WolfiBaseRebuild:
203+
return "Wolfi base images rebuild"
195204
case AppRelease:
196205
return "App release build"
197206
case AppInsiders:

dev/ci/runtype/runtype_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func TestComputeRunType(t *testing.T) {
6060
},
6161
},
6262
want: VsceNightly,
63+
}, {
64+
name: "wolfi base image rebuild",
65+
args: args{
66+
branch: "main",
67+
env: map[string]string{
68+
"WOLFI_BASE_REBUILD": "true",
69+
},
70+
},
71+
want: WolfiBaseRebuild,
6372
}, {
6473
name: "vsce release",
6574
args: args{

doc/dev/how-to/wolfi/add_update_images.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ These configuration files can be processed with apko, which will generate a base
1616

1717
Before each release, we should update the base images to ensure we include any updated packages and vulnerability fixes.
1818

19-
This is currently a two-step process, which will be further automated in the future:
20-
21-
- Run [`wolfi-images/rebuild-images.sh`](https://sourcegraph.com/github.com/sourcegraph/sourcegraph@588463afbb0904c125cdcf78c7b182f43328504e/-/blob/wolfi-images/rebuild-images.sh) script, commit the updated YAML files, and merge to main.
22-
- Wait for the `main` branch's Buildkite run to complete.
23-
- Buildkite will rebuild the base images and publish them to Dockerhub.
2419
- Run `sg wolfi update-hashes` locally to update the base image hashes in `dev/oci_deps.bzl`. Commit these changes and merge to `main`.
25-
- This fetches the updated base image hashes from the images that were pushed to Dockerhub in the previous step.
26-
- Backport the PR that updated `dev/oci_deps.bzl` to the release branch.
20+
- Backport the PR to the release branch.
21+
22+
#### Automation
23+
24+
This process is partially automated by Buildkite. A scheduled build runs daily to rebuild Wolfi base images - pulling in any updated dependencies - then push them to Docker Hub. When `sg wolfi update-hashes` is run, it pulls these latest image hashes from Docker Hub to update the references in `dev/oci_deps.bzl`.
25+
26+
To rebuild the images (perhaps to pick up a just-released package version), [find a scheduled build](https://buildkite.com/sourcegraph/sourcegraph/builds?branch=main) in Buildkite named "Nightly Rebuild of Wolfi Base Images", and hit "Rebuild".
27+
28+
It is also possible to manually rebuild individual images by running `wolfi-images/rebuild-images.sh` locally, then pushing and merging.
2729

2830
### Modify an existing base image
2931

0 commit comments

Comments
 (0)