Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit e6e64be

Browse files
committed
Build and release multiplatform images using ko
1 parent 2a7a447 commit e6e64be

File tree

10 files changed

+242
-295
lines changed

10 files changed

+242
-295
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
name: Build and Release
2+
3+
on:
4+
workflow_dispatch:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
tags:
11+
- 'v*'
12+
pull_request:
13+
14+
permissions:
15+
contents: write
16+
packages: write
17+
18+
jobs:
19+
test:
20+
name: Test/Cover
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v3
26+
27+
- name: Set up Go
28+
uses: actions/setup-go@v3
29+
with:
30+
go-version: '1.19'
31+
32+
- name: Go caches
33+
uses: actions/cache@v2
34+
with:
35+
path: |
36+
~/go/pkg/mod
37+
key: ${{ github.job }}-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
38+
restore-keys: |
39+
${{ github.job }}-${{ runner.os }}-go-
40+
41+
- name: Run test/cover
42+
run: WITH_DEPENDENCIES=true make cover
43+
env:
44+
TEST_OUTPUT_DIR: /tmp/test-results/
45+
COVER_OUTPUT_DIR: /tmp/cover-results/
46+
47+
publish-images:
48+
name: Build and publish images
49+
if: github.event_name != 'pull_request'
50+
runs-on: ubuntu-latest
51+
needs: test
52+
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v3
56+
57+
- name: Set up Go
58+
uses: actions/setup-go@v3
59+
with:
60+
go-version: '1.19'
61+
62+
- name: Go caches
63+
uses: actions/cache@v2
64+
with:
65+
path: |
66+
~/go/pkg/mod
67+
key: ${{ github.job }}-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
68+
restore-keys: |
69+
${{ github.job }}-${{ runner.os }}-go-
70+
71+
- name: Installing ko
72+
run: go install github.com/google/ko@v0.11.2
73+
74+
- name: Login to Docker Hub
75+
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
76+
uses: docker/login-action@v2
77+
with:
78+
username: ${{ secrets.DOCKERHUB_USERNAME }}
79+
password: ${{ secrets.DOCKERHUB_TOKEN }}
80+
81+
- name: Login to GCR
82+
uses: docker/login-action@v2
83+
with:
84+
registry: gcr.io
85+
username: _json_key
86+
password: ${{ secrets.GCLOUD_SERVICEACCOUNT_KEY }}
87+
88+
- name: Set IMAGE_TAG
89+
id: image-tag
90+
run: |
91+
IMAGE_TAG=${GITHUB_SHA}
92+
[[ ${GITHUB_REF_TYPE} == "tag" ]] && IMAGE_TAG=${GITHUB_REF_NAME}
93+
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_OUTPUT
94+
95+
- name: Publish container images to DockerHub
96+
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
97+
env:
98+
KO_DOCKER_REPO: docker.io/triggermesh
99+
KOFLAGS: --jobs=4 --platform=linux/amd64,linux/arm64,linux/ppc64le --push=${{ github.event_name != 'pull_request' }}
100+
DIST_DIR: ./releases/manifests
101+
run: |
102+
IMAGE_TAG=${{ steps.image-tag.outputs.IMAGE_TAG }} make release
103+
104+
- name: Publish container images to GCR
105+
env:
106+
KO_DOCKER_REPO: gcr.io/triggermesh
107+
KOFLAGS: --jobs=4 --platform=linux/amd64,linux/arm64,linux/ppc64le --push=false
108+
DIST_DIR: ./releases/manifests
109+
run: |
110+
pushd hack/manifest-cleaner
111+
go install .
112+
popd
113+
114+
IMAGE_TAG=${{ steps.image-tag.outputs.IMAGE_TAG }} make release
115+
116+
declare -a release_files=(
117+
triggermesh-core-crds.yaml
118+
triggermesh-core.yaml
119+
)
120+
121+
for f in "${release_files[@]}"; do
122+
manifest-cleaner <"${DIST_DIR}/${f}" >"${DIST_DIR}/${f}.clean"
123+
mv "${DIST_DIR}/${f}.clean" "${DIST_DIR}/${f}"
124+
done
125+
126+
- name: Upload artifact
127+
uses: actions/upload-artifact@master
128+
with:
129+
name: manifests
130+
path: ./releases/manifests
131+
retention-days: 14
132+
133+
release:
134+
name: Create Release
135+
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
136+
runs-on: ubuntu-latest
137+
needs: publish-images
138+
permissions:
139+
contents: write
140+
steps:
141+
142+
- name: Checkout
143+
uses: actions/checkout@v3
144+
with:
145+
fetch-depth: 0
146+
147+
- name: Download artifact
148+
uses: actions/download-artifact@master
149+
with:
150+
name: manifests
151+
path: ./releases/manifests
152+
153+
- name: Run GoReleaser
154+
uses: goreleaser/goreleaser-action@v4
155+
with:
156+
distribution: goreleaser
157+
version: latest
158+
args: release --rm-dist
159+
env:
160+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/container.yaml

Lines changed: 0 additions & 63 deletions
This file was deleted.

.github/workflows/release.yaml

Lines changed: 0 additions & 41 deletions
This file was deleted.

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ builds:
1515
- amd64
1616
- arm64
1717
- arm
18+
- ppc64le
1819
goarm:
1920
- "7"
2021

Makefile

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ OUTPUT_DIR ?= $(BASE_DIR)/_output
1111
COMMANDS := $(notdir $(wildcard cmd/*))
1212

1313
# Commands and images that require custom build proccess
14-
CUSTOM_BUILD_BINARIES :=
1514
CUSTOM_BUILD_IMAGES :=
1615

1716
BIN_OUTPUT_DIR ?= $(OUTPUT_DIR)
@@ -51,17 +50,12 @@ WITH_DEPENDENCIES ?=
5150
LDFLAGS = -w -s
5251
LDFLAGS_STATIC = $(LDFLAGS) -extldflags=-static
5352

53+
TAG_REGEX := ^v([0-9]{1,}\.){2}[0-9]{1,}$
54+
5455
HAS_GOTESTSUM := $(shell command -v gotestsum;)
5556
HAS_GOLANGCI_LINT := $(shell command -v golangci-lint;)
5657

57-
SED := sed -i
58-
59-
OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
60-
ifeq ($(OS),$(filter $(OS),Darwin FreeBSD NetBSD))
61-
SED += ''
62-
endif
63-
64-
.PHONY: help all build release vm-images test lint fmt fmt-test images clean install-gotestsum install-golangci-lint deploy undeploy
58+
.PHONY: help all build release test lint fmt fmt-test images clean install-gotestsum install-golangci-lint deploy undeploy
6559

6660
.DEFAULT_GOAL := build
6761

@@ -83,81 +77,56 @@ help: ## Display this help
8377

8478
build: $(COMMANDS) ## Build all artifacts
8579

86-
$(filter-out $(CUSTOM_BUILD_BINARIES), $(COMMANDS)): ## Build artifact
80+
$(COMMANDS): ## Build artifact
8781
$(GO) build -ldflags "$(LDFLAGS_STATIC)" -o $(BIN_OUTPUT_DIR)/$@ ./cmd/$@
8882

8983
deploy: ## Deploy TriggerMesh stack to default Kubernetes cluster
9084
$(KO) resolve -f $(BASE_DIR)/config > $(BASE_DIR)/triggermesh-core-$(IMAGE_TAG).yaml
91-
@for component in $(CUSTOM_BUILD_IMAGES); do \
92-
$(MAKE) -C ./cmd/$$component build CONTEXT=$(BASE_DIR) IMAGE_TAG=$(KO_DOCKER_REPO)/$$component-$(IMAGE_TAG) && \
93-
$(MAKE) -C ./cmd/$$component push IMAGE_TAG=$(KO_DOCKER_REPO)/$$component-$(IMAGE_TAG) && \
94-
$(SED) 's|value: .*'$$component'.*|value: $(KO_DOCKER_REPO)/'$$component'-$(IMAGE_TAG)|g' $(BASE_DIR)/triggermesh-core-$(IMAGE_TAG).yaml || exit 1; \
95-
done
96-
9785
$(KO) apply -f $(BASE_DIR)/triggermesh-core-$(IMAGE_TAG).yaml
9886
@rm $(BASE_DIR)/triggermesh-core-$(IMAGE_TAG).yaml
9987

10088
undeploy: ## Remove TriggerMesh stack from default Kubernetes cluster
10189
$(KO) delete -f $(BASE_DIR)/config
10290

103-
vm-images:
104-
@$(MAKE) -C packer/
105-
10691
release: ## Publish container images and generate release manifests
10792
@mkdir -p $(DIST_DIR)
10893
$(KO) resolve -f config/ -l 'triggermesh.io/crd-install' > $(DIST_DIR)/triggermesh-core-crds.yaml
10994
@cp config/namespace/100-namespace.yaml $(DIST_DIR)/triggermesh-core.yaml
95+
ifeq ($(shell echo ${IMAGE_TAG} | egrep "${TAG_REGEX}"),${IMAGE_TAG})
11096
$(KO) resolve $(KOFLAGS) -B -t latest -f config/ -l '!triggermesh.io/crd-install' > /dev/null
97+
endif
11198
$(KO) resolve $(KOFLAGS) -B -t $(IMAGE_TAG) --tag-only -f config/ -l '!triggermesh.io/crd-install' >> $(DIST_DIR)/triggermesh-core.yaml
11299

113-
@for component in $(CUSTOM_BUILD_IMAGES); do \
114-
$(MAKE) -C ./cmd/$$component build CONTEXT=$(BASE_DIR) IMAGE_TAG=$(KO_DOCKER_REPO)/$$component:$(IMAGE_TAG) && \
115-
$(MAKE) -C ./cmd/$$component tag IMAGE_TAG=$(KO_DOCKER_REPO)/$$component:$(IMAGE_TAG) TAGS=$(KO_DOCKER_REPO)/$$component:latest && \
116-
$(MAKE) -C ./cmd/$$component push IMAGE_TAG=$(KO_DOCKER_REPO)/$$component && \
117-
$(SED) 's/'$$component':.*/'$$component':$(IMAGE_TAG)/g' $(DIST_DIR)/triggermesh-core.yaml || exit 1; \
118-
done
119-
120100
gen-apidocs: ## Generate API docs
121101
GOPATH="" OUTPUT_DIR=$(DOCS_OUTPUT_DIR) ./hack/gen-api-reference-docs.sh
122102

123-
124103
GOPKGS_LIST ?= $(filter-out $(GOPKGS_SKIP_TESTS), $(shell go list $(GOPKGS)))
125104
ifdef WITH_DEPENDENCIES
126105
GOPKGS_LIST += $(GOPKGS_TESTS_WITH_DEPENDENCIES)
127106
endif
107+
128108
test: install-gotestsum ## Run unit tests
129109
@mkdir -p $(TEST_OUTPUT_DIR)
130-
131110
$(GOTEST) -p=1 -race -cover -coverprofile=$(TEST_OUTPUT_DIR)/$(KREPO)-c.out $(GOPKGS_LIST)
132111

133-
@for component in $(CUSTOM_BUILD_IMAGES); do \
134-
$(MAKE) -C ./cmd/$$component test || exit 1; \
135-
done
136-
137112
cover: test ## Generate code coverage
138113
@mkdir -p $(COVER_OUTPUT_DIR)
139114
$(GOTOOL) cover -html=$(TEST_OUTPUT_DIR)/$(KREPO)-c.out -o $(COVER_OUTPUT_DIR)/$(KREPO)-coverage.html
140115

141116
lint: install-golangci-lint ## Lint source files
142-
$(GOLINT) $(GOPKGS) ./test/...
117+
$(GOLINT) $(GOPKGS)
143118

144119
fmt: ## Format source files
145120
$(GOFMT) -s -w $(shell $(GO) list -f '{{$$d := .Dir}}{{range .GoFiles}}{{$$d}}/{{.}} {{end}} {{$$d := .Dir}}{{range .TestGoFiles}}{{$$d}}/{{.}} {{end}}' $(GOPKGS))
146121

147122
fmt-test: ## Check source formatting
148123
@test -z $(shell $(GOFMT) -l $(shell $(GO) list -f '{{$$d := .Dir}}{{range .GoFiles}}{{$$d}}/{{.}} {{end}} {{$$d := .Dir}}{{range .TestGoFiles}}{{$$d}}/{{.}} {{end}}' $(GOPKGS)))
149124

150-
KO_PUBLISHABLE = $(filter-out $(CUSTOM_BUILD_IMAGES), $(COMMANDS))
151-
KO_IMAGES = $(foreach cmd,$(KO_PUBLISHABLE),$(cmd).image)
152-
CUSTOM_IMAGES = $(foreach cmd,$(CUSTOM_BUILD_IMAGES),$(cmd).image)
153-
154-
images: $(KO_IMAGES) $(CUSTOM_IMAGES) ## Build container images
125+
KO_IMAGES = $(foreach cmd,$(COMMANDS),$(cmd).image)
126+
images: $(KO_IMAGES) ## Build container images
155127
$(KO_IMAGES): %.image:
156128
$(KO) publish --push=false -B --tag-only -t $(IMAGE_TAG) ./cmd/$*
157129

158-
$(CUSTOM_IMAGES): %.image:
159-
@$(MAKE) -C ./cmd/$* image CONTEXT=$(BASE_DIR) IMAGE_TAG=$(KO_DOCKER_REPO)/$*:$(IMAGE_TAG)
160-
161130
clean: ## Clean build artifacts
162131
@for bin in $(COMMANDS) ; do \
163132
$(RM) -v $(BIN_OUTPUT_DIR)/$$bin; \

0 commit comments

Comments
 (0)