diff --git a/.github/workflows/base-ci-goreleaser.yaml b/.github/workflows/base-ci-goreleaser.yaml index 5790efaf..4268fb69 100644 --- a/.github/workflows/base-ci-goreleaser.yaml +++ b/.github/workflows/base-ci-goreleaser.yaml @@ -12,6 +12,9 @@ on: goarch: required: true type: string + cgo: + required: true + type: string jobs: check-goreleaser: @@ -32,7 +35,7 @@ jobs: GOARCH: arm - GOOS: windows GOARCH: s390x - runs-on: ubuntu-22.04 + runs-on: ${{ ( matrix.GOOS == 'darwin' && contains( fromJSON( inputs.cgo), 'darwin')) && 'macos-14' || 'ubuntu-22.04' }} steps: - name: Checkout @@ -41,6 +44,7 @@ jobs: fetch-depth: 0 - name: Setup QEMU + if: ${{ runner.os != 'macOS' }} # docker/setup-qemu-action action is not available on macos uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0 with: platforms: arm64,ppc64le,linux/arm/v7,s390x @@ -53,6 +57,7 @@ jobs: - name: Setup Docker Buildx uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 + if: ${{ runner.os != 'macOS' }} # docker is not available on macos - name: Setup Go uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 diff --git a/.github/workflows/base-release.yaml b/.github/workflows/base-release.yaml index be351240..74d8fdac 100644 --- a/.github/workflows/base-release.yaml +++ b/.github/workflows/base-release.yaml @@ -12,6 +12,9 @@ on: goarch: required: true type: string + cgo: + required: true + type: string jobs: prepare: @@ -32,7 +35,7 @@ jobs: GOARCH: arm - GOOS: windows GOARCH: s390x - runs-on: ubuntu-22.04 + runs-on: ${{ ( matrix.GOOS == 'darwin' && contains( fromJSON( inputs.cgo), 'darwin')) && 'macos-14' || 'ubuntu-22.04' }} steps: - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 @@ -44,10 +47,12 @@ jobs: - uses: anchore/sbom-action/download-syft@61119d458adab75f756bc0b9e4bde25725f86a7a # v0.17.2 - uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0 + if: ${{ runner.os != 'macOS' }} # docker/setup-qemu-action action is not available on macos with: platforms: arm64,ppc64le,linux/arm/v7,s390x - uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 + if: ${{ runner.os != 'macOS' }} - uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 with: diff --git a/.github/workflows/ci-goreleaser-contrib.yaml b/.github/workflows/ci-goreleaser-contrib.yaml index ad7db0a6..4fc8b7ca 100644 --- a/.github/workflows/ci-goreleaser-contrib.yaml +++ b/.github/workflows/ci-goreleaser-contrib.yaml @@ -30,6 +30,7 @@ jobs: distribution: otelcol-contrib goos: '[ "linux", "windows", "darwin" ]' goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]' + cgo: '[ "darwin" ]' secrets: inherit package-tests: diff --git a/.github/workflows/ci-goreleaser-core.yaml b/.github/workflows/ci-goreleaser-core.yaml index 50410eb7..64b47a5c 100644 --- a/.github/workflows/ci-goreleaser-core.yaml +++ b/.github/workflows/ci-goreleaser-core.yaml @@ -31,6 +31,7 @@ jobs: distribution: otelcol goos: '[ "linux", "windows", "darwin" ]' goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]' + cgo: '[]' secrets: inherit package-tests: diff --git a/.github/workflows/ci-goreleaser-k8s.yaml b/.github/workflows/ci-goreleaser-k8s.yaml index fd4b0950..609c82ad 100644 --- a/.github/workflows/ci-goreleaser-k8s.yaml +++ b/.github/workflows/ci-goreleaser-k8s.yaml @@ -30,4 +30,5 @@ jobs: distribution: otelcol-k8s goos: '[ "linux" ]' goarch: '[ "amd64", "arm64", "ppc64le", "s390x" ]' + cgo: '[]' secrets: inherit diff --git a/.github/workflows/release-contrib.yaml b/.github/workflows/release-contrib.yaml index efb6e03e..389c2ac1 100644 --- a/.github/workflows/release-contrib.yaml +++ b/.github/workflows/release-contrib.yaml @@ -12,5 +12,6 @@ jobs: distribution: otelcol-contrib goos: '[ "linux", "windows", "darwin" ]' goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]' + cgo: '[ "darwin" ]' secrets: inherit permissions: write-all diff --git a/.github/workflows/release-core.yaml b/.github/workflows/release-core.yaml index 9c1b7ce7..623766af 100644 --- a/.github/workflows/release-core.yaml +++ b/.github/workflows/release-core.yaml @@ -12,5 +12,6 @@ jobs: distribution: otelcol goos: '[ "linux", "windows", "darwin" ]' goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]' + cgo: '[]' secrets: inherit permissions: write-all diff --git a/.github/workflows/release-k8s.yaml b/.github/workflows/release-k8s.yaml index 20a595f9..48c56cfa 100644 --- a/.github/workflows/release-k8s.yaml +++ b/.github/workflows/release-k8s.yaml @@ -12,5 +12,6 @@ jobs: distribution: otelcol-k8s goos: '[ "linux" ]' goarch: '[ "amd64", "arm64", "ppc64le", "s390x" ]' + cgo: '[]' secrets: inherit permissions: write-all diff --git a/cmd/goreleaser/internal/configure.go b/cmd/goreleaser/internal/configure.go index 7a865165..16c8f333 100644 --- a/cmd/goreleaser/internal/configure.go +++ b/cmd/goreleaser/internal/configure.go @@ -21,6 +21,7 @@ package internal import ( "fmt" + "log" "path" "strings" @@ -35,15 +36,15 @@ var ( ArmVersions = []string{"7"} ) -func Generate(dist string) config.Project { +func Generate(dist string, cgo string) config.Project { return config.Project{ ProjectName: "opentelemetry-collector-releases", Checksum: config.Checksum{ NameTemplate: fmt.Sprintf("{{ .ProjectName }}_%v_checksums.txt", dist), }, Env: []string{"COSIGN_YES=true"}, - Builds: Builds(dist), - Archives: Archives(dist), + Builds: Builds(dist, cgo), + Archives: Archives(dist, cgo), MSI: WinPackages(dist), NFPMs: Packages(dist), Dockers: DockerImages(dist), @@ -52,15 +53,22 @@ func Generate(dist string) config.Project { DockerSigns: DockerSigns(), SBOMs: SBOM(), Version: 2, - Monorepo: config.Monorepo{ + Monorepo: config.Monorepo{ TagPrefix: "v", }, } } -func Builds(dist string) []config.Build { - return []config.Build{ - Build(dist), +func Builds(dist string, cgo string) []config.Build { + if len(cgo) == 0 { + return []config.Build{ + Build(dist), + } + } else if cgo == "darwin" { + return BuildCGO(dist) + } else { + log.Fatal("Unsupport CGO OS version") + return []config.Build{} } } @@ -90,19 +98,58 @@ func Build(dist string) config.Build { } } -func Archives(dist string) (r []config.Archive) { +func BuildCGO(dist string) []config.Build { + return []config.Build{ + { + ID: dist, + Dir: "_build", + Binary: dist, + BuildDetails: config.BuildDetails{ + Env: []string{"CGO_ENABLED=0"}, + Flags: []string{"-trimpath"}, + Ldflags: []string{"-s", "-w"}, + }, + Goos: []string{"linux", "windows"}, + Goarch: Architectures, + Goarm: ArmVersions, + Ignore: []config.IgnoredBuild{ + {Goos: "windows", Goarch: "arm"}, + {Goos: "windows", Goarch: "arm64"}, + {Goos: "windows", Goarch: "s390x"}, + }, + }, + { + ID: getCgoDistId(dist), + Dir: "_build", + Binary: dist, + BuildDetails: config.BuildDetails{ + Env: []string{"CGO_ENABLED=1", "CGO_LDFLAGS='-fstack-protector'"}, + Flags: []string{"-trimpath", "-buildmode=pie"}, + Ldflags: []string{"-s", "-w"}, + }, + Goos: []string{"darwin"}, + Goarch: []string{"amd64", "arm64"}, + }, + } +} + +func Archives(dist string, cgo string) (r []config.Archive) { return []config.Archive{ - Archive(dist), + Archive(dist, cgo), } } // Archive configures a goreleaser archive (tarball). // https://goreleaser.com/customization/archive/ -func Archive(dist string) config.Archive { +func Archive(dist string, cgo string) config.Archive { + builds := []string{dist} + if len(cgo) > 0 { + builds = append(builds, getCgoDistId(dist)) + } return config.Archive{ ID: dist, NameTemplate: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}", - Builds: []string{dist}, + Builds: builds, } } @@ -328,3 +375,7 @@ func SBOM() []config.SBOM { }, } } + +func getCgoDistId(dist string) string { + return dist + "-cgo" +} diff --git a/cmd/goreleaser/main.go b/cmd/goreleaser/main.go index b466e06d..e982d2c6 100644 --- a/cmd/goreleaser/main.go +++ b/cmd/goreleaser/main.go @@ -25,6 +25,7 @@ import ( ) var distFlag = flag.String("d", "", "Collector distributions to build") +var cgoFlag = flag.String("c", "", "Cgo enabled flag, followed by the OS that need to be supported, currently only darwin is supported, i.e. '-c darwin'") func main() { flag.Parse() @@ -33,7 +34,7 @@ func main() { log.Fatal("no distribution to build") } - project := internal.Generate(*distFlag) + project := internal.Generate(*distFlag, *cgoFlag) partial := map[string]any{ "partial": map[string]any{ diff --git a/distributions/otelcol-contrib/.goreleaser.yaml b/distributions/otelcol-contrib/.goreleaser.yaml index 79185590..5b2ac170 100644 --- a/distributions/otelcol-contrib/.goreleaser.yaml +++ b/distributions/otelcol-contrib/.goreleaser.yaml @@ -14,7 +14,6 @@ msi: builds: - id: otelcol-contrib goos: - - darwin - linux - windows goarch: @@ -27,12 +26,6 @@ builds: goarm: - "7" ignore: - - goos: darwin - goarch: "386" - - goos: darwin - goarch: arm - - goos: darwin - goarch: s390x - goos: windows goarch: arm - goos: windows @@ -48,10 +41,28 @@ builds: - -trimpath env: - CGO_ENABLED=0 + - id: otelcol-contrib-cgo + goos: + - darwin + goarch: + - amd64 + - arm64 + dir: _build + binary: otelcol-contrib + ldflags: + - -s + - -w + flags: + - -trimpath + - -buildmode=pie + env: + - CGO_ENABLED=1 + - CGO_LDFLAGS='-fstack-protector' archives: - id: otelcol-contrib builds: - otelcol-contrib + - otelcol-contrib-cgo name_template: '{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}' nfpms: - package_name: otelcol-contrib diff --git a/scripts/generate-goreleaser.sh b/scripts/generate-goreleaser.sh index 7b6c7ae7..fbca7bb4 100755 --- a/scripts/generate-goreleaser.sh +++ b/scripts/generate-goreleaser.sh @@ -23,5 +23,9 @@ 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" + if [[ "${distribution}" == "otelcol-contrib" ]]; then + ${GO} run cmd/goreleaser/main.go -d "${distribution}" -c "darwin" > "./distributions/${distribution}/.goreleaser.yaml" + else + ${GO} run cmd/goreleaser/main.go -d "${distribution}" > "./distributions/${distribution}/.goreleaser.yaml" + fi done