Skip to content

Commit 06d1bb0

Browse files
committed
build: package runner provider
1 parent 67514f4 commit 06d1bb0

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

.goreleaser.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,25 @@ builds:
2323
- arm64
2424
ldflags:
2525
- -s -w -X github.com/GoCodeAlone/workflow-plugin-github/internal.Version={{.Version}}
26+
- id: github-runner-provider
27+
main: ./cmd/github-runner-provider
28+
binary: github-runner-provider
29+
env:
30+
- CGO_ENABLED=0
31+
- GOPRIVATE=github.com/GoCodeAlone/*
32+
goos:
33+
- linux
34+
- darwin
35+
- windows
36+
goarch:
37+
- amd64
38+
- arm64
2639

2740
archives:
2841
- id: workflow-plugin-github
29-
builds:
42+
ids:
3043
- workflow-plugin-github
44+
- github-runner-provider
3145
name_template: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
3246
files:
3347
- plugin.json

release_packaging_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package githubplugin_test
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestReleaseArchiveIncludesGitHubRunnerProvider(t *testing.T) {
10+
data, err := os.ReadFile(".goreleaser.yaml")
11+
if err != nil {
12+
t.Fatalf("read .goreleaser.yaml: %v", err)
13+
}
14+
text := string(data)
15+
16+
if !goreleaserBuildIncludes(text, "github-runner-provider", []string{
17+
"main: ./cmd/github-runner-provider",
18+
"binary: github-runner-provider",
19+
}) {
20+
t.Fatal("release config must build the versioned github-runner-provider binary")
21+
}
22+
23+
if !goreleaserArchiveIncludesBuild(text, "workflow-plugin-github", "github-runner-provider") {
24+
t.Fatal("release archive must include github-runner-provider so wfctl plugin install/fetch can provide it")
25+
}
26+
27+
if _, err := os.Stat("cmd/github-runner-provider/main.go"); err != nil {
28+
t.Fatalf("github-runner-provider command must exist: %v", err)
29+
}
30+
}
31+
32+
func TestReleaseArchiveCheckRejectsProviderBuildOutsideArchive(t *testing.T) {
33+
config := `
34+
builds:
35+
- id: workflow-plugin-github
36+
main: ./cmd/workflow-plugin-github
37+
- id: github-runner-provider
38+
main: ./cmd/github-runner-provider
39+
binary: github-runner-provider
40+
41+
archives:
42+
- id: workflow-plugin-github
43+
ids:
44+
- workflow-plugin-github
45+
`
46+
47+
if goreleaserArchiveIncludesBuild(config, "workflow-plugin-github", "github-runner-provider") {
48+
t.Fatal("archive check must reject configs that build github-runner-provider without packaging it")
49+
}
50+
}
51+
52+
func goreleaserBuildIncludes(config, id string, required []string) bool {
53+
builds := topLevelSection(config, "builds:")
54+
build := listItemWithID(builds, id)
55+
if build == "" {
56+
return false
57+
}
58+
for _, want := range required {
59+
if !strings.Contains(build, want) {
60+
return false
61+
}
62+
}
63+
return true
64+
}
65+
66+
func goreleaserArchiveIncludesBuild(config, archiveID, buildID string) bool {
67+
archives := topLevelSection(config, "archives:")
68+
archive := listItemWithID(archives, archiveID)
69+
return strings.Contains(archive, "ids:") && strings.Contains(archive, "- "+buildID)
70+
}
71+
72+
func topLevelSection(config, header string) string {
73+
lines := strings.Split(config, "\n")
74+
for i, line := range lines {
75+
if line != header {
76+
continue
77+
}
78+
79+
var section []string
80+
for _, next := range lines[i+1:] {
81+
if next != "" && !strings.HasPrefix(next, " ") && !strings.HasPrefix(next, "\t") {
82+
break
83+
}
84+
section = append(section, next)
85+
}
86+
return strings.Join(section, "\n")
87+
}
88+
return ""
89+
}
90+
91+
func listItemWithID(section, id string) string {
92+
lines := strings.Split(section, "\n")
93+
marker := "- id: " + id
94+
for i, line := range lines {
95+
if strings.TrimSpace(line) != marker {
96+
continue
97+
}
98+
99+
item := []string{line}
100+
for _, next := range lines[i+1:] {
101+
if strings.HasPrefix(next, " - id: ") {
102+
break
103+
}
104+
item = append(item, next)
105+
}
106+
return strings.Join(item, "\n")
107+
}
108+
return ""
109+
}

0 commit comments

Comments
 (0)