2025-04-27 20:09:52 +02:00
#!/usr/bin/env bash
#
# Makefile with some common workflow for dev, build and test
#
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk command is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
# The following help command is Licensed under the Apache License, Version 2.0 (the "License")
# Copyright 2023 The Kubernetes Authors.
.PHONY : help
help : ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $( MAKEFILE_LIST)
2020-05-09 16:07:46 +01:00
GO ?= go
GOLANGCILINT ?= golangci-lint
2020-03-29 14:54:36 +01:00
BINARY := oauth2-proxy
2020-07-07 09:53:32 +01:00
VERSION ?= $( shell git describe --always --dirty --tags 2>/dev/null || echo "undefined" )
2019-10-08 03:53:46 +07:00
# Allow to override image registry.
2024-01-20 19:48:04 +01:00
REGISTRY ?= quay.io/oauth2-proxy
REPOSITORY ?= oauth2-proxy
2023-11-18 14:56:29 +01:00
DATE := $( shell date +"%Y%m%d" )
2019-01-07 16:41:11 +00:00
.NOTPARALLEL :
2019-01-04 10:58:30 +00:00
2020-05-09 16:07:46 +01:00
GO_MAJOR_VERSION = $( shell $( GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
GO_MINOR_VERSION = $( shell $( GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
2025-01-17 17:56:28 +01:00
GO_MOD_VERSION = $( shell sed -En 's/^go ([[:digit:]]\.[[:digit:]]+)\.[[:digit:]]+/\1/p' go.mod)
MINIMUM_SUPPORTED_GO_MAJOR_VERSION = $( shell echo ${ GO_MOD_VERSION } | cut -d' ' -f1 | cut -d'.' -f1)
MINIMUM_SUPPORTED_GO_MINOR_VERSION = $( shell echo ${ GO_MOD_VERSION } | cut -d' ' -f1 | cut -d'.' -f2)
2020-05-09 16:07:46 +01:00
GO_VERSION_VALIDATION_ERR_MSG = Golang version is not supported, please update to at least $( MINIMUM_SUPPORTED_GO_MAJOR_VERSION) .$( MINIMUM_SUPPORTED_GO_MINOR_VERSION)
2020-05-10 07:29:37 +01:00
i f e q ( $( COVER ) , t r u e )
TESTCOVER ?= -coverprofile c.out
e n d i f
2025-04-27 20:09:52 +02:00
##@ Build
2019-01-04 10:58:30 +00:00
.PHONY : build
2025-04-27 20:09:52 +02:00
build : validate -go -version clean $( BINARY ) ## Build and create oauth2-proxy binary from current source code
2019-01-04 10:58:30 +00:00
$(BINARY) :
2024-07-14 22:09:17 +02:00
CGO_ENABLED = 0 $( GO) build -a -installsuffix cgo -ldflags= " -X github.com/oauth2-proxy/oauth2-proxy/v7/pkg/version.VERSION= ${ VERSION } " -o $@ github.com/oauth2-proxy/oauth2-proxy/v7
2019-01-04 10:58:30 +00:00
2025-01-17 17:56:28 +01:00
DOCKER_BUILDX_COMMON_ARGS ?= --build-arg BUILD_IMAGE = docker.io/library/golang:${ GO_MOD_VERSION } -bookworm --build-arg VERSION = ${ VERSION }
2024-08-27 21:36:36 -05:00
DOCKER_BUILD_PLATFORM ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v7,linux/s390x
2024-01-20 19:48:04 +01:00
DOCKER_BUILD_RUNTIME_IMAGE ?= gcr.io/distroless/static:nonroot
2025-01-17 17:56:28 +01:00
DOCKER_BUILDX_ARGS ?= --build-arg RUNTIME_IMAGE = ${ DOCKER_BUILD_RUNTIME_IMAGE } ${ DOCKER_BUILDX_COMMON_ARGS }
2024-01-20 19:48:04 +01:00
DOCKER_BUILDX := docker buildx build ${ DOCKER_BUILDX_ARGS } --pull
2023-09-08 18:18:20 +02:00
DOCKER_BUILDX_X_PLATFORM := $( DOCKER_BUILDX) --platform ${ DOCKER_BUILD_PLATFORM }
DOCKER_BUILDX_PUSH := $( DOCKER_BUILDX) --push
2021-11-14 17:50:12 +00:00
DOCKER_BUILDX_PUSH_X_PLATFORM := $( DOCKER_BUILDX_PUSH) --platform ${ DOCKER_BUILD_PLATFORM }
2021-09-21 08:17:59 -05:00
2024-08-27 21:36:36 -05:00
DOCKER_BUILD_PLATFORM_ALPINE ?= linux/amd64,linux/arm64,linux/ppc64le,linux/arm/v6,linux/arm/v7,linux/s390x
2025-03-12 07:46:30 +00:00
DOCKER_BUILD_RUNTIME_IMAGE_ALPINE ?= alpine:3.21.3
2025-01-17 17:56:28 +01:00
DOCKER_BUILDX_ARGS_ALPINE ?= --build-arg RUNTIME_IMAGE = ${ DOCKER_BUILD_RUNTIME_IMAGE_ALPINE } ${ DOCKER_BUILDX_COMMON_ARGS }
2024-01-20 19:48:04 +01:00
DOCKER_BUILDX_X_PLATFORM_ALPINE := docker buildx build ${ DOCKER_BUILDX_ARGS_ALPINE } --platform ${ DOCKER_BUILD_PLATFORM_ALPINE }
DOCKER_BUILDX_PUSH_X_PLATFORM_ALPINE := $( DOCKER_BUILDX_X_PLATFORM_ALPINE) --push
2025-04-27 20:09:52 +02:00
.PHONY : build -docker
build-docker : build -distroless build -alpine ## Build multi architecture docker images in both flavours (distroless / alpine)
.PHONY : build -distroless
build-distroless : ## Build multi architecture distroless based docker image
2024-01-20 19:48:04 +01:00
$( DOCKER_BUILDX_X_PLATFORM) -t $( REGISTRY) /$( REPOSITORY) :latest -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } .
2019-02-02 12:08:19 +13:00
2025-04-27 20:09:52 +02:00
.PHONY : build -alpine
build-alpine : ## Build multi architecture alpine based docker image
$( DOCKER_BUILDX_X_PLATFORM_ALPINE) -t $( REGISTRY) /$( REPOSITORY) :latest-alpine -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -alpine .
2023-11-18 14:56:29 +01:00
2025-04-27 20:09:52 +02:00
.PHONY : build -docker -all
build-docker-all : build -docker ## Build docker images for all supported architectures in both flavours (distroless / alpine)
2024-01-20 19:48:04 +01:00
$( DOCKER_BUILDX) --platform linux/amd64 -t $( REGISTRY) /$( REPOSITORY) :latest-amd64 -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -amd64 .
$( DOCKER_BUILDX) --platform linux/arm64 -t $( REGISTRY) /$( REPOSITORY) :latest-arm64 -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -arm64 .
$( DOCKER_BUILDX) --platform linux/ppc64le -t $( REGISTRY) /$( REPOSITORY) :latest-ppc64le -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -ppc64le .
$( DOCKER_BUILDX) --platform linux/arm/v7 -t $( REGISTRY) /$( REPOSITORY) :latest-armv7 -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -armv7 .
2024-08-27 21:36:36 -05:00
$( DOCKER_BUILDX) --platform linux/s390x -t $( REGISTRY) /$( REPOSITORY) :latest-s390x -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -s390x .
2019-02-08 10:05:57 +00:00
2025-04-27 20:09:52 +02:00
##@ Publish
.PHONY : push -docker
push-docker : push -distroless push -alpine ## Push multi architecture docker images for both flavours (distroless / alpine)
.PHONY : push -distroless
push-distroless : ## Push multi architecture distroless based docker image
$( DOCKER_BUILDX_PUSH_X_PLATFORM) -t $( REGISTRY) /$( REPOSITORY) :latest -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } .
.PHONY : push -alpine
push-alpine : ## Push multi architecture alpine based docker image
$( DOCKER_BUILDX_PUSH_X_PLATFORM_ALPINE) -t $( REGISTRY) /$( REPOSITORY) :latest-alpine -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -alpine .
.PHONY : push -docker -all
push-docker-all : push -docker ## Push docker images for all supported architectures for both flavours (distroless / alpine)
2024-01-20 19:48:04 +01:00
$( DOCKER_BUILDX_PUSH) --platform linux/amd64 -t $( REGISTRY) /$( REPOSITORY) :latest-amd64 -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -amd64 .
$( DOCKER_BUILDX_PUSH) --platform linux/arm64 -t $( REGISTRY) /$( REPOSITORY) :latest-arm64 -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -arm64 .
$( DOCKER_BUILDX_PUSH) --platform linux/ppc64le -t $( REGISTRY) /$( REPOSITORY) :latest-ppc64le -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -ppc64le .
$( DOCKER_BUILDX_PUSH) --platform linux/arm/v7 -t $( REGISTRY) /$( REPOSITORY) :latest-armv7 -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -armv7 .
2024-08-27 21:36:36 -05:00
$( DOCKER_BUILDX_PUSH) --platform linux/s390x -t $( REGISTRY) /$( REPOSITORY) :latest-s390x -t $( REGISTRY) /$( REPOSITORY) :${ VERSION } -s390x .
2019-02-02 12:08:19 +13:00
2025-04-27 20:09:52 +02:00
##@ Nightly scheduling
.PHONY : nightly -build
nightly-build : ## Nightly build command for docker images in both flavours (distroless / alpine)
2024-01-20 19:48:04 +01:00
$( DOCKER_BUILDX_X_PLATFORM) -t $( REGISTRY) /$( REPOSITORY) -nightly:latest -t $( REGISTRY) /$( REPOSITORY) -nightly:${ DATE } .
$( DOCKER_BUILDX_X_PLATFORM_ALPINE) -t ${ REGISTRY } /$( REPOSITORY) -nightly:latest-alpine -t $( REGISTRY) /$( REPOSITORY) -nightly:${ DATE } -alpine .
2023-11-18 14:56:29 +01:00
2025-04-27 20:09:52 +02:00
.PHONY : nightly -push
nightly-push : ## Nightly push command for docker images in both flavours (distroless / alpine)
2024-01-20 19:48:04 +01:00
$( DOCKER_BUILDX_PUSH_X_PLATFORM) -t $( REGISTRY) /$( REPOSITORY) -nightly:latest -t $( REGISTRY) /$( REPOSITORY) -nightly:${ DATE } .
$( DOCKER_BUILDX_PUSH_X_PLATFORM_ALPINE) -t ${ REGISTRY } /$( REPOSITORY) -nightly:latest-alpine -t $( REGISTRY) /$( REPOSITORY) -nightly:${ DATE } -alpine .
2023-11-18 14:56:29 +01:00
2025-04-27 20:09:52 +02:00
##@ Docs
2020-11-07 16:31:31 +00:00
.PHONY : generate
2025-04-27 20:09:52 +02:00
generate : ## Generate alpha config docs from golang structs
2020-11-07 16:31:31 +00:00
go generate ./pkg/...
.PHONY : verify -generate
2025-04-27 20:09:52 +02:00
verify-generate : generate ## Verify command to check if alpha config docs are in line with golang struct changes
2020-11-07 16:31:31 +00:00
git diff --exit-code
2025-04-27 20:09:52 +02:00
##@ Miscellaneous
2019-01-04 10:58:30 +00:00
.PHONY : test
2025-04-27 20:09:52 +02:00
test : lint ## Run all Go tests
2020-05-10 07:29:37 +01:00
GO111MODULE = on $( GO) test $( TESTCOVER) -v -race ./...
2019-01-04 10:58:30 +00:00
.PHONY : release
2025-04-27 20:09:52 +02:00
release : validate -go -version lint test ## Create a full release for all architectures (binaries and checksums)
2019-10-29 10:27:08 -07:00
BINARY = ${ BINARY } VERSION = ${ VERSION } ./dist.sh
2020-05-09 16:07:46 +01:00
2025-04-27 20:09:52 +02:00
.PHONY : clean
clean : ## Cleanup release and build files
-rm -rf release
-rm -f $( BINARY)
.PHONY : lint
lint : validate -go -version ## Lint all files using golangci-lint
GO111MODULE = on $( GOLANGCILINT) run
2020-05-09 16:07:46 +01:00
.PHONY : validate -go -version
2025-04-27 20:09:52 +02:00
validate-go-version : ## Validate Go environment requirements
2020-05-09 16:07:46 +01:00
@if [ $( GO_MAJOR_VERSION) -gt $( MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ] ; then \
exit 0 ; \
elif [ $( GO_MAJOR_VERSION) -lt $( MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ] ; then \
echo '$(GO_VERSION_VALIDATION_ERR_MSG)' ; \
exit 1; \
elif [ $( GO_MINOR_VERSION) -lt $( MINIMUM_SUPPORTED_GO_MINOR_VERSION) ] ; then \
echo '$(GO_VERSION_VALIDATION_ERR_MSG)' ; \
exit 1; \
fi
2020-05-03 21:05:40 +01:00
# local-env can be used to interact with the local development environment
2020-05-07 22:59:43 +01:00
# eg:
2023-11-18 14:56:29 +01:00
# make local-env-up # Bring up a basic test environment
# make local-env-down # Tear down the basic test environment
# make local-env-nginx-up # Bring up an nginx based test environment
# make local-env-nginx-down # Tead down the nginx based test environment
2020-05-03 21:05:40 +01:00
.PHONY : local -env -%
local-env-% :
make -C contrib/local-environment $*