Skip to content

Commit

Permalink
docs: update of code for Dockerized package building
Browse files Browse the repository at this point in the history
  • Loading branch information
jhermann committed Mar 9, 2020
1 parent 1475df2 commit daaa623
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
37 changes: 25 additions & 12 deletions dh-virtualenv/Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@
# Build arguments, as provided by 'build.sh'
ARG DIST_ID="debian"
ARG CODENAME="stretch"
ARG PYVERSION="3"
ARG PKGNAME

# Other build arguments (adapt as needed)
ARG NODEREPO="node_8.x"
ARG NODEREPO="node_10.x"
ARG DEB_POOL="http://ftp.nl.debian.org/debian/pool/main"

## Start package builder image for the chosen platform
FROM ${DIST_ID}:${CODENAME} AS dpkg-build

# Pass build args into image scope
ARG CODENAME
ARG PYVERSION
ARG PKGNAME
ARG NODEREPO
ARG DEB_POOL

# Install build tools and package build deps including nodejs
RUN env LANG=C apt-get update -qq -o Acquire::Languages=none \
RUN ( test "${CODENAME}" = "xenial" \
&& echo "deb [trusted=yes] http://ppa.launchpad.net/deadsnakes/ppa/ubuntu ${CODENAME} main" \
>/etc/apt/sources.list.d/deadsnakes.list || : ) \
&& env LANG=C apt-get update -qq -o Acquire::Languages=none \
&& env LANG=C DEBIAN_FRONTEND=noninteractive apt-get install \
-yqq --no-install-recommends -o Dpkg::Options::=--force-unsafe-io \
\
"" \
apt-transport-https \
apt-utils \
build-essential \
Expand All @@ -39,24 +44,28 @@ RUN env LANG=C apt-get update -qq -o Acquire::Languages=none \
libjs-sphinxdoc \
libparse-debianchangelog-perl \
lsb-release \
python3 \
python3-dev \
python3-pip \
python${PYVERSION} \
python${PYVERSION}-venv \
python${PYVERSION}-tk \
python${PYVERSION}-dev \
python3-pkg-resources \
python3-setuptools \
python-virtualenv \
python3-venv \
sphinx-rtd-theme-common \
tar \
\
"" \
libcurl4-openssl-dev \
libffi-dev \
libfontconfig1 \
libfreetype6-dev \
libjpeg-dev \
libncurses5-dev \
libncursesw5-dev \
libssl-dev \
libxml2-dev \
libxslt1-dev \
libyaml-dev \
libz-dev \
libzmq3-dev \
&& ( curl -sL https://deb.nodesource.com/gpgkey/nodesource.gpg.key \
| env APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=true apt-key add - ) \
Expand All @@ -77,14 +86,18 @@ RUN env LANG=C apt-get update -qq -o Acquire::Languages=none \
# Install updated Python tooling and a current 'dh-virtualenv'
WORKDIR /dpkg-build
ADD "${DEB_POOL}/d/dh-virtualenv/dh-virtualenv_1.1-1_all.deb" ./
RUN dpkg -i --force-unsafe-io --ignore-depends=sphinx-rtd-theme-common *_all.deb \
&& python3 -m pip install -U pip \
&& python3 -m pip install -U virtualenv setuptools wheel
RUN dpkg -i --force-unsafe-io \
--ignore-depends=python:any \
--ignore-depends=virtualenv \
--ignore-depends=sphinx-rtd-theme-common \
*_all.deb \
&& python${PYVERSION} -m easy_install pip \
&& python${PYVERSION} -m pip install -U setuptools wheel

# Build project and show metadata of built package
COPY ./ ./
RUN hr() { printf '\n %-74s\n' "[$1]" | tr ' ' = ; } \
&& hr Versions && python3 -m pip --version && dh_virtualenv --version \
&& hr Versions && python${PYVERSION} -m pip --version && dh_virtualenv --version \
&& sed -i -r \
-e "1s/(UNRELEASED|unstable|jessie|stretch|xenial|bionic)/$(lsb_release -cs)/g" \
debian/changelog \
Expand Down
45 changes: 39 additions & 6 deletions dh-virtualenv/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

set -e

# If you change this, you MUST also change "debian/control" and "debina/rules"
PYTHON_MINOR="3.8" # Deadsnakes version on Ubuntu

# Check build environment
if ! command which "dh_listpackages" >/dev/null 2>&1; then
echo >&2 "ERROR: You must 'apt install debhelper' on the build host."
exit 1
fi

# Get build platform as 1st argument, and collect project metadata
image="${1:?You MUST provide a docker image name}"; shift
dist_id=${image%%:*}
Expand All @@ -13,15 +22,39 @@ pypi_name="$(./setup.py --name)"
pypi_version="$(./setup.py --version)"
pkgname="$(dh_listpackages)"
tag=$pypi_name-$dist_id-$codename
staging="$pypi_name-$pypi_version"

build_opts=(
-f Dockerfile.build
--tag $tag
--build-arg "DIST_ID=$dist_id"
--build-arg "CODENAME=$codename"
--build-arg "PKGNAME=$pkgname"
)

if test "$dist_id" = "ubuntu"; then
build_opts+=( --build-arg "PYVERSION=$PYTHON_MINOR" )
fi


# Create Docker staging directory and apply environment changes
echo "*** Creating staging directory (sdist)"
echo "*** Ignore warnings regarding non-matching filters..."
python3 ./setup.py -q sdist -k
if test "$dist_id" = "debian"; then
sed -i -e "s/python$PYTHON_MINOR/python3/g" "$staging/debian/control"
fi

mkdir -p build
rm -rf build/docker.staging 2>/dev/null
mv "$staging" build/docker.staging


# Build in Docker container, save results, and show package info
echo
echo "*** Building DEB package (takes a while)"
rm -f dist/${pkgname}?*${pypi_version//./?}*${codename}*.*
docker build --tag $tag \
--build-arg "DIST_ID=$dist_id" \
--build-arg "CODENAME=$codename" \
--build-arg "PKGNAME=$pkgname" \
-f Dockerfile.build \
"$@" .
docker build "${build_opts[@]}" "$@" build/docker.staging
mkdir -p dist
docker run --rm $tag tar -C /dpkg -c . | tar -C dist -x
ls -lh dist/${pkgname}?*${pypi_version//./?}*${codename}*.*
18 changes: 17 additions & 1 deletion docs/packaging-howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ the given platform and existing project metadata is stored into a bunch of varia

.. literalinclude:: ../dh-virtualenv/build.sh
:language: shell
:end-before: Create Docker staging

On Ubuntu, the Deadsnakes packages are used, thus the
default of the ``PYVERSION`` Docker argument is replaced.
Since the ``control`` file contains that more detailed version,
the reverse is done for Debian builds – ``3.x`` is replaced by just ``3``.

To keep this and other possible changes out of git's reach,
a staging copy of the workdir is created via ``sdist``
– adding ``-k`` keeps the expanded distribution tree intact, so we can change
and finally move it to ``build/docker.staging``.
Creating that copy based on a source distribution also tests that
such a distribution is complete and buildable, as a welcome side-effect.

.. literalinclude:: ../dh-virtualenv/build.sh
:language: shell
:start-at: Create Docker staging
:end-before: Build in Docker

After that prep work, we get to build our package, passing along the needed build arguments.
Expand All @@ -51,7 +68,6 @@ Also the package metadata is shown for a quick visual check if everything looks
:language: shell
:start-at: Build in Docker


.. rubric:: The Dockerfile

This is the start of the Dockerfile, setting up some parameters used in the ``RUN`` instructions,
Expand Down

0 comments on commit daaa623

Please sign in to comment.