Skip to content

Commit

Permalink
build: add build and push image
Browse files Browse the repository at this point in the history
  • Loading branch information
blackhorseya committed May 20, 2023
1 parent 893f520 commit ae4a342
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build --@io_bazel_rules_docker//transitions:enable=false
run --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
37 changes: 37 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push")

# gazelle:prefix github.com/blackhorseya/irent
# gazelle:proto disable_global

gazelle(name = "gazelle")

container_image(
name = "irent-app-restful-image",
base = "@alpine_linux_amd64//image",
directory = "/workspace",
entrypoint = ["./app"],
files = ["//cmd/restful/app"],
workdir = "/workspace",
)

container_push(
name = "push-irent-app-restful-image",
format = "Docker",
image = ":irent-app-restful-image",
registry = "gcr.io",
repository = "sean-side/irent-app-restful",
tag = "$(VERSION)",
)

container_image(
name = "irent-rental-cronjob-image",
base = "@alpine_linux_amd64//image",
directory = "/workspace",
entrypoint = ["./rental"],
files = ["//cmd/cronjob/rental"],
workdir = "/workspace",
)

container_push(
name = "push-irent-rental-cronjob-image",
format = "Docker",
image = ":irent-rental-cronjob-image",
registry = "gcr.io",
repository = "sean-side/irent-rental-cronjob",
tag = "$(VERSION)",
)
34 changes: 8 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,14 @@ build-go: gazelle ## build go binary
test-go: gazelle ## test go binary
@bazel test //...

.PHONY: build-image
build-image: check-SVC_NAME check-SVC_ADAPTER check-VERSION check-GITHUB_TOKEN ## build docker image with APP_NAME and VERSION
@docker build -t $(REGISTRY)/$(APP_NAME):$(VERSION) \
--label "app.name=$(APP_NAME)" \
--label "app.version=$(VERSION)" \
--build-arg MAIN_FOLDER=$(MAIN_FOLDER) \
--build-arg GITHUB_TOKEN=$(GITHUB_TOKEN) \
--platform linux/amd64 \
--pull --cache-from=$(REGISTRY)/$(APP_NAME) \
-f Dockerfile .

.PHONY: list-images
list-images: check-SVC_NAME check-SVC_ADAPTER ## list all images
@docker images --filter=label=app.name=$(APP_NAME)

.PHONY: prune-images
prune-images: check-SVC_NAME check-SVC_ADAPTER ## remove all images
@docker rmi -f `docker images --filter=label=app.name=$(APP_NAME) -q`

.PHONY: push-image
push-image: check-SVC_NAME check-SVC_ADAPTER check-VERSION ## push image to registry
@docker push $(REGISTRY)/$(APP_NAME):$(VERSION)
## docker
.PHONY: push-irent-app-restful-image
push-irent-app-restful-image: ## push irent app restful image
@bazel run //:$@ --define=VERSION=$(VERSION)

.PHONY: push-irent-rental-cronjob-image
push-irent-rental-cronjob-image: ## push irent rental cronjob image
@bazel run //:$@ --define=VERSION=$(VERSION)

.PHONY: gen
gen: gen-pb gen-wire gen-go gen-swagger ## generate all generate commands
Expand Down Expand Up @@ -111,10 +97,6 @@ gen-pb: ## generate protobuf messages and services
@protoc-go-inject-tag -input="./pkg/entity/domain/*/model/*.pb.go"
@echo Successfully injected tags

.PHONY: gen-build
gen-build: ## run gazelle using bazel
@bazel run //:gazelle

.PHONY: deploy
deploy: check-SVC_NAME check-SVC_ADAPTER check-VERSION check-DEPLOY_TO ## deploy the application via helm 3
@helm -n $(NS) upgrade --install $(DEPLOY_TO)-$(APP_NAME) \
Expand Down
27 changes: 27 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,30 @@ go_rules_dependencies()
go_register_toolchains(version = "1.19.3")

gazelle_dependencies()

## Docker
http_archive(
name = "io_bazel_rules_docker",
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
)

load(
"@io_bazel_rules_docker//repositories:repositories.bzl",
container_repositories = "repositories",
)

container_repositories()

load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")

container_deps()

load("@io_bazel_rules_docker//container:pull.bzl", "container_pull")

container_pull(
name = "alpine_linux_amd64",
registry = "index.docker.io",
repository = "library/alpine",
tag = "3.8",
)

0 comments on commit ae4a342

Please sign in to comment.