From 46f589c539fb9053c5017f02d9303a324bfca6a2 Mon Sep 17 00:00:00 2001 From: Matej Matuska <75834032+matejmatuska@users.noreply.github.com> Date: Thu, 4 Aug 2022 17:30:34 +0200 Subject: [PATCH 02/16] Add Makefile target for building RPMs locally and in containers Add `build` target to build locally and `build_container` to build locally in a container. Building in containers has the benefit of being able to build on all the different platforms e.g. both on el7 and el8. Run `make build` to build locally. Run `make build_container` to build in a container. Note that BUILD_CONTAINER environment variable must be set. Run `clean_containers` to clean up container images used for building. The containers are built from Containerfiles in `res/container-builds/`. Since Containerfiles for all the Fedora versions would differ only in the `FROM` statement, a generic Containerfile `Containerfile.fedora_generic` is used and the `FROM` statement is inserted in the Makefile. For el7 builds CentOS 7 container is used, because rpm-build package is not available in ubi7 image. Builds cannot be run in parallel because containers operate on the same files, which would result in them conflicting. Also update `.gitignore` to exclude build files from git. --- .gitignore | 5 ++ Makefile | 63 +++++++++++++++++-- res/container-builds/Containerfile.centos7 | 11 ++++ .../Containerfile.fedora_generic | 12 ++++ res/container-builds/Containerfile.ubi8 | 12 ++++ 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 res/container-builds/Containerfile.centos7 create mode 100644 res/container-builds/Containerfile.fedora_generic create mode 100644 res/container-builds/Containerfile.ubi8 diff --git a/.gitignore b/.gitignore index d9ae04d..e9c1edb 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,8 @@ res/schema/schemas.py leapp.db # do not ignore the directory with tests... !tests/scripts/ + +packaging/BUILD/ +packaging/RPMS/ +packaging/SRPMS/ +packaging/sources/ diff --git a/Makefile b/Makefile index 59d5118..22d29a4 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,13 @@ help: @echo " - available containers are: rhel7, rhel8, rhel9" @echo " test_container_all runs linter and tests in all available containers" @echo " lint runs just the linter" - @echo " clean_containers cleans up all testing containers" + @echo " build create the RPM" + @echo " build_container create the RPM in container" + @echo " - set BUILD_CONTAINER to select the container" + @echo " - available containers are: el7, el8, f35, f36, rawhide" + @echo " - this can't be used to build in parallel," + @echo " as build containers operate on the same files" + @echo " clean_containers clean container images used for building" @echo "" @echo "Possible use:" @echo " make " @@ -85,11 +91,12 @@ help: @echo " PR=7 SUFFIX='my_additional_suffix' make " @echo " MR=6 COPR_CONFIG='path/to/the/config/copr/file' " @echo " TEST_CONTAINER=rhel7 make test_container" + @echo " BUILD_CONTAINER=el7 make build_container" @echo "" clean: @echo "--- Clean repo ---" - @rm -rf packaging/{sources,SRPMS}/ + @rm -rf packaging/{sources,SRPMS,BUILD,BUILDROOT,RPMS}/ @rm -rf build/ dist/ *.egg-info @find . -name '__pycache__' -exec rm -fr {} + @find . -name '*.pyc' -exec rm -f {} + @@ -97,7 +104,7 @@ clean: prepare: clean @echo "--- Prepare build directories ---" - @mkdir -p packaging/{sources,SRPMS}/ + @mkdir -p packaging/{sources,SRPMS,BUILD,BUILDROOT,RPMS}/ source: prepare @echo "--- Create source tarball ---" @@ -123,6 +130,50 @@ copr_build: srpm @copr-cli --config $(_COPR_CONFIG) build $(_COPR_REPO) \ packaging/SRPMS/${PKGNAME}-${VERSION}-${RELEASE}*.src.rpm +build: source + @echo "--- Build RPM ${PKGNAME}-${VERSION}-${RELEASE}.el$(DIST_VERSION).rpm ---" + @cp packaging/$(PKGNAME).spec packaging/$(PKGNAME).spec.bak + @sed -i "s/1%{?dist}/$(RELEASE)%{?dist}/g" packaging/$(PKGNAME).spec + @rpmbuild -ba packaging/$(PKGNAME).spec \ + --define "_sourcedir `pwd`/packaging/sources" \ + --define "_srcrpmdir `pwd`/packaging/SRPMS" \ + --define "_builddir `pwd`/packaging/BUILD" \ + --define "_buildrootdir `pwd`/packaging/BUILDROOT" \ + --define "_rpmdir `pwd`/packaging/RPMS" \ + --define "rhel $(DIST_VERSION)" \ + --define 'dist .el$(DIST_VERSION)' \ + --define 'el$(DIST_VERSION) 1' || FAILED=1 + @mv packaging/$(PKGNAME).spec.bak packaging/$(PKGNAME).spec + +build_container: + @case "$$BUILD_CONTAINER" in \ + el7) \ + _CONT_FILE="Containerfile.centos7"; \ + ;; \ + el8) \ + _CONT_FILE="Containerfile.ubi8"; \ + ;; \ + f3[56]|rawhide) \ + [ $$BUILD_CONTAINER = rawhide ] && VERSION=latest || VERSION=$${BUILD_CONTAINER: -2}; \ + _CONT_FILE=".Containerfile.$${BUILD_CONTAINER}"; \ + cp res/container-builds/Containerfile.fedora_generic res/container-builds/$$_CONT_FILE && \ + sed -i "1i FROM fedora:$${VERSION}" res/container-builds/$$_CONT_FILE \ + ;; \ + "") \ + echo "BUILD_CONTAINER must be set"; \ + exit 1; \ + ;; \ + *) \ + echo "Available containers are el7, el8, f35, f36, rawhide"; \ + exit 1; \ + ;; \ + esac && \ + echo "--- Preparing $$BUILD_CONTAINER container for building ---" && \ + IMAGE_NAME="leapp-build-$${BUILD_CONTAINER}"; \ + podman build -f res/container-builds/$$_CONT_FILE -t $$IMAGE_NAME res/container-builds/ && \ + podman run --rm -ti -v $$PWD:/payload:Z --name "$${IMAGE_NAME}-cont" $$IMAGE_NAME && \ + [ '$${_CONT_FILE:0:1}' = '.' ] && rm -f res/container-builds/$$_CONT_FILE || : + print_release: @echo $(RELEASE) @@ -179,8 +230,8 @@ test_container_all: done clean_containers: - @for i in "leapp-tests-rhel"{7,8,9}; do \ - podman rmi "$$i" > /dev/null 2>&1 || :; \ + @for i in "leapp-build-"{el7,el8,f35,f36,rawhide} "leapp-tests-rhel"{7,8,9}; do \ + podman rmi "$$i" > /dev/null 2>&1 || :; \ done lint: @@ -216,4 +267,4 @@ fast_lint: echo "No files to lint."; \ fi -.PHONY: clean copr_build install install-deps install-test srpm test test_container test_container_all lint fast_lint clean_containers +.PHONY: clean copr_build build build_container install install-deps install-test srpm test test_container test_container_all lint fast_lint clean_containers diff --git a/res/container-builds/Containerfile.centos7 b/res/container-builds/Containerfile.centos7 new file mode 100644 index 0000000..a3b0ea0 --- /dev/null +++ b/res/container-builds/Containerfile.centos7 @@ -0,0 +1,11 @@ +FROM centos:7 + +VOLUME /payload + +ENV DIST_VERSION 7 + +RUN yum update -y && \ + yum install -y make git rpm-build python-setuptools python-devel + +WORKDIR /payload +ENTRYPOINT make build diff --git a/res/container-builds/Containerfile.fedora_generic b/res/container-builds/Containerfile.fedora_generic new file mode 100644 index 0000000..c0af2b7 --- /dev/null +++ b/res/container-builds/Containerfile.fedora_generic @@ -0,0 +1,12 @@ +# FROM statement is inserted in Makefile + +VOLUME /payload + +ENV DIST_VERSION 8 + +RUN dnf update -y && \ + dnf install -y python3 make git rpm-build python3-devel + #yum install -y python3-pip && \ python3 -m pip install --upgrade pip==20.3.4 + +WORKDIR /payload +ENTRYPOINT make build diff --git a/res/container-builds/Containerfile.ubi8 b/res/container-builds/Containerfile.ubi8 new file mode 100644 index 0000000..0b2c021 --- /dev/null +++ b/res/container-builds/Containerfile.ubi8 @@ -0,0 +1,12 @@ +FROM registry.access.redhat.com/ubi8/ubi:latest + +VOLUME /payload + +ENV DIST_VERSION 8 + +RUN dnf update -y && \ + dnf install -y python3 make git rpm-build python3-devel + #yum install -y python3-pip && \ python3 -m pip install --upgrade pip==20.3.4 + +WORKDIR /payload +ENTRYPOINT make build -- 2.39.0