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

Commit 9632aa9

Browse files
Autogenerate pull requests for base image hash updates (#57557)
* Add pipeline step to run sg wolfi update-hashes * Remove unused variable * Testing sg version * Update sg command * Test gh cli client * Ad-hoc install gh-cli This will later be added to the base agents, so this is just a temporary step * Commit changes to oci_deps and try using gh to fetch PRs * Remove sg debug commands * Fox typoo * Delete branch if it already exists This might cause problems with stateful runners - need to confirm * Add debugging * Tweak github PR search * Catch potential error in git br -D * Enable push and PR creation * Tweak PR metadata * Add test plan to PR * Use a multi-line string * Remove debug comments * Comment out unused variable * Replace `cat` with `git diff` to show changes * Quiet grep Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com> * Add git emoji in output Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com> * Add github icon to output Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com> --------- Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>
1 parent 3718562 commit 9632aa9

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

dev/ci/internal/ci/pipeline.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ func GeneratePipeline(c Config) (*bk.Pipeline, error) {
196196
)
197197

198198
case runtype.WolfiBaseRebuild:
199-
// If this is a Wolfi base image rebuild, rebuild all Wolfi base images and push to registry
199+
// If this is a Wolfi base image rebuild, rebuild all Wolfi base images
200+
// and push to registry, then open a PR
200201
baseImageOps := wolfiRebuildAllBaseImages(c)
201202
if baseImageOps != nil {
202203
ops.Merge(baseImageOps)
204+
ops.Merge(wolfiGenerateBaseImagePR())
203205
}
204206

205207
case runtype.CandidatesNoTest:

dev/ci/internal/ci/wolfi_operations.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,21 @@ func wolfiRebuildAllBaseImages(c Config) *operations.Set {
325325

326326
return baseImageOps
327327
}
328+
329+
// wolfiGenerateBaseImagePR updates base image hashes and creates a PR in GitHub
330+
func wolfiGenerateBaseImagePR() *operations.Set {
331+
ops := operations.NewNamedSet("Base Image Update PR")
332+
333+
ops.Append(
334+
func(pipeline *bk.Pipeline) {
335+
pipeline.AddStep(":whale::hash: Update Base Image Hashes",
336+
bk.Cmd("./dev/ci/scripts/wolfi/update-base-image-hashes.sh"),
337+
bk.Agent("queue", "bazel"),
338+
bk.DependsOn("buildAllBaseImages"),
339+
bk.Key("updateBaseImageHashes"),
340+
)
341+
},
342+
)
343+
344+
return ops
345+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu -o pipefail
4+
5+
cd "$(dirname "${BASH_SOURCE[0]}")/../../../.."
6+
7+
# Update hashes for all base images
8+
go run ./dev/sg wolfi update-hashes
9+
# Print diff
10+
git diff dev/oci_deps.bzl
11+
12+
# Temporary: Install GitHub CLI
13+
ghtmpdir=$(mktemp -d -t github-cli.XXXXXXXX)
14+
curl -L https://github.com/cli/cli/releases/download/v2.36.0/gh_2.36.0_linux_amd64.tar.gz -o "${ghtmpdir}/gh.tar.gz"
15+
# From https://github.com/cli/cli/releases/download/v2.36.0/gh_2.36.0_checksums.txt
16+
expected_hash="29ed6c04931e6ac8a5f5f383411d7828902fed22f08b0daf9c8ddb97a89d97ce"
17+
actual_hash=$(sha256sum "${ghtmpdir}/gh.tar.gz" | cut -d ' ' -f 1)
18+
if [ "$expected_hash" = "$actual_hash" ]; then
19+
echo "Hashes match"
20+
else
21+
echo "Error - hashes do not match!"
22+
exit 1
23+
fi
24+
tar -xzf "${ghtmpdir}/gh.tar.gz" -C "${ghtmpdir}/"
25+
cp "${ghtmpdir}/gh_2.36.0_linux_amd64/bin/gh" "/usr/local/bin/"
26+
# Test gh
27+
gh --version
28+
29+
BRANCH_NAME="wolfi-autoupdate/main"
30+
TIMESTAMP=$(TZ=UTC date "+%Y-%m-%d %H:%M:%S %z")
31+
PR_TITLE="Update Wolfi base images to latest"
32+
# PR_REVIEWER="sourcegraph/security"
33+
PR_LABELS="SSDLC,wolfi-auto-update"
34+
PR_BODY="Automatically generated PR to update Wolfi base images to the latest hashes.
35+
## Test Plan
36+
- CI build verifies image functionality"
37+
38+
# Commit changes to dev/oci-deps.bzl
39+
# Delete branch if it exists; catch status code if not
40+
git branch -D "${BRANCH_NAME}" || :
41+
git checkout -b "${BRANCH_NAME}"
42+
git add dev/oci_deps.bzl
43+
git commit -m "Automatically update Wolfi base image hashes at ${TIMESTAMP}"
44+
# git remote set-url token-origin https://sg-test:${GH_TOKEN}@github.com/sourcegraph/sourcegraph.git
45+
git push --force -u origin "${BRANCH_NAME}"
46+
echo ":git: Successfully commited changes and pushed to branch ${BRANCH_NAME}"
47+
48+
# Check if an update PR already exists
49+
if gh pr list --head "${BRANCH_NAME}" --state open | grep -q "${PR_TITLE}"; then
50+
echo ":github: A pull request already exists - no action required"
51+
else
52+
# If not, create a new PR from the branch foobar-day
53+
# TODO: Once validated add '--reviewer "${PR_REVIEWER}"'
54+
gh pr create --title "${PR_TITLE}" --head "${BRANCH_NAME}" --base main --body "${PR_BODY}" --label "${PR_LABELS}"
55+
echo ":github: Created a new pull request from branch '${BRANCH_NAME}' with title '${PR_TITLE}'"
56+
fi

0 commit comments

Comments
 (0)