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

Use goreleaser everywhere #50

Merged
merged 2 commits into from
Dec 17, 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
Binary file removed .DS_Store
Binary file not shown.
40 changes: 26 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,40 @@ jobs:
matrix:
component: [webhook, api, operator]
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v4
-
name: Docker meta
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: cagip/kubi-${{ matrix.component }}
-
name: Login to DockerHub
- uses: actions/setup-go@v5
with:
go-version: '1.23'
- name: run tests
run: make test
- uses: goreleaser/goreleaser-action@v6
if: github.event_name == 'pull_request'
with:
distribution: goreleaser
version: latest
args: release --snapshot --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ORG: ${{ github.REPOSITORY_OWNER }}

- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v6

- uses: goreleaser/goreleaser-action@v6
if: github.event_name != 'pull_request'
with:
context: .
file: Dockerfile.${{ matrix.component }}
push: ${{ GitHub.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
- run: git fetch --force --tags
- uses: actions/setup-go@v5
with:
go-version: '1.22'
go-version: '1.23'
- uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser
version: latest
args: release --clean
workdir: ./cmd/operator
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ORG: ${{ github.repository_owner }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ vendor
coverage.*
kubi
bin
build/*
hack/bin
dist/
114 changes: 114 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
project_name: kubi
before:
hooks:
- make clean
- make deps
builds:
- main: ./cmd/api/main.go
id: api
binary: kubi-api
env:
- CGO_ENABLED=0
goarch:
- amd64
- arm64
goos:
- linux
ldflags:
- -s -w -X main.version={{ if .IsSnapshot }}{{ .ShortCommit }}{{ else }}{{ .Version }}{{ end }}
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath

- main: ./cmd/authorization-webhook/main.go
id: webhook
binary: kubi-webhook
env:
- CGO_ENABLED=0
goarch:
- amd64
- arm64
goos:
- linux
ldflags:
- -s -w -X main.version={{ if .IsSnapshot }}{{ .ShortCommit }}{{ else }}{{ .Version }}{{ end }}
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath

- main: ./cmd/operator/main.go
id: operator
binary: kubi-operator
env:
- CGO_ENABLED=0
goarch:
- amd64
- arm64
goos:
- linux
ldflags:
- -s -w -X main.version={{ if .IsSnapshot }}{{ .ShortCommit }}{{ else }}{{ .Version }}{{ end }}
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath

dockers:
- id: docker-operator
ids:
- operator
use: buildx
build_flag_templates:
- "--pull"
- "--platform=linux/amd64"
- "--build-arg=BINARYNAME=operator"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.source=https://github.com/{{.Env.ORG}}/{{.ProjectName}}"
- "--label=org.opencontainers.image.url=https://github.com/{{.Env.ORG}}/{{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
image_templates:
- "ghcr.io/{{.Env.ORG}}/{{.ProjectName}}-operator:{{.ShortCommit}}-amd64"

- id: docker-api
ids:
- api
use: buildx
build_flag_templates:
- "--pull"
- "--platform=linux/amd64"
- "--build-arg=BINARYNAME=api"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.source=https://github.com/{{.Env.ORG}}/{{.ProjectName}}"
- "--label=org.opencontainers.image.url=https://github.com/{{.Env.ORG}}/{{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
image_templates:
- "ghcr.io/{{.Env.ORG}}/{{.ProjectName}}-api:{{.ShortCommit}}-amd64"

- id: docker-webhook
ids:
- webhook
use: buildx
build_flag_templates:
- "--pull"
- "--platform=linux/amd64"
- "--build-arg=BINARYNAME=webhook"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.source=https://github.com/{{.Env.ORG}}/{{.ProjectName}}"
- "--label=org.opencontainers.image.url=https://github.com/{{.Env.ORG}}/{{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
image_templates:
- "ghcr.io/{{.Env.ORG}}/{{.ProjectName}}-webhook:{{.ShortCommit}}-amd64"

snapshot:
name_template: "{{ .ShortCommit }}"

release:
disable: true

changelog:
skip: true
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM scratch
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ARG BINARYNAME

COPY kubi-${BINARYNAME} /
CMD ["./kubi-${BINARYNAME}"]
10 changes: 0 additions & 10 deletions Dockerfile.api

This file was deleted.

11 changes: 0 additions & 11 deletions Dockerfile.operator

This file was deleted.

10 changes: 0 additions & 10 deletions Dockerfile.webhook

This file was deleted.

52 changes: 24 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
.PHONY: clean dependecy test test-only codegen dependency build build-operator build-api build-webhook
.PHONY: clean test deps build build-operator build-auth bootstrap-tools

REPO= github.com/ca-gip/kubi
IMAGE= kubi
DOCKER_REPO= cagip
HACKDIR=./hack/bin
GORELEASER_CMD=$(HACKDIR)/goreleaser
ORG ?= ca-gip
VERSION=$(shell git rev-parse --short HEAD)

$(HACKDIR):
mkdir -p $(HACKDIR)

bootstrap-tools: $(HACKDIR)
command -v $(HACKDIR)/goreleaser || VERSION=v1.24.0 TMPDIR=$(HACKDIR) bash hack/goreleaser-install.sh
command -v staticcheck || go install honnef.co/go/tools/cmd/staticcheck@latest
chmod +x $(HACKDIR)/goreleaser

clean:
rm -rf vendor build/*

dependency:
go mod vendor
build: bootstrap-tools deps
ORG=${ORG} $(GORELEASER_CMD) build --clean --snapshot

codegen: dependency
deps:
go mod tidy
go mod vendor
bash hack/update-codegen.sh
go mod tidy

test: codegen
GOARCH=amd64 go test ./internal/services ./pkg/types ./internal/utils

test-only:
@echo "-> Test only kubi operator binary"
GOARCH=amd64 go test ./internal/services ./pkg/types ./internal/utils

build-operator: test
@echo "-> Building kubi operator"
CGO_ENABLED=0 GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -v -o ./build/kubi-operator $(GOPATH)/src/$(REPO)/cmd/operator/main.go

build-api: test
@echo "-> Building kubi api"
CGO_ENABLED=0 GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -v -o ./build/kubi-api $(GOPATH)/src/$(REPO)/cmd/api/main.go

build-webhook: test
@echo "-> Building kubi authorization webhook"
CGO_ENABLED=0 GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' -v -o ./build/kubi-webhook $(GOPATH)/src/$(REPO)/cmd/authorization-webhook/main.go

build: build-webhook build-operator build-api

test: bootstrap-tools
go test ./...
staticcheck ./...

image:
ORG=${ORG} $(GORELEASER_CMD) release --clean --snapshot
30 changes: 30 additions & 0 deletions hack/goreleaser-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh
set -e

RELEASES_URL="https://github.com/goreleaser/goreleaser/releases"
FILE_BASENAME="goreleaser"

test -z "$VERSION" && {
echo "Unable to get goreleaser version." >&2
exit 1
}

test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
# goreleaser uses arm64 instead of aarch64
goreleaser_arch=$(uname -m | sed -e 's/aarch64/arm64/g' -e 's/ppc64le/ppc64/' -e 's/armv7l/armv7/' )
TAR_FILE="$TMPDIR/${FILE_BASENAME}_$(uname -s)_${goreleaser_arch}.tar.gz"
export TAR_FILE

(
echo "Downloading GoReleaser $VERSION..."
curl -sfLo "$TAR_FILE" \
"$RELEASES_URL/download/$VERSION/${FILE_BASENAME}_$(uname -s)_${goreleaser_arch}.tar.gz"
cd "$TMPDIR"
curl -sfLo "checksums.txt" "$RELEASES_URL/download/$VERSION/checksums.txt"
echo "Verifying checksums..."
sha256sum --ignore-missing --quiet --check checksums.txt
)

tar -xf "$TAR_FILE" -O goreleaser > "$TMPDIR/goreleaser"
rm "$TMPDIR/checksums.txt"
rm "$TAR_FILE"
Loading