From 95dddb554920be465c2760e730a14d24ea3b57f3 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Fri, 21 Mar 2025 10:34:01 -0400 Subject: [PATCH] cmd/containerbuild: default to ttl.sh for third party contributions (#51) * cmd/containerbuild: default to ttl.sh for third party contributions Closes #48 Signed-off-by: Xe Iaso * track comment tags Signed-off-by: Xe Iaso * empty commit to make sure double-commenting doesn't work Signed-off-by: Xe Iaso --------- Signed-off-by: Xe Iaso --- .github/workflows/docker.yml | 12 ++++++++++++ cmd/containerbuild/main.go | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 5aa85db..9f0f9a0 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -16,6 +16,7 @@ permissions: packages: write attestations: write id-token: write + pull-requests: write jobs: build: @@ -56,6 +57,17 @@ jobs: id: build run: | go run ./cmd/containerbuild --docker-repo ghcr.io/techarohq/anubis --slog-level debug + + - name: "Comment about where to test this" + uses: thollander/actions-comment-pull-request@v3 + with: + message: | + You can try this PR out by using the following docker image: + + ``` + ${{ steps.build.outputs.docker_image }} + ``` + comment-tag: ${{ steps.build.outputs.docker_image }} - name: Generate artifact attestation uses: actions/attest-build-provenance@v2 diff --git a/cmd/containerbuild/main.go b/cmd/containerbuild/main.go index e99dee5..022cb7b 100644 --- a/cmd/containerbuild/main.go +++ b/cmd/containerbuild/main.go @@ -19,9 +19,25 @@ var ( dockerLabels = flag.String("docker-labels", os.Getenv("DOCKER_METADATA_OUTPUT_LABELS"), "Docker image labels") dockerRepo = flag.String("docker-repo", "registry.int.xeserv.us/techaro/anubis", "Docker image repository for Anubis") dockerTags = flag.String("docker-tags", os.Getenv("DOCKER_METADATA_OUTPUT_TAGS"), "newline separated docker tags including the registry name") + githubActor = flag.String("github-actor", "", "GitHub actor") + githubEventName = flag.String("github-event-name", "", "GitHub event name") + pullRequestID = flag.Int("pull-request-id", -1, "GitHub pull request ID") slogLevel = flag.String("slog-level", "INFO", "logging level (see https://pkg.go.dev/log/slog#hdr-Levels)") + + knownContributors = []string{ + "Xe", + } ) +func inList(needle string, haystack []string) bool { + for _, h := range haystack { + if h == needle { + return true + } + } + return false +} + func main() { flagenv.Parse() flag.Parse() @@ -29,6 +45,27 @@ func main() { internal.InitSlog(*slogLevel) koDockerRepo := strings.TrimRight(*dockerRepo, "/"+filepath.Base(*dockerRepo)) + + if *githubEventName == "pull_request" && !inList(*githubActor, knownContributors) { + if *pullRequestID == -1 { + log.Fatal("Must set --pull-request-id when --github-event-name=pull_request") + } + + *dockerRepo = fmt.Sprintf("ttl.sh/techaro/pr-%d/anubis", *pullRequestID) + *dockerTags = fmt.Sprintf("ttl.sh/techaro/pr-%d/anubis:24h", *pullRequestID) + koDockerRepo = fmt.Sprintf("ttl.sh/techaro/pr-%d", *pullRequestID) + + slog.Info( + "Building image for pull request", + "docker-repo", *dockerRepo, + "docker-tags", *dockerTags, + "github-event-name", *githubEventName, + "pull-request-id", *pullRequestID, + ) + } + + setOutput("docker_image", strings.SplitN(*dockerTags, "\n", 2)[0]) + version, err := run("git describe --tags --always --dirty") if err != nil { log.Fatal(err) @@ -129,6 +166,7 @@ func run(command string) (string, error) { if err != nil { return "", err } + slog.Debug("running command", "command", command) cmd := exec.Command(bin, "-c", command) cmd.Stderr = os.Stderr out, err := cmd.Output()