Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] update generate-goreleaser to generate distribution goreleaser files. #486

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build: go ocb
generate: generate-sources generate-goreleaser

generate-goreleaser: go
@${GO} run cmd/goreleaser/main.go -d "${DISTRIBUTIONS}" > .goreleaser.yaml
@./scripts/generate-goreleaser.sh -d "${DISTRIBUTIONS}" -g ${GO}

generate-sources: go ocb
@./scripts/build.sh -d "${DISTRIBUTIONS}" -s true -b ${OTELCOL_BUILDER} -g ${GO}
Expand All @@ -25,7 +25,7 @@ goreleaser-verify: goreleaser
@${GORELEASER} release --snapshot --clean

ensure-goreleaser-up-to-date: generate-goreleaser
@git diff -s --exit-code .goreleaser.yaml || (echo "Build failed: The goreleaser templates have changed but the .goreleaser.yaml hasn't. Run 'make generate-goreleaser' and update your PR." && exit 0)
@git diff -s --exit-code distributions/*/.goreleaser.yaml || (echo "Build failed: The goreleaser templates have changed but the .goreleaser.yamls haven't. Run 'make generate-goreleaser' and update your PR." && exit 0)

.PHONY: ocb
ocb:
Expand Down
94 changes: 43 additions & 51 deletions cmd/goreleaser/internal/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,36 @@ var (
ArmVersions = []string{"7"}
)

func Generate(imagePrefixes []string, dists []string) config.Project {
func Generate(dist string) config.Project {
return config.Project{
ProjectName: "opentelemetry-collector-releases",
Checksum: config.Checksum{
NameTemplate: "{{ .ProjectName }}_checksums.txt",
NameTemplate: fmt.Sprintf("{{ .ProjectName }}_%v_checksums.txt", dist),
},
Env: []string{"COSIGN_YES=true"},
Builds: Builds(dists),
Archives: Archives(dists),
NFPMs: Packages(dists),
Dockers: DockerImages(imagePrefixes, dists),
DockerManifests: DockerManifests(imagePrefixes, dists),
Builds: Builds(dist),
Archives: Archives(dist),
NFPMs: Packages(dist),
Dockers: DockerImages(dist),
DockerManifests: DockerManifests(dist),
Signs: Sign(),
DockerSigns: DockerSigns(),
SBOMs: SBOM(),
}
}

func Builds(dists []string) (r []config.Build) {
for _, dist := range dists {
r = append(r, Build(dist))
func Builds(dist string) []config.Build {
return []config.Build{
Build(dist),
}
return
}

// Build configures a goreleaser build.
// https://goreleaser.com/customization/build/
func Build(dist string) config.Build {
return config.Build{
ID: dist,
Dir: path.Join("distributions", dist, "_build"),
Dir: "_build",
Binary: dist,
BuildDetails: config.BuildDetails{
Env: []string{"CGO_ENABLED=0"},
Expand All @@ -87,11 +86,10 @@ func Build(dist string) config.Build {
}
}

func Archives(dists []string) (r []config.Archive) {
for _, dist := range dists {
r = append(r, Archive(dist))
func Archives(dist string) (r []config.Archive) {
return []config.Archive{
Archive(dist),
}
return
}

// Archive configures a goreleaser archive (tarball).
Expand All @@ -104,11 +102,10 @@ func Archive(dist string) config.Archive {
}
}

func Packages(dists []string) (r []config.NFPM) {
for _, dist := range dists {
r = append(r, Package(dist))
func Packages(dist string) (r []config.NFPM) {
return []config.NFPM{
Package(dist),
}
return
}

// Package configures goreleaser to build a system package.
Expand All @@ -126,22 +123,22 @@ func Package(dist string) config.NFPM {
NFPMOverridables: config.NFPMOverridables{
PackageName: dist,
Scripts: config.NFPMScripts{
PreInstall: path.Join("distributions", dist, "preinstall.sh"),
PostInstall: path.Join("distributions", dist, "postinstall.sh"),
PreRemove: path.Join("distributions", dist, "preremove.sh"),
PreInstall: "preinstall.sh",
PostInstall: "postinstall.sh",
PreRemove: "preremove.sh",
},
Contents: files.Contents{
{
Source: path.Join("distributions", dist, fmt.Sprintf("%s.service", dist)),
Source: fmt.Sprintf("%s.service", dist),
Destination: path.Join("/lib", "systemd", "system", fmt.Sprintf("%s.service", dist)),
},
{
Source: path.Join("distributions", dist, fmt.Sprintf("%s.conf", dist)),
Source: fmt.Sprintf("%s.conf", dist),
Destination: path.Join("/etc", dist, fmt.Sprintf("%s.conf", dist)),
Type: "config|noreplace",
},
{
Source: path.Join("configs", fmt.Sprintf("%s.yaml", dist)),
Source: "config.yaml",
Destination: path.Join("/etc", dist, "config.yaml"),
Type: "config",
},
Expand All @@ -150,28 +147,27 @@ func Package(dist string) config.NFPM {
}
}

func DockerImages(imagePrefixes, dists []string) (r []config.Docker) {
for _, dist := range dists {
for _, arch := range Architectures {
switch arch {
case ArmArch:
for _, vers := range ArmVersions {
r = append(r, DockerImage(imagePrefixes, dist, arch, vers))
}
default:
r = append(r, DockerImage(imagePrefixes, dist, arch, ""))
func DockerImages(dist string) []config.Docker {
r := make([]config.Docker, 0)
for _, arch := range Architectures {
switch arch {
case ArmArch:
for _, vers := range ArmVersions {
r = append(r, DockerImage(dist, arch, vers))
}
default:
r = append(r, DockerImage(dist, arch, ""))
}
}
return
return r
}

// DockerImage configures goreleaser to build a container image.
// https://goreleaser.com/customization/docker/
func DockerImage(imagePrefixes []string, dist, arch, armVersion string) config.Docker {
func DockerImage(dist, arch, armVersion string) config.Docker {
dockerArchName := archName(arch, armVersion)
var imageTemplates []string
for _, prefix := range imagePrefixes {
for _, prefix := range ImagePrefixes {
dockerArchTag := strings.ReplaceAll(dockerArchName, "/", "")
imageTemplates = append(
imageTemplates,
Expand All @@ -186,7 +182,7 @@ func DockerImage(imagePrefixes []string, dist, arch, armVersion string) config.D

return config.Docker{
ImageTemplates: imageTemplates,
Dockerfile: path.Join("distributions", dist, "Dockerfile"),
Dockerfile: "Dockerfile",

Use: "buildx",
BuildFlagTemplates: []string{
Expand All @@ -198,24 +194,20 @@ func DockerImage(imagePrefixes []string, dist, arch, armVersion string) config.D
label("version", ".Version"),
label("source", ".GitURL"),
},
Files: []string{path.Join("configs", fmt.Sprintf("%s.yaml", dist))},
Files: []string{"config.yaml"},
Goos: "linux",
Goarch: arch,
Goarm: armVersion,
IDs: []string{
dist,
},
}
}

func DockerManifests(imagePrefixes, dists []string) (r []config.DockerManifest) {
for _, dist := range dists {
for _, prefix := range imagePrefixes {
r = append(r, DockerManifest(prefix, `{{ .Version }}`, dist))
r = append(r, DockerManifest(prefix, "latest", dist))
}
func DockerManifests(dist string) []config.DockerManifest {
r := make([]config.DockerManifest, 0)
for _, prefix := range ImagePrefixes {
r = append(r, DockerManifest(prefix, `{{ .Version }}`, dist))
r = append(r, DockerManifest(prefix, "latest", dist))
}
return
return r
}

// DockerManifest configures goreleaser to build a multi-arch container image manifest.
Expand Down
14 changes: 7 additions & 7 deletions cmd/goreleaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,22 @@ import (
"flag"
"log"
"os"
"strings"

"gopkg.in/yaml.v3"

"github.com/open-telemetry/opentelemetry-collector-releases/cmd/goreleaser/internal"
)

var distsFlag = flag.String("d", "", "Collector distributions(s) to build, comma-separated")
var distFlag = flag.String("d", "", "Collector distributions to build")

func main() {
flag.Parse()

if len(*distsFlag) == 0 {
log.Fatal("no distributions to build")
if len(*distFlag) == 0 {
log.Fatal("no distribution to build")
}
dists := strings.Split(*distsFlag, ",")

project := internal.Generate(internal.ImagePrefixes, dists)
project := internal.Generate(*distFlag)

partial := map[string]any{
"partial": map[string]any{
Expand All @@ -48,7 +46,9 @@ func main() {
log.Fatal(err)
}

if err := yaml.NewEncoder(os.Stdout).Encode(&project); err != nil {
e = yaml.NewEncoder(os.Stdout)
e.SetIndent(2)
if err := e.Encode(&project); err != nil {
log.Fatal(err)
}
}
27 changes: 27 additions & 0 deletions scripts/generate-goreleaser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

GO=''

while getopts d:g: flag
do
case "${flag}" in
d) distributions=${OPTARG};;
g) GO=${OPTARG};;
*) exit 1;;
esac
done

[[ -n "$GO" ]] || GO='go'

if [[ -z $distributions ]]; then
echo "List of distributions to generate the goreleaser not provided. Use '-d' to specify the names of the distributions use. Ex.:"
echo "$0 -d otelcol"
exit 1
fi

echo "Distributions to generate: $distributions";

for distribution in $(echo "$distributions" | tr "," "\n")
do
${GO} run cmd/goreleaser/main.go -d "${distribution}" > "./distributions/${distribution}/.goreleaser.yaml"
done