Skip to content

Commit

Permalink
Updates to make building more flexible
Browse files Browse the repository at this point in the history
* Clean up and rename targets
* Should not have PROGRAM as a dep for release, as it will build the wrong binary
* Add manifest to workflow, and add arch to DATE tags
  • Loading branch information
crunchyheath authored Jan 31, 2024
1 parent 90262d0 commit 76fe64d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 37 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ jobs:
if: github.repository == 'CrunchyData/pg_featureserv'
steps:

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x

- name: Checkout
uses: actions/checkout@v3

- name: Build
run: make build && make docker
- name: Build ARM64
run: make TARGETARCH=arm64 multi-stage-docker

- name: Build x86_64
run: make TARGETARCH=amd64 multi-stage-docker

- name: Docker Upload
env:
Expand All @@ -29,3 +27,9 @@ jobs:
run: |
echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
docker push --all-tags $DOCKER_REPO
docker manifest create $DOCKER_REPO:latest $DOCKER_REPO:latest-amd64 $DOCKER_REPO:latest-arm64
docker manifest push $DOCKER_REPO:latest
date_tag=$(docker images | grep featureserv | grep -v latest | grep -v arm64 | awk '{print $2}')
date_tag=${date_tag/-amd64/}
docker manifest create $DOCKER_REPO:${date_tag} $DOCKER_REPO:${date_tag}-amd64 $DOCKER_REPO:${date_tag}-arm64
docker manifest push $DOCKER_REPO:${date_tag}
40 changes: 32 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
FROM centos:7
ARG GOLANG_VERSION
ARG TARGETARCH
ARG VERSION
ARG BASE_REGISTRY
ARG BASE_IMAGE
ARG PLATFORM

FROM --platform=${PLATFORM} docker.io/library/golang:${GOLANG_VERSION}-alpine AS builder
LABEL stage=featureservbuilder

ARG TARGETARCH
ARG VERSION

LABEL vendor="Crunchy Data" \
url="https://crunchydata.com" \
release="${VERSION}" \
org.opencontainers.image.vendor="Crunchy Data" \
os.version="7.7"
WORKDIR /app
COPY . ./

RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -v -ldflags "-s -w -X main.programVersion=${VERSION}"

FROM --platform=${TARGETARCH} ${BASE_REGISTRY}/${BASE_IMAGE} AS multi-stage

COPY --from=builder /app/pg_featureserv .
COPY --from=builder /app/assets ./assets

VOLUME ["/config"]
VOLUME ["/assets"]

USER 1001
EXPOSE 9000

ENTRYPOINT ["./pg_featureserv"]
CMD []

FROM --platform=${PLATFORM} ${BASE_REGISTRY}/${BASE_IMAGE} AS local

ADD ./pg_featureserv .
ADD ./assets ./assets
Expand All @@ -21,10 +45,10 @@ ENTRYPOINT ["./pg_featureserv"]
CMD []

# To build
# make APPVERSION=1.1 clean build build-docker
# make APPVERSION=1.1 clean build docker

# To build using binaries from golang docker image
# make APPVERSION=1.1 clean bin-docker build-docker
# make APPVERSION=1.1 clean multi-stage-docker

# To run using an image build above
# docker run -dt -e DATABASE_URL=postgres://user:pass@host/dbname -p 9000:9000 pramsey/pg_featureserv:1.1
95 changes: 73 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,54 +1,102 @@

APPVERSION := latest
GOVERSION := 1.15
PROGRAM := pg_featureserv
CONTAINER := pramsey/$(PROGRAM)
DATE := $(shell date +%Y%m%d)
##AVAILABLE BUILD OPTIONS -
## APPVERSION - Variable to set the version label
## GOVERSION - Defaults to 1.21.6 but can be overriden, uses alpine go container as base
## PROGRAM - Name of binary, pg_featureserv
## CONTAINER - prefix and name of the generated container
## DATE - Date String used as alternate tag for generated containers
## BASE_REGISTRY - This is the registry to pull the base image from
## BASE_IMAGE - The base image to use for the final container
## TARGETARCH - The architecture the resulting image is based on and the binary is compiled for
## IMAGE_TAG - The tag to be applied to the container

APPVERSION ?= latest
GOVERSION ?= 1.21.6
PROGRAM ?= pg_featureserv
CONTAINER ?= pramsey/$(PROGRAM)
DATE ?= $(shell date +%Y%m%d)
BASE_REGISTRY ?= registry.access.redhat.com
BASE_IMAGE ?= ubi8-micro
SYSTEMARCH = $(shell uname -i)

ifeq ($(SYSTEMARCH), x86_64)
TARGETARCH ?= amd64
PLATFORM=amd64
else
TARGETARCH ?= arm64
PLATFORM=arm64
endif

IMAGE_TAG ?= $(APPVERSION)-$(TARGETARCH)
DATE_TAG ?= $(DATE)-$(TARGETARCH)

RM = /bin/rm
CP = /bin/cp
MKDIR = /bin/mkdir
SED = /usr/bin/sed

.PHONY: build bin-for-docker check clean build-in-docker docs install release test uninstall
.PHONY: build bin-for-docker build-common build-in-docker check clean docker docs install multi-stage-docker release set-local set-multi-stage test uninstall

.DEFAULT_GOAL := help

GOFILES := $(shell find . -type f -name '*.go')

check: ## This checks the current version of Go installed locally
check: ## This checks the current version of Go installed locally
go version

clean: ## This will clean all local build artifacts
clean: ## This will clean all local build artifacts
$(info Cleaning project...)
@rm -f $(PROGRAM)
@rm -rf docs/*
@docker image inspect $(CONTAINER):$(APPVERSION) >/dev/null 2>&1 && docker rmi $(CONTAINER):$(APPVERSION) $(CONTAINER):$(DATE) || echo -n ""
@docker image inspect $(CONTAINER):$(IMAGE_TAG) >/dev/null 2>&1 && docker rmi -f $(shell docker images --filter label=release=latest --filter=reference="*featureserv:*" -q) || echo -n ""

docs: ## Generate docs
docs: ## Generate docs
@rm -rf docs/* && cd hugo && hugo && cd ..

build: $(PROGRAM) ## just an alias
build: $(PROGRAM) ## Build a local binary using APPVERSION parameter or CI as default

$(PROGRAM): $(GOFILES) ## Build a local binary using APPVERSION parameter or CI as default
$(PROGRAM): $(GOFILES)
go build -v -ldflags "-s -w -X github.com/CrunchyData/pg_featureserv/conf.setVersion=$(APPVERSION)"

bin-for-docker: $(GOFILES) ## Build a local binary using APPVERSION parameter or CI as default (to be used in docker image)
bin-for-docker: $(GOFILES) ## Build a local binary using APPVERSION parameter or CI as default (to be used in docker image)
# to be used in docker the built binary needs the CGO_ENABLED=0 option
CGO_ENABLED=0 go build -v -ldflags "-s -w -X github.com/CrunchyData/pg_featureserv/conf.setVersion=$(APPVERSION)"

build-in-docker: $(GOFILES) ## Build a local binary based of a golang base docker image without the need of a local go environment
docker run --rm -v "$(PWD)":/usr/src/myapp:z -w /usr/src/myapp golang:$(GOVERSION) make APPVERSION=$(APPVERSION) $(PROGRAM)

docker: bin-for-docker Dockerfile ## Generate a CentOS 7 container with APPVERSION tag, with the dedicated binary installed into
docker build -f Dockerfile --build-arg VERSION=$(APPVERSION) -t $(CONTAINER):$(APPVERSION) -t $(CONTAINER):$(DATE) .
build-common: Dockerfile
docker build -f Dockerfile \
--target $(BUILDTYPE) \
--build-arg VERSION=$(APPVERSION) \
--build-arg GOLANG_VERSION=$(GOVERSION) \
--build-arg TARGETARCH=$(TARGETARCH) \
--build-arg PLATFORM=$(PLATFORM) \
--build-arg BASE_REGISTRY=$(BASE_REGISTRY) \
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
--label vendor="Crunchy Data" \
--label url="https://crunchydata.com" \
--label release="$(APPVERSION)" \
--label org.opencontainers.image.vendor="Crunchy Data" \
--label os.version="7.7" \
-t $(CONTAINER):$(IMAGE_TAG) -t $(CONTAINER):$(DATE_TAG) .
docker image prune --filter label=stage=featureservbuilder -f

set-local:
$(eval BUILDTYPE = local)

set-multi-stage:
$(eval BUILDTYPE = multi-stage)

release: clean docs $(PROGRAM) docker ## Generate the docs, a local build, and then uses the local build to generate a CentOS 7 container
docker: bin-for-docker Dockerfile set-local build-common ## Generate a BASE_IMAGE container with APPVERSION tag, using a locally built binary

test: ## Run the tests locally
multi-stage-docker: Dockerfile set-multi-stage build-common ## Generate a BASE_IMAGE container with APPVERSION tag, using a binary built in an alpine golang build container

release: clean docs docker ## Generate the docs, a local build, and then uses the local build to generate BASE_IMAGE container

test: ## Run the tests locally
go test -v ./internal/cql ./internal/service

install: $(PROGRAM) docs ## This will install the program locally
install: $(PROGRAM) docs ## This will install the program locally
$(MKDIR) -p $(DESTDIR)/usr/bin
$(MKDIR) -p $(DESTDIR)/usr/share/$(PROGRAM)
$(MKDIR) -p $(DESTDIR)/etc
Expand All @@ -57,14 +105,17 @@ install: $(PROGRAM) docs ## This will install the program locally
$(CP) -r assets $(DESTDIR)/usr/share/$(PROGRAM)/assets
$(CP) -r docs $(DESTDIR)/usr/share/$(PROGRAM)/docs

uninstall: ## This will uninstall the program from your local system
uninstall: ## This will uninstall the program from your local system
$(RM) $(DESTDIR)/usr/bin/$(PROGRAM)
$(RM) $(DESTDIR)/etc/$(PROGRAM).toml
$(RM) -r $(DESTDIR)/usr/share/$(PROGRAM)

help: ## Prints this help message
help: ## Prints this help message
@echo ""
@echo ""
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | fgrep -v : | sed -e 's/\\$$//' | sed -e 's/.*##//'
@echo ""
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/:.*##/:/'
@echo "BUILD TARGETS:"
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | fgrep : | sed -e 's/\\$$//' | sed -e 's/:.*##/:/'
@echo ""
@echo ""

0 comments on commit 76fe64d

Please sign in to comment.