Skip to content

Commit

Permalink
Makefile: Simplify building routine
Browse files Browse the repository at this point in the history
The makefile used in project has been simplified to make
building images more easy. It has been also unified with the
other makefiles used in osbuild projects.
  • Loading branch information
elkoniu committed Feb 28, 2024
1 parent 070bdb6 commit e49eefc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 128 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
fi
echo "deploy=${IMG_DEPLOY}" >>$GITHUB_OUTPUT
echo "images=$(make img-list)" >>$GITHUB_OUTPUT
echo "images=$(make list-targets)" >>$GITHUB_OUTPUT
echo "now=$(date -u '+%Y%m%d%H%M')" >>$GITHUB_OUTPUT
- name: "Print Parameters"
env:
Expand Down Expand Up @@ -103,7 +103,7 @@ jobs:
env:
IMG_BUILDER: ${{ steps.buildx.outputs.name }}
IMG_TARGET: ${{ matrix.image }}
run: make img-bake
run: make bake

- name: "Authenticate to GHCR"
if: ${{ needs.config.outputs.deploy == 'yes' }}
Expand All @@ -127,4 +127,5 @@ jobs:
IMG_BAKE_ARGS: --push
IMG_BUILDER: ${{ steps.buildx.outputs.name }}
IMG_TARGET: ${{ matrix.image }}
run: make img-bake
run: make bake

182 changes: 57 additions & 125 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,71 @@
# container images of the osbuild-containers repository.
#

#
# Global Setup
#
# This section sets some global parameters that get rid of some old `make`
# annoyences.
#
# SHELL
# We standardize on `bash` for better inline scripting capabilities,
# and we always enable `pipefail`, to make sure individual failures
# in a pipeline will be treated as failure.
#
# .SECONDARY:
# An empty SECONDARY target signals gnu-make to keep every intermediate
# files around, even on failure. We want intermediates to stay around
# so we get better caching behavior.
#

SHELL := /bin/bash -eox pipefail

.SECONDARY:

#
# Parameters
#
# The set of global parameters that can be controlled by the caller and the
# calling environment.
#
# BUILDDIR
# Path to the directory used to store build artifacts. This defaults
# to `./build`, so all artifacts are stored in a subdirectory that can
# be easily cleaned.
#
# SRCDIR
# Path to the source code directory. This defaults to `.`, so it
# expects `make` to be called from within the source directory.
#
# BIN_*
# For all binaries that are executed as part of this makefile, a
# variable called `BIN_<exe>` defines the path or name of the
# executable. By default, they are set to the name of the binary.
#
BIN_DOCKER ?= docker
BIN_JQ ?= jq
BIN_MKDIR ?= mkdir

BUILDDIR ?= ./build
SRCDIR ?= .

BIN_DOCKER ?= docker
BIN_JQ ?= jq
BIN_MKDIR ?= mkdir

#
# Generic Targets
#
# The following is a set of generic targets used across the makefile. The
# following targets are defined:
#
# help
# This target prints all supported targets. It is meant as
# documentation of targets we support and might use outside of this
# repository.
# This is also the default target.
#
# $(BUILDDIR)/
# $(BUILDDIR)/%/
# This target simply creates the specified directory. It is limited to
# the build-dir as a safety measure. Note that this requires you to use
# a trailing slash after the directory to not mix it up with regular
# files. Lastly, you mostly want this as order-only dependency, since
# timestamps on directories do not affect their content.
#
# FORCE
# Dummy target to use as dependency to force `.PHONY` behavior on
# targets that cannot use `.PHONY`.
#
IMG_BAKE_ARGS ?=
IMG_BUILDER ?= default
IMG_TARGET ?= osbuild-ci-latest

.PHONY: help
help:
@echo "make [TARGETS...]"
@echo
@echo "This is the maintenance makefile of osbuild/containers. The"
@echo "following targets are available:"
@echo
@echo " help: Print this usage information."
@echo "This is the maintenance makefile of osbuild/containers."
@echo "The following targets are available:"
@echo
@echo " img-setup: Prepare local docker for image builds"
@echo " img-list: List image targets as JSON array"
@echo " img-tags: List image tags as JSON array"
@echo " img-bake: Build images via docker-buildx-bake"

$(BUILDDIR)/:
$(BIN_MKDIR) -p "$@"

$(BUILDDIR)/%/:
$(BIN_MKDIR) -p "$@"

.PHONY: FORCE
FORCE:
@echo " help: Print this usage information."
@echo " setup-builder: Prepare local docker for image builds"
@echo " list-targets: List all targets from bakefile."
@echo " list-tags: List all groups from bakefile."
@echo " inspect-target: Show details of target"
@echo " bake: Build images via docker-buildx-bake"
@echo ""
@echo "Additional env variables which can be overwritten from command line:"
@echo "IMG_TARGET: Set image to build / inspect (default osbuild-ci-latest)"
@echo "IMG_BAKE_ARGS: Add extra arguments for docker bake"
@echo "IMG_BUILDER: Set custom builder"
@echo ""
@echo "Examples:"
@echo "make setup-builder"
@echo "make list-targets"
@echo "make inspect-target IMG_TARGET=osbuild-ci-latest"
@echo "make bake IMG_TARGET=osbuild-ci-latest"
@echo ""
@echo "When no extra parameters are specified IMG_TARGET defaults"
@echo "to osbuild-ci-latest"


.PHONY: list-targets
list-targets:
@$(BIN_DOCKER) \
buildx \
bake \
--print \
all-images \
| jq -c '.target | keys'

#
# Image Builds
#
# The following section provides common helpers around `docker buildx`, which
# we use to build all images. For more control, you should invoke buildx
# directly.
#
.PHONY: list-tags
list-tags:
@$(BIN_DOCKER) \
buildx \
bake \
--print \
all-images \
| jq -c '[.target[].tags[]]'

IMG_BAKE_ARGS ?=
IMG_BUILDER ?= default
IMG_TARGET ?= default
.PHONY: inspect-target
inspect-target:
@$(BIN_DOCKER) \
buildx \
bake \
--print \
$(IMG_TARGET)

.PHONY: img-setup
img-setup:
.PHONY: setup-builder
setup-builder:
@if \
! $(BIN_DOCKER) \
buildx \
Expand All @@ -138,30 +92,8 @@ img-setup:
--bootstrap \
--builder "$(IMG_BUILDER)"

.PHONY: img-list
img-list:
@$(BIN_DOCKER) \
buildx \
bake \
--builder "$(IMG_BUILDER)" \
--print \
$(IMG_BAKE_ARGS) \
$(IMG_TARGET) \
| $(BIN_JQ) -c '.target | keys'

.PHONY: img-tags
img-tags:
@$(BIN_DOCKER) \
buildx \
bake \
--builder "$(IMG_BUILDER)" \
--print \
$(IMG_BAKE_ARGS) \
$(IMG_TARGET) \
| $(BIN_JQ) -c '[.target[].tags[]]'

.PHONY: img-bake
img-bake:
.PHONY: bake
bake:
@$(BIN_DOCKER) \
buildx \
bake \
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ The requirements for this project are:

### Build

This project contains makefile with predefined targets which allows
easy building of defined images.

If not specified, the default `IMG_TARGET` is set to `osbuild-ci-latest`

Example usage:

```sh
# Prepare host environment
make setup-builder

# Show abailable targets
make list-targets

# Show details of particular target
make inspect-target IMG_TARGET=osbuild-ci-latest

# Build selected target
make bake IMG_TARGET=osbuild-ci-latest
```

### Manual build

Container images in `./src/images/` are built via Docker BuildKit. The build
instructions are available in `./docker-bake.hcl`, and can be executed via
`docker buildx bake`. To build **all** targets, use:
Expand Down

0 comments on commit e49eefc

Please sign in to comment.